Skip to content

Commit

Permalink
fix notification page.test.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
ryandotfurrer committed Oct 4, 2024
1 parent 370ca56 commit 4f68af9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
37 changes: 20 additions & 17 deletions app/(admin)/admin/notifications/page.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import AdminNotifications from './page';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { getCurrentLeague } from '@/api/apiFunctions';
import React from 'react';
import { sendEmailNotifications } from './actions/sendEmailNotification';
import AdminNotifications from './page';
import React from 'react';

let contentInput: HTMLInputElement,
emailButton: HTMLElement,
emailTestersButton: HTMLElement,
selectAllUsersRadioOption: HTMLElement,
selectRecipientsRadioGroup: HTMLElement,
sendEmailButton: HTMLElement,
subjectInput: HTMLInputElement;

jest.mock('@/api/apiFunctions', () => ({
Expand All @@ -18,16 +19,23 @@ jest.mock('./actions/sendEmailNotification', () => ({
}));

describe('Admin notifications page', () => {
beforeEach(() => {
beforeEach(async () => {
jest.clearAllMocks();

(getCurrentLeague as jest.Mock).mockResolvedValue({
participants: ['12345', '1234', '123'],
leagueName: 'Test League',
});

render(<AdminNotifications />);

contentInput = screen.getByTestId('content-text');
emailButton = screen.getByTestId('send-email');
emailTestersButton = screen.getByTestId('email-testers');
selectAllUsersRadioOption = screen.getByTestId('all-users-option');
selectRecipientsRadioGroup = screen.getByTestId('radio-group-default');
sendEmailButton = screen.getByTestId('send-email');
subjectInput = screen.getByTestId('subject-text');
});

it(`should render it's content`, () => {
(sendEmailNotifications as jest.Mock).mockResolvedValue({});

Expand All @@ -36,30 +44,25 @@ describe('Admin notifications page', () => {
);
expect(adminNotificationsContent).toBeInTheDocument();
});
it('should call the sendEmailNotifications function with the provided inputs', async () => {
const dummyParticipants = ['12345', '1234', '123'];
(getCurrentLeague as jest.Mock).mockResolvedValue({
participants: dummyParticipants,
});

fireEvent.click(emailTestersButton);
it('should call the sendEmailNotifications function with the provided inputs', async () => {
fireEvent.click(selectAllUsersRadioOption);
fireEvent.change(subjectInput, { target: { value: 'Test Title' } });
fireEvent.change(contentInput, {
target: { value: 'Test message section.' },
});

await waitFor(() => {
expect(emailButton).toBeInTheDocument();
expect(sendEmailButton).toBeInTheDocument();
});

fireEvent.submit(emailButton);
fireEvent.submit(sendEmailButton);

await waitFor(() => {
expect(sendEmailNotifications as jest.Mock).toHaveBeenCalledWith({
content: 'Test message section.',
sendEmailUsers: dummyParticipants,
sendEmailUsers: ['12345', '1234', '123'],
subject: 'Test Title',
testBCC: expect.any(Array),
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion app/(admin)/admin/notifications/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const AdminNotifications = (): JSX.Element => {
*/
const getLeagueData = async (): Promise<void> => {
try {
const leagueId = '66c6618900033d179dda';
const leagueId = '66e1cc9000160b10bf2c'; // TEST LEAGUE (DO NOT JOIN)
const leagueData = await getCurrentLeague(leagueId);
setGroupUsers(leagueData.participants);
setLeagueName(leagueData.leagueName);
Expand Down

0 comments on commit 4f68af9

Please sign in to comment.