-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed test after adding in new functions
- Loading branch information
1 parent
19a416f
commit d1b7c32
Showing
1 changed file
with
80 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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 />); | ||
|
@@ -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; | ||
|
||
|
@@ -131,6 +158,7 @@ describe('Leagues Component', () => { | |
survivors: ['123456', '78'], | ||
}, | ||
], | ||
userLeagues: [], | ||
}); | ||
|
||
mockGetUserLeagues.mockResolvedValueOnce([]); | ||
|
@@ -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]); | ||
|
@@ -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, | ||
|