-
Notifications
You must be signed in to change notification settings - Fork 3
test(shared): achieve 100% unit test coverage #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { canLoadPage } from "./canLoadPage"; | ||
|
|
||
| describe("canLoadPage", () => { | ||
| const emptyPages = new Map<number, number[]>(); | ||
| const emptyLoading = new Set<number>(); | ||
|
|
||
| it("returns true for first page in empty state", () => { | ||
| expect(canLoadPage(0, emptyPages, emptyLoading, 0, 20, true)).toBe(true); | ||
| }); | ||
|
|
||
| it("returns false when page is already loaded", () => { | ||
| const pages = new Map([[0, [1, 2, 3]]]); | ||
| expect(canLoadPage(0, pages, emptyLoading, 100, 20, true)).toBe(false); | ||
| }); | ||
|
|
||
| it("returns false when page is currently loading", () => { | ||
| const loadingPages = new Set([2]); | ||
| expect(canLoadPage(2, emptyPages, loadingPages, 100, 20, true)).toBe(false); | ||
| }); | ||
|
|
||
| it("returns false when page is both loaded and loading", () => { | ||
| const pages = new Map([[1, [1]]]); | ||
| const loadingPages = new Set([1]); | ||
| expect(canLoadPage(1, pages, loadingPages, 100, 20, true)).toBe(false); | ||
| }); | ||
|
|
||
| it("returns false when page * pageSize >= total", () => { | ||
| // total=50, pageSize=20: page 3 → 3*20=60 >= 50 | ||
| expect(canLoadPage(3, emptyPages, emptyLoading, 50, 20, true)).toBe(false); | ||
| }); | ||
|
|
||
| it("returns false when page * pageSize exactly equals total", () => { | ||
| // total=40, pageSize=20: page 2 → 2*20=40 >= 40 | ||
| expect(canLoadPage(2, emptyPages, emptyLoading, 40, 20, true)).toBe(false); | ||
| }); | ||
|
|
||
| it("returns true when page * pageSize is within total", () => { | ||
| // total=50, pageSize=20: page 2 → 2*20=40 < 50 | ||
| expect(canLoadPage(2, emptyPages, emptyLoading, 50, 20, true)).toBe(true); | ||
| }); | ||
|
|
||
| it("skips total check when total is 0", () => { | ||
| expect(canLoadPage(0, emptyPages, emptyLoading, 0, 20, true)).toBe(true); | ||
| }); | ||
|
|
||
| it("returns false when !hasMore and page exceeds last page index (total=0)", () => { | ||
| // total=0, hasMore=false: Math.floor(0/20)=0, page=1 > 0 → false | ||
| expect(canLoadPage(1, emptyPages, emptyLoading, 0, 20, false)).toBe(false); | ||
| }); | ||
|
|
||
| it("returns true for page 0 when !hasMore and total is 0", () => { | ||
| // Math.floor(0/20)=0, page=0 is NOT > 0 | ||
| expect(canLoadPage(0, emptyPages, emptyLoading, 0, 20, false)).toBe(true); | ||
| }); | ||
|
|
||
| it("returns true when hasMore is true even if page is beyond floor(total/pageSize)", () => { | ||
| // total=50, pageSize=20: floor(50/20)=2, page=2 → NOT > 2, but line 11 catches it (2*20=40<50 → ok) | ||
| // Use total=0 so line 11 is skipped: page=5, hasMore=true → line 13 not triggered → true | ||
| expect(canLoadPage(5, emptyPages, emptyLoading, 0, 20, true)).toBe(true); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { findMissingPages } from "./findMissingPages"; | ||
|
|
||
| describe("findMissingPages", () => { | ||
zaewc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const emptyPages = new Map<number, number[]>(); | ||
| const emptyLoading = new Set<number>(); | ||
|
|
||
| it("returns all pages in range when nothing is loaded or loading", () => { | ||
| const result = findMissingPages(0, 2, emptyPages, emptyLoading); | ||
| expect(result).toEqual([0, 1, 2]); | ||
| }); | ||
|
|
||
| it("returns empty array when start > end", () => { | ||
| const result = findMissingPages(5, 3, emptyPages, emptyLoading); | ||
| expect(result).toEqual([]); | ||
| }); | ||
|
|
||
| it("returns empty array when start === end and page is loaded", () => { | ||
| const pages = new Map([[1, [1, 2]]]); | ||
| const result = findMissingPages(1, 1, pages, emptyLoading); | ||
| expect(result).toEqual([]); | ||
| }); | ||
|
|
||
| it("excludes pages that are already loaded", () => { | ||
| const pages = new Map([[1, [1, 2]]]); | ||
| const result = findMissingPages(0, 3, pages, emptyLoading); | ||
| expect(result).toEqual([0, 2, 3]); | ||
| }); | ||
|
|
||
| it("excludes pages that are currently loading", () => { | ||
| const loadingPages = new Set([2]); | ||
| const result = findMissingPages(0, 3, emptyPages, loadingPages); | ||
| expect(result).toEqual([0, 1, 3]); | ||
| }); | ||
|
|
||
| it("excludes pages that are both loaded and loading", () => { | ||
| const pages = new Map([[0, [1]]]); | ||
| const loadingPages = new Set([2]); | ||
| const result = findMissingPages(0, 3, pages, loadingPages); | ||
| expect(result).toEqual([1, 3]); | ||
| }); | ||
|
|
||
| it("returns empty array when all pages are loaded", () => { | ||
| const pages = new Map([ | ||
| [0, [1]], | ||
| [1, [2]], | ||
| [2, [3]], | ||
| ]); | ||
| const result = findMissingPages(0, 2, pages, emptyLoading); | ||
| expect(result).toEqual([]); | ||
| }); | ||
|
|
||
| it("returns empty array when all pages are loading", () => { | ||
| const loadingPages = new Set([0, 1, 2]); | ||
| const result = findMissingPages(0, 2, emptyPages, loadingPages); | ||
| expect(result).toEqual([]); | ||
| }); | ||
|
|
||
| it("handles single-page range that is missing", () => { | ||
| const result = findMissingPages(3, 3, emptyPages, emptyLoading); | ||
| expect(result).toEqual([3]); | ||
| }); | ||
|
|
||
| it("handles large range with sparse loaded pages", () => { | ||
| const pages = new Map([ | ||
| [2, []], | ||
| [5, []], | ||
| ]); | ||
| const result = findMissingPages(0, 6, pages, emptyLoading); | ||
| expect(result).toEqual([0, 1, 3, 4, 6]); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.