Skip to content

Commit

Permalink
[#51] Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tednguyendev committed Jun 21, 2023
1 parent 0e71f45 commit 2619cbf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 75 deletions.
2 changes: 1 addition & 1 deletion nextjs-13/src/app/send/page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('SendNewsletter', () => {

await waitFor(() => {
expect(toast.error).toHaveBeenCalledWith(
'Please select atleast one newsletter',
'Please select at least one newsletter',
{
position: 'top-center',
autoClose: 3000,
Expand Down
71 changes: 25 additions & 46 deletions nextjs-13/src/components/AppLayout/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { render, screen } from '@testing-library/react';
import { redirect, usePathname } from 'next/navigation';
import { useSession } from 'next-auth/react';
import { usePathname } from 'next/navigation';

import AppLayout from './';

Expand All @@ -12,56 +11,36 @@ describe('AppLayout', () => {
usePathname.mockReturnValue('localhost:3003/send');
});

describe('Session status is "unauthenticated', () => {
it('redirects to home page', () => {
useSession.mockReturnValue({ status: 'unauthenticated' });
it('renders the component', () => {
render(
<AppLayout>
<></>
</AppLayout>
);

render(
<AppLayout>
<></>
</AppLayout>
);

expect(redirect).toHaveBeenCalledWith('/auth/sign-in');
});
expect(screen.getByTestId('layout-default')).toBeVisible();
});

describe('Session status is "authenticated', () => {
beforeEach(() => {
useSession.mockReturnValue({ status: 'authenticated' });
});

it('renders the component', () => {
render(
<AppLayout>
<></>
</AppLayout>
);

expect(screen.getByTestId('layout-default')).toBeVisible();
});
it('renders nav', () => {
render(
<AppLayout>
<></>
</AppLayout>
);

it('renders nav', () => {
render(
<AppLayout>
<></>
</AppLayout>
);

expect(screen.getByTestId('dashboard__nav')).toBeVisible();
});
expect(screen.getByTestId('dashboard__nav')).toBeVisible();
});

it('adds class dashboard__tab--selected to the selected Link', () => {
render(
<AppLayout>
<></>
</AppLayout>
);
it('adds class dashboard__tab--selected to the selected Link', () => {
render(
<AppLayout>
<></>
</AppLayout>
);

const sendNewsletterTab = screen.getByTestId('send-newsletter-tab');
const sendNewsletterTab = screen.getByTestId('send-newsletter-tab');

expect(sendNewsletterTab).toBeVisible();
expect(sendNewsletterTab.className).toContain('dashboard__tab--selected');
});
expect(sendNewsletterTab).toBeVisible();
expect(sendNewsletterTab.className).toContain('dashboard__tab--selected');
});
});
36 changes: 8 additions & 28 deletions nextjs-13/src/components/AuthLayout/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,18 @@
import { render, screen } from '@testing-library/react';
import { redirect } from 'next/navigation';
import { useSession } from 'next-auth/react';

import AuthLayout from './';

jest.mock('next-auth/react');
jest.mock('next/navigation');

describe('AuthLayout', () => {
describe('Session status is "authenticated', () => {
it('redirects to home page', () => {
useSession.mockReturnValue({ status: 'authenticated' });

render(
<AuthLayout>
<></>
</AuthLayout>
);

expect(redirect).toHaveBeenCalledWith('/');
});
});

describe('Session status is "unauthenticated', () => {
it('renders the component', () => {
useSession.mockReturnValue({ status: 'unauthenticated' });

render(
<AuthLayout>
<></>
</AuthLayout>
);

expect(screen.getByTestId('layout-auth')).toBeVisible();
});
it('renders the component', () => {
render(
<AuthLayout>
<></>
</AuthLayout>
);

expect(screen.getByTestId('layout-auth')).toBeVisible();
});
});

0 comments on commit 2619cbf

Please sign in to comment.