Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-bizz committed Dec 12, 2024
1 parent f5c5c10 commit 8ce5f64
Showing 1 changed file with 52 additions and 4 deletions.
56 changes: 52 additions & 4 deletions pages/api/utils/pagePropsHelpers.test.ts
Original file line number Diff line number Diff line change
@@ -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' },
Expand Down Expand Up @@ -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 },
},
Expand All @@ -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', () => {
Expand Down

0 comments on commit 8ce5f64

Please sign in to comment.