From d316e3c5cb0c9d402bccfbf0a5e502d01cc97f37 Mon Sep 17 00:00:00 2001 From: Anbraten <6918444+anbraten@users.noreply.github.com> Date: Wed, 17 Apr 2024 10:57:58 +0200 Subject: [PATCH] improve tests --- web/src/compositions/usePaginate.test.ts | 110 ++++++++++++++++++++--- 1 file changed, 100 insertions(+), 10 deletions(-) diff --git a/web/src/compositions/usePaginate.test.ts b/web/src/compositions/usePaginate.test.ts index 742a111c2..fa77c738f 100644 --- a/web/src/compositions/usePaginate.test.ts +++ b/web/src/compositions/usePaginate.test.ts @@ -33,21 +33,53 @@ export const mountComposition = (cb: () => void) => { }; describe('usePaginate', () => { - it('get first repo page', async () => { - const repoSecrets = [ - [{ name: 'repo1' }, { name: 'repo2' }, { name: 'repo3' }], - [{ name: 'repo4' }, { name: 'repo5' }, { name: 'repo6' }], - ]; - const orgSecrets = [ - [{ name: 'org1' }, { name: 'org2' }, { name: 'org3' }], - [{ name: 'org4' }, { name: 'org5' }, { name: 'org6' }], - ]; + const repoSecrets = [ + [{ name: 'repo1' }, { name: 'repo2' }, { name: 'repo3' }], + [{ name: 'repo4' }, { name: 'repo5' }, { name: 'repo6' }], + ]; + const orgSecrets = [ + [{ name: 'org1' }, { name: 'org2' }, { name: 'org3' }], + [{ name: 'org4' }, { name: 'org5' }, { name: 'org6' }], + ]; + it('should get first page', async () => { + let usePaginationComposition = null as unknown as ReturnType; + mountComposition(() => { + usePaginationComposition = usePagination<{ name: string }>( + async (page) => repoSecrets[page - 1], + () => true, + ); + }); + await waitForState(usePaginationComposition.loading, true); + await waitForState(usePaginationComposition.loading, false); + + expect(usePaginationComposition.data.value.length).toBe(3); + expect(usePaginationComposition.data.value[0]).toStrictEqual(repoSecrets[0][0]); + }); + + it('should get first & second page', async () => { + let usePaginationComposition = null as unknown as ReturnType; + mountComposition(() => { + usePaginationComposition = usePagination<{ name: string }>( + async (page) => repoSecrets[page - 1], + () => true, + ); + }); + await waitForState(usePaginationComposition.loading, true); + await waitForState(usePaginationComposition.loading, false); + + usePaginationComposition.nextPage(); + await waitForState(usePaginationComposition.loading, false); + + expect(usePaginationComposition.data.value.length).toBe(6); + expect(usePaginationComposition.data.value.at(-1)).toStrictEqual(repoSecrets[1][2]); + }); + + it('should get first page for each category', async () => { let usePaginationComposition = null as unknown as ReturnType; mountComposition(() => { usePaginationComposition = usePagination<{ name: string }>( async (page, level) => { - console.log('getSingle', page, level); if (level === 'repo') { return repoSecrets[page - 1]; } @@ -60,6 +92,64 @@ describe('usePaginate', () => { await waitForState(usePaginationComposition.loading, true); await waitForState(usePaginationComposition.loading, false); + usePaginationComposition.nextPage(); + await waitForState(usePaginationComposition.loading, false); + + usePaginationComposition.nextPage(); + await waitForState(usePaginationComposition.loading, false); + + usePaginationComposition.nextPage(); + await waitForState(usePaginationComposition.loading, false); + + console.log(usePaginationComposition.data.value); + + expect(usePaginationComposition.data.value.length).toBe(9); + expect(usePaginationComposition.data.value.at(-1)).toStrictEqual(orgSecrets[0][2]); + }); + + it('should reset page and get first page again', async () => { + let usePaginationComposition = null as unknown as ReturnType; + mountComposition(() => { + usePaginationComposition = usePagination<{ name: string }>( + async (page) => repoSecrets[page - 1], + () => true, + ); + }); + await waitForState(usePaginationComposition.loading, true); + await waitForState(usePaginationComposition.loading, false); + + usePaginationComposition.nextPage(); + await waitForState(usePaginationComposition.loading, false); + + usePaginationComposition.resetPage(); + await waitForState(usePaginationComposition.loading, false); + + expect(usePaginationComposition.data.value.length).toBe(3); + expect(usePaginationComposition.data.value[0]).toStrictEqual(repoSecrets[0][0]); + }); + + it('should not hasMore when no data is left', async () => { + let usePaginationComposition = null as unknown as ReturnType; + mountComposition(() => { + usePaginationComposition = usePagination<{ name: string }>( + async (page) => repoSecrets[page - 1], + () => true, + ); + }); + await waitForState(usePaginationComposition.loading, true); + await waitForState(usePaginationComposition.loading, false); + + expect(usePaginationComposition.hasMore.value).toBe(true); + expect(usePaginationComposition.data.value.length).toBe(3); + + usePaginationComposition.nextPage(); + await waitForState(usePaginationComposition.loading, false); + expect(usePaginationComposition.hasMore.value).toBe(true); + expect(usePaginationComposition.data.value.length).toBe(6); + + usePaginationComposition.nextPage(); + await waitForState(usePaginationComposition.loading, false); + expect(usePaginationComposition.hasMore.value).toBe(false); expect(usePaginationComposition.data.value.length).toBe(6); }); });