Skip to content

Commit

Permalink
fixed test after adding in new functions
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-nowicki committed Oct 16, 2024
1 parent 19a416f commit d1b7c32
Showing 1 changed file with 80 additions and 41 deletions.
121 changes: 80 additions & 41 deletions app/(main)/league/all/page.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import {
import Leagues from './page';
import { useDataStore } from '@/store/dataStore';
import { getUserLeagues } from '@/utils/utils';
import { getAllLeagues, addUserToLeague } from '@/api/apiFunctions';
import {
getAllLeagues,
addUserToLeague,
getGameWeek,
getCurrentUserEntries,
} from '@/api/apiFunctions';
import { toast } from 'react-hot-toast';
import Alert from '@/components/AlertNotification/AlertNotification';
import { AlertVariants } from '@/components/AlertNotification/Alerts.enum';
Expand All @@ -26,62 +31,102 @@ jest.mock('@/context/AuthContextProvider', () => ({
}));

jest.mock('@/store/dataStore', () => ({
useDataStore: jest.fn(() => ({
user: {
documentId: '123',
id: '1234',
email: '[email protected]',
leagues: ['league1'],
},
allLeagues: [
{
leagueId: '123',
leagueName: 'Test League',
logo: 'logo.png',
participants: ['123456', '78'],
survivors: ['123456', '78'],
},
],
updateUser: jest.fn(),
})),
useDataStore: jest.fn(),
}));

jest.mock('@/utils/utils', () => ({
getUserLeagues: jest.fn(() => Promise.resolve([])),
cn: jest.fn(),
cn: jest.fn((className) => className),
}));

jest.mock('@/api/apiFunctions', () => ({
getAllLeagues: jest.fn(),
addUserToLeague: jest.fn(),
getGameWeek: jest.fn(),
getCurrentUserEntries: jest.fn(),
}));

const mockAllLeagues = [
{
leagueId: '123',
leagueName: 'Test League',
logo: 'https://findmylogo.com/logo.png',
participants: ['123456', '78'],
survivors: ['123456', '78', '9'],
},
];

const mockEntries = [
{
$id: '123',
name: 'Test Entry',
user: '123',
league: '123',
selectedTeams: [],
eliminated: false,
},
];

const mockGameWeek = {
id: '123',
week: 1,
};

jest.mock('react-hot-toast', () => ({
toast: {
custom: jest.fn(),
},
}));

const mockUseDataStore = useDataStore as jest.MockedFunction<
typeof useDataStore
>;
const mockGetUserLeagues = getUserLeagues as jest.MockedFunction<
typeof getUserLeagues
>;
const mockGetGameWeek = getGameWeek as jest.MockedFunction<typeof getGameWeek>;
const mockGetCurrentUserEntries = getCurrentUserEntries as jest.MockedFunction<
typeof getCurrentUserEntries
>;

describe('Leagues Component', () => {
const mockUseDataStore = useDataStore as unknown as jest.Mock;
const mockGetUserLeagues = getUserLeagues as jest.Mock;
const mockUpdateLeagues = jest.fn();
const mockUpdateUserLeagues = jest.fn();
const mockUpdateGameWeek = jest.fn();
const mockUpdateEntries = jest.fn();
const mockGetAllLeagues = getAllLeagues as jest.Mock;
const mockAddUserToLeague = addUserToLeague as jest.Mock;

beforeEach(() => {
jest.clearAllMocks();
mockUseDataStore.mockReturnValue({
user: { id: '123', leagues: [] },
leagues: [],
updateAllLeagues: mockUpdateLeagues,
updateUserLeagues: mockUpdateUserLeagues,
updateEntries: mockUpdateEntries,
updateGameWeek: mockUpdateGameWeek,
});
});

it('should display GlobalSpinner while loading data', async () => {
render(<Leagues />);
expect(screen.getByTestId('global-spinner')).toBeInTheDocument();
});

it('should render "You are not enrolled in any leagues" message when no leagues are found', async () => {
mockUseAuthContext.isSignedIn = true;
mockGetUserLeagues.mockResolvedValue([]);

mockUseDataStore.mockReturnValue({
user: {
documentId: '123',
email: '[email protected]',
id: '123',
leagues: [],
},
allLeagues: [],
allLeagues: mockAllLeagues,
userLeagues: [],
});

render(<Leagues />);
Expand All @@ -94,24 +139,6 @@ describe('Leagues Component', () => {
});
});

it('should display GlobalSpinner while loading data', async () => {
mockUseAuthContext.isSignedIn = true;

mockUseDataStore.mockReturnValueOnce({
user: {
documentId: '123',
email: '[email protected]',
id: '123',
leagues: [],
},
allLeagues: [],
});

render(<Leagues />);

expect(screen.getByTestId('global-spinner')).toBeInTheDocument();
});

it('should not display GlobalSpinner after loading data', async () => {
mockUseAuthContext.isSignedIn = true;

Expand All @@ -131,6 +158,7 @@ describe('Leagues Component', () => {
survivors: ['123456', '78'],
},
],
userLeagues: [],
});

mockGetUserLeagues.mockResolvedValueOnce([]);
Expand Down Expand Up @@ -161,11 +189,20 @@ describe('Leagues Component', () => {
};

const updateUser = jest.fn();
const updateUserLeagues = jest.fn();
const updateAllLeagues = jest.fn();
const updateGameWeek = jest.fn();
const updateEntries = jest.fn();

mockUseDataStore.mockReturnValue({
user,
allLeagues: [league],
updateUser,
updateUserLeagues,
userLeagues: [],
updateAllLeagues,
updateGameWeek,
updateEntries,
});

mockGetAllLeagues.mockResolvedValueOnce([league]);
Expand Down Expand Up @@ -197,6 +234,8 @@ describe('Leagues Component', () => {
participants: [user.id],
survivors: [user.id],
});
expect(updateAllLeagues).toHaveBeenCalledWith([league]);
expect(updateUserLeagues).toHaveBeenCalledWith([league]);
expect(updateUser).toHaveBeenCalledWith(
user.documentId,
user.id,
Expand Down

0 comments on commit d1b7c32

Please sign in to comment.