Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated unit tests from #477 PR #538

Merged
merged 29 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
49f2d25
updated league tests
vmaineng Sep 14, 2024
2e3d089
modified a lot of tests to be formatted similar to league entry
vmaineng Sep 15, 2024
b49472b
updated utils
vmaineng Sep 15, 2024
826699b
updated mockApiFunctions
vmaineng Sep 15, 2024
8f0eae3
updated apiFunctions file
vmaineng Sep 15, 2024
abfca63
added in tests updates
vmaineng Sep 15, 2024
ab549dc
updated tests
vmaineng Sep 16, 2024
1835a0d
added in spinner to not be in document
vmaineng Sep 17, 2024
e7c2582
added in form submission
vmaineng Sep 17, 2024
513ae38
added in changes to test
vmaineng Sep 20, 2024
72c3d6d
Merge branch 'develop' into mai/477-add-tests
vmaineng Sep 20, 2024
a17add0
added in changes from develop
vmaineng Sep 20, 2024
21ec1f9
pulled in changes for apifunctions.test
vmaineng Sep 20, 2024
aa9a0d3
updated tests
vmaineng Sep 20, 2024
2813481
added in updates per chris comment
vmaineng Sep 20, 2024
5e714ea
added in test that passed
vmaineng Sep 21, 2024
4691022
added in changes for submission
vmaineng Sep 21, 2024
ad0830c
added in tests for league page
vmaineng Sep 21, 2024
8bbbf98
Merge branch 'develop' into mai/477-add-tests
vmaineng Sep 24, 2024
b3063aa
Merge branch 'develop' of github.com:LetsGetTechnical/gridiron-surviv…
vmaineng Sep 24, 2024
5cfa162
added in specific unit tests
vmaineng Sep 24, 2024
a6a1e4b
Merge branch 'develop' into mai/477-add-tests
vmaineng Sep 26, 2024
904fdee
added in updates to test the results
vmaineng Sep 26, 2024
b625d56
modified the rest of the formats
vmaineng Sep 26, 2024
c588465
Merge branch 'develop' into mai/477-add-tests
vmaineng Oct 10, 2024
784998c
updated test to it and reverted utils file
vmaineng Oct 11, 2024
630f584
added in packages
vmaineng Oct 11, 2024
aa84260
added a space
vmaineng Oct 11, 2024
dd36ddb
reverted utils
vmaineng Oct 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
277 changes: 215 additions & 62 deletions app/(main)/league/all/page.test.tsx
Original file line number Diff line number Diff line change
@@ -1,124 +1,277 @@
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
import {
render,
screen,
waitFor,
waitForElementToBeRemoved,
fireEvent,
} from '@testing-library/react';
import Leagues from './page';
import { useDataStore } from '@/store/dataStore';
import { getUserLeagues } from '@/utils/utils';
import {
getGameWeek,
getAllLeagues,
getUserDocumentId,
} from '@/api/apiFunctions';
import { AuthContext } from '@/context/AuthContextProvider';
import { getAllLeagues, addUserToLeague } from '@/api/apiFunctions';
import { toast } from 'react-hot-toast';
import Alert from '@/components/AlertNotification/AlertNotification';
import { AlertVariants } from '@/components/AlertNotification/Alerts.enum';

jest.mock('@/store/dataStore', () => ({
useDataStore: jest.fn(() => ({ user: { id: '123', leagues: [] } })),
}));
const mockUseAuthContext = {
isSignedIn: false,
};

jest.mock('@/utils/utils', () => ({
getUserLeagues: jest.fn(() => Promise.resolve([])),
jest.mock('@/context/AuthContextProvider', () => ({
useAuthContext() {
return {
...mockUseAuthContext,
};
},
}));

jest.mock('@/api/apiFunctions', () => ({
getGameWeek: jest.fn(() =>
Promise.resolve({
week: 1,
}),
),
getAllLeagues: jest.fn(() =>
Promise.resolve([
jest.mock('@/store/dataStore', () => ({
useDataStore: jest.fn(() => ({
user: {
documentId: '123',
id: '1234',
email: '[email protected]',
leagues: ['league1'],
},
allLeagues: [
{
leagueId: '123',
leagueName: 'Test League',
logo: 'https://example.com/logo.png',
participants: ['123456', '78', '9'],
logo: 'logo.png',
participants: ['123456', '78'],
survivors: ['123456', '78'],
},
]),
),
],
updateUser: jest.fn(),
})),
}));

jest.mock('@/context/AuthContextProvider', () => ({
useAuthContext: jest.fn(() => ({ user: { id: '123' } })),
jest.mock('@/utils/utils', () => ({
getUserLeagues: jest.fn(() => Promise.resolve([])),
cn: jest.fn(),
}));

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

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

describe('Leagues Component', () => {
const mockUseDataStore = useDataStore as unknown as jest.Mock;
const mockGetUserLeagues = getUserLeagues as jest.Mock;
const mockGetGameWeek = getGameWeek as jest.Mock;
const mockGetAllLeagues = getAllLeagues as jest.Mock;
const mockAddUserToLeague = addUserToLeague as jest.Mock;

beforeEach(() => {
jest.clearAllMocks();
});

xtest('should render "You are not enrolled in any leagues" message when no leagues are found', async () => {
mockUseDataStore.mockReturnValueOnce({ user: { id: '123', leagues: [] } });
mockGetUserLeagues.mockResolvedValueOnce([]);
mockGetGameWeek.mockResolvedValueOnce({ week: 1 });
it('should render "You are not enrolled in any leagues" message when no leagues are found', async () => {
mockUseAuthContext.isSignedIn = true;
mockUseDataStore.mockReturnValue({
user: {
documentId: '123',
email: '[email protected]',
id: '123',
leagues: [],
},
allLeagues: [],
});

render(<Leagues />);

await waitForElementToBeRemoved(() => screen.getByTestId('global-spinner'));

await waitFor(() => {
expect(
screen.getByText('You are not enrolled in any leagues'),
).toBeInTheDocument();
const messageElement = screen.getByTestId('no-leagues-message');
expect(messageElement).toBeInTheDocument();
});
});

xtest('should display GlobalSpinner while loading data', async () => {
mockUseDataStore.mockReturnValueOnce({ user: { id: '123', leagues: [] } });
mockGetUserLeagues.mockResolvedValueOnce([]);
mockGetGameWeek.mockResolvedValueOnce({ week: 1 });
render(<Leagues />);
it('should display GlobalSpinner while loading data', async () => {
mockUseAuthContext.isSignedIn = true;

await waitFor(() => {
expect(screen.getByTestId('global-spinner')).toBeInTheDocument();
mockUseDataStore.mockReturnValueOnce({
user: {
documentId: '123',
email: '[email protected]',
id: '123',
leagues: [],
},
allLeagues: [],
});
});
xtest('should not display GlobalSpinner after loading data', async () => {
mockUseDataStore.mockReturnValueOnce({ user: { id: '123', leagues: [] } });
mockGetUserLeagues.mockResolvedValueOnce([]);
mockGetGameWeek.mockResolvedValueOnce({ week: 1 });

render(<Leagues />);

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

await waitFor(() => {
expect(screen.queryByTestId('global-spinner')).not.toBeInTheDocument();
});
});

xtest('should handle form submission to join a league', async () => {
mockUseDataStore.mockReturnValueOnce({
user: { id: '123', leagues: [] },
it('should not display GlobalSpinner after loading data', async () => {
mockUseAuthContext.isSignedIn = true;

mockUseDataStore.mockReturnValue({
user: {
documentId: '123',
email: '[email protected]',
id: '123',
leagues: [],
},
allLeagues: [
{
leagueId: '123',
leagueName: 'Test League',
logo: 'https://findmylogo.com/logo.png',
logo: 'logo.png',
participants: ['123456', '78'],
survivors: ['123456', '78', '9'],
survivors: ['123456', '78'],
},
],
});

mockGetUserLeagues.mockResolvedValueOnce([]);
mockGetGameWeek.mockResolvedValueOnce({ week: 1 });

render(<Leagues />);

await waitForElementToBeRemoved(() => screen.getByTestId('global-spinner'));

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

it('should handle form submission to join a league', async () => {
mockUseAuthContext.isSignedIn = true;

const user = {
documentId: '123',
email: '[email protected]',
id: '123',
leagues: [],
};

const league = {
leagueId: '123',
leagueName: 'Test League',
logo: 'logo.png',
participants: [],
survivors: [],
};

const updateUser = jest.fn();

mockUseDataStore.mockReturnValue({
user,
allLeagues: [league],
updateUser,
});

mockGetAllLeagues.mockResolvedValueOnce([league]);
mockAddUserToLeague.mockResolvedValue(
Promise.resolve({
userDocumentId: user.documentId,
selectedLeague: league.leagueId,
selectedLeagues: [league.leagueId],
participants: [user.id],
survivors: [user.id],
}),
);

render(<Leagues />);

await waitFor(() => {
expect(screen.queryByTestId('global-spinner')).not.toBeInTheDocument();
});

const selectElement = screen.getByLabelText(/Select league to join/i);
expect(selectElement).toBeInTheDocument();
const selectElement = screen.getByTestId('select-available-leagues');
fireEvent.change(selectElement, { target: { value: '123' } });
fireEvent.click(screen.getByTestId('join-league-button'));

await waitFor(() => {
expect(mockAddUserToLeague).toHaveBeenCalledWith({
userDocumentId: user.documentId,
selectedLeague: league.leagueId,
selectedLeagues: [league.leagueId],
participants: [user.id],
survivors: [user.id],
});
expect(updateUser).toHaveBeenCalledWith(
user.documentId,
user.id,
user.email,
[...user.leagues, league.leagueId],
);
expect(toast.custom).toHaveBeenCalledWith(
<Alert
variant={AlertVariants.Success}
message={`Added ${league.leagueName} to your leagues!`}
/>,
);
});
});

it('should show error if adding to league fails', async () => {
mockUseAuthContext.isSignedIn = true;

const user = {
documentId: '123',
email: '[email protected]',
id: '123',
leagues: [],
};

const league = {
leagueId: '123',
leagueName: 'Test League',
logo: 'logo.png',
participants: [],
survivors: [],
};

mockUseDataStore.mockReturnValue({
user,
allLeagues: [league],
});

mockGetUserLeagues.mockResolvedValueOnce([]);
mockGetAllLeagues.mockResolvedValueOnce([league]);
mockAddUserToLeague.mockResolvedValue(
Promise.resolve({
userDocumentId: user.documentId,
selectedLeague: league.leagueId,
selectedLeagues: [league.leagueId],
participants: [user.id],
survivors: [user.id],
}),
);

render(<Leagues />);

await waitFor(() => {
expect(screen.queryByTestId('global-spinner')).not.toBeInTheDocument();
});

const selectElement = screen.getByTestId('select-available-leagues');
fireEvent.change(selectElement, { target: { value: '123' } });
fireEvent.click(screen.getByText(/Join League/i));
fireEvent.click(screen.getByTestId('join-league-button'));

await waitFor(() => {
expect(
screen.getByText('Added Test League to your leagues!'),
).toBeInTheDocument();
expect(mockAddUserToLeague).toHaveBeenCalledWith({
userDocumentId: user.documentId,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment:

  userDocumentId: user.documentId,
        selectedLeague: league.leagueId,
        selectedLeagues: [league.leagueId],
        participants: [user.id],
        survivors: [user.id],

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why I set up this code like this is to match the code in league/page.tsx file. What are your thoughts on the different formats?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vmaineng it's not about different formats. It's about what they are doing.

In page.tsx, the code handles adding a new participant/survivor as follows:

  • It spreads the existing participants/survivors (if any).
  • It then adds the new participant/survivor to this spread array.
  • If there's no existing history of participants/survivors, it starts with an empty array before adding the new user.

The test is verifying that the function is called with the correct data you're passing into it. By having it the same way as page.tsx you are duplicating code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated :)

selectedLeague: league.leagueId,
selectedLeagues: [league.leagueId],
participants: [user.id],
survivors: [user.id],
});

expect(toast.custom).toHaveBeenCalledWith(
<Alert
variant={AlertVariants.Error}
message="Failed to add the league. Please try again."
/>,
);
});
});
});
13 changes: 9 additions & 4 deletions app/(main)/league/all/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,12 @@ const Leagues = (): JSX.Element => {
return (
<div className="Leagues mx-auto max-w-3xl pt-10">
chris-nowicki marked this conversation as resolved.
Show resolved Hide resolved
{loadingData ? (
<GlobalSpinner />
<GlobalSpinner data-testid="global-spinner" />
) : (
<>
<h1 className="pb-10 text-center text-3xl font-bold tracking-tight">
Your Leagues
</h1>

<section className="grid gap-6 md:grid-cols-2 mb-10">
{leagues.length > 0 ? (
leagues.map((league) => (
Expand All @@ -145,7 +144,10 @@ const Leagues = (): JSX.Element => {
))
) : (
<div className="text-center">
<p className="text-lg font-bold">
<p
className="text-lg font-bold"
data-testid="no-leagues-message"
>
You are not enrolled in any leagues
</p>
</div>
Expand All @@ -169,6 +171,7 @@ const Leagues = (): JSX.Element => {
render={({ field, fieldState }) => (
<>
<select
data-testid="select-available-leagues"
{...field}
id="available-leagues"
className={`border border-border rounded p-2 w-full text-secondary ${
Expand Down Expand Up @@ -198,7 +201,9 @@ const Leagues = (): JSX.Element => {
)}
/>
</div>
<Button type="submit">Join League</Button>
<Button type="submit" data-testid="join-league-button">
Join League
</Button>
</form>
</>
)}
Expand Down
Loading