From 8ce5f6405d5d02266ef6b2ffc2b08c04ba430d98 Mon Sep 17 00:00:00 2001 From: Daniel Bisgrove Date: Thu, 12 Dec 2024 14:23:56 -0500 Subject: [PATCH] Adding tests --- pages/api/utils/pagePropsHelpers.test.ts | 56 ++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/pages/api/utils/pagePropsHelpers.test.ts b/pages/api/utils/pagePropsHelpers.test.ts index a6c1fbdaf..0ec05829e 100644 --- a/pages/api/utils/pagePropsHelpers.test.ts +++ b/pages/api/utils/pagePropsHelpers.test.ts @@ -1,14 +1,16 @@ import { GetServerSidePropsContext } from 'next'; import { getSession } from 'next-auth/react'; import { session } from '__tests__/fixtures/session'; +import makeSsrClient from 'src/lib/apollo/ssrClient'; import { enforceAdmin, - loadSession, + ensureSessionAndAccountList, loginRedirect, makeGetServerSideProps, } from './pagePropsHelpers'; jest.mock('next-auth/react'); +jest.mock('src/lib/apollo/ssrClient', () => jest.fn()); const context = { query: { accountListId: 'account-list-1' }, @@ -46,12 +48,12 @@ describe('enforceAdmin', () => { }); }); -describe('loadSession', () => { +describe('ensureSessionAndAccountList', () => { it('does not return a redirect if the user is logged in', async () => { const user = { apiToken: 'token' }; (getSession as jest.Mock).mockResolvedValue({ user }); - await expect(loadSession(context)).resolves.toMatchObject({ + await expect(ensureSessionAndAccountList(context)).resolves.toMatchObject({ props: { session: { user }, }, @@ -61,12 +63,58 @@ describe('loadSession', () => { it('returns a redirect if the user is not logged in', async () => { (getSession as jest.Mock).mockResolvedValue(null); - await expect(loadSession(context)).resolves.toMatchObject({ + await expect(ensureSessionAndAccountList(context)).resolves.toMatchObject({ redirect: { destination: '/login?redirect=%2Fpage%3Fparam%3Dvalue', }, }); }); + + describe('redirects to the default account list if the URL contains "_"', () => { + const context = { + req: { url: '/accountLists/_/contacts' }, + resolvedUrl: '/filters?param=value', + } as unknown as GetServerSidePropsContext; + beforeEach(() => { + const user = { apiToken: 'token' }; + (getSession as jest.Mock).mockResolvedValue({ user }); + const query = jest.fn(); + (makeSsrClient as jest.Mock).mockReturnValue({ + query: query, + }); + query.mockResolvedValueOnce({ + data: { + user: { + defaultAccountList: 'defaultAccountList', + }, + }, + }); + }); + + it('redirects to the contacts page with default account list', async () => { + await expect(ensureSessionAndAccountList(context)).resolves.toMatchObject( + { + redirect: { + destination: '/accountLists/defaultAccountList/contacts', + }, + props: {}, + }, + ); + }); + + it('redirects to dashboard with default account list"', async () => { + await expect( + ensureSessionAndAccountList({ + req: { url: '/accountLists/_' }, + } as unknown as GetServerSidePropsContext), + ).resolves.toMatchObject({ + redirect: { + destination: '/accountLists/defaultAccountList', + }, + props: {}, + }); + }); + }); }); describe('makeGetServerSideProps', () => {