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

Cody/change entry name #657

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
26 changes: 26 additions & 0 deletions api/apiFunctions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
registerAccount,
resetPassword,
resetRecoveredPassword,
updateEntryName,
updateUserEmail,
} from './apiFunctions';
import { IUser } from './apiFunctions.interface';
Expand All @@ -24,6 +25,7 @@ jest.mock('./apiFunctions', () => {
getAllLeagues: jest.fn(),
getUserDocumentId: jest.fn(),
addUserToLeague: jest.fn(),
updateEntryName: jest.fn(),
};
});

Expand Down Expand Up @@ -497,4 +499,28 @@ describe('apiFunctions', () => {
);
});
});

describe('update entry name', () => {
const mockEntryId = 'entry123';
const mockEntryName = 'New Entry Name';
const mockUpdatedEntry = {
$id: mockEntryId,
name: mockEntryName,
user: 'user123',
league: 'league123',
selectedTeams: [],
eliminated: false,
};

it('should successfully updateEntryName', async () => {
apiFunctions.updateEntryName.mockResolvedValue(mockUpdatedEntry);

const result = await apiFunctions.updateEntryName({
entryId: mockEntryId,
entryName: mockEntryName,
});

expect(result).toEqual(mockUpdatedEntry);
});
});
});
29 changes: 29 additions & 0 deletions api/apiFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,35 @@ export async function updateEntry({
}
}

/**
* Updates the name of an entry
* @param {Object} params - The parameters object
* @param {string} params.entryId - The ID of the entry to update
* @param {string} params.entryName - The new name for the entry
* @returns {Models.Document | Error} - The entry object or an error
*/
export async function updateEntryName({
entryId,
entryName,
}: {
entryId: string;
entryName: string;
}): Promise<Models.Document & IEntry> {
try {
return await databases.updateDocument(
appwriteConfig.databaseId,
Collection.ENTRIES,
entryId,
{
name: entryName,
},
);
} catch (error) {
console.error(error);
alexappleget marked this conversation as resolved.
Show resolved Hide resolved
throw new Error('Error updating entry');
alexappleget marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* Retrieves a list of all leagues.
* @returns {Models.Document[]} A list of all available leagues.
Expand Down
109 changes: 106 additions & 3 deletions app/(main)/league/[leagueId]/entry/[entryId]/week/Week.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import React from 'react';
import Week from './Week';
import { createWeeklyPicks, getCurrentUserEntries } from '@/api/apiFunctions';
import {
createWeeklyPicks,
getCurrentUserEntries,
updateEntryName,
} from '@/api/apiFunctions';
import Alert from '@/components/AlertNotification/AlertNotification';
import { AlertVariants } from '@/components/AlertNotification/Alerts.enum';
import { toast } from 'react-hot-toast';
Expand Down Expand Up @@ -55,6 +59,7 @@ jest.mock('@/api/apiFunctions', () => ({
createWeeklyPicks: jest.fn(),
getAllWeeklyPicks: jest.fn(),
getCurrentUserEntries: jest.fn(),
updateEntryName: jest.fn(),
}));

jest.mock('@/utils/utils', () => {
Expand Down Expand Up @@ -119,6 +124,100 @@ const updatedWeeklyPicks = {
},
};

describe('Entry Name Editiing', () => {
beforeEach(() => {
mockUseAuthContext.isSignedIn = true;
(getCurrentUserEntries as jest.Mock).mockResolvedValue([
{
$id: '123',
name: 'Entry 1',
user: '123',
league: '123',
selectedTeams: [],
eliminated: false,
},
]);
});

it('should display the edit button', async () => {
render(
<Week entry={entry} league={league} NFLTeams={NFLTeams} week={week} />,
);

await waitFor(() => {
const editEntryNameButton = screen.getByTestId('edit-entry-name-button');
expect(editEntryNameButton).toBeInTheDocument();
});
});

it('should switch to edit mode when the button is clicked', async () => {
render(
<Week entry={entry} league={league} NFLTeams={NFLTeams} week={week} />,
);

await waitFor(() => {
expect(screen.getByTestId('edit-entry-name-button')).toBeInTheDocument();
});
fireEvent.click(screen.getByTestId('edit-entry-name-button'));

const entryNameInput = screen.getByTestId('entry-name-input');
const cancelButton = screen.getByLabelText('Cancel Editing');
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do not grab elements by text. If the text changes, this test will fail. Instead, add data-testid attribute. This is a unique ID that won't ever change. On top of this, when we add localization, things won't always be in English.

const acceptButton = screen.getByLabelText('Accept Edit');

expect(entryNameInput).toBeInTheDocument();
expect(cancelButton).toBeInTheDocument();
expect(acceptButton).toBeInTheDocument();
});

it('should switch to view mode when the cancel button is clicked', async () => {
render(
<Week entry={entry} league={league} NFLTeams={NFLTeams} week={week} />,
);

await waitFor(() => {
expect(screen.getByTestId('edit-entry-name-button')).toBeInTheDocument();
});

fireEvent.click(screen.getByTestId('edit-entry-name-button'));
fireEvent.click(screen.getByLabelText('Cancel Editing'));

await waitFor(() => {
expect(screen.queryByTestId('entry-name-input')).not.toBeInTheDocument();
});
expect(screen.getByTestId('week__entry-name')).toHaveTextContent('Entry 1');
});

it('should update the entry name when valid name is submitted', async () => {
(updateEntryName as jest.Mock).mockResolvedValue({
name: 'New Entry Name',
});
render(
<Week entry={entry} league={league} NFLTeams={NFLTeams} week={week} />,
);

await waitFor(() => {
const editEntryNameButton = screen.getByTestId('edit-entry-name-button');
expect(editEntryNameButton).toBeInTheDocument();
fireEvent.click(editEntryNameButton);
});

const input = screen.getByTestId('entry-name-input');
fireEvent.change(input, { target: { value: 'New Entry Name' } });
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here. Add a data-testid.


fireEvent.click(screen.getByTestId('save-entry-name-button'));

await waitFor(() => {
expect(screen.getByTestId('week__entry-name')).toHaveTextContent(
'New Entry Name',
);
});
expect(updateEntryName).toHaveBeenCalledWith({
entryId: entry,
entryName: 'New Entry Name',
});
});
});

describe('League Week Picks', () => {
const setUserPick = jest.fn();
const updateWeeklyPicks = jest.fn();
Expand Down Expand Up @@ -186,8 +285,12 @@ describe('League Week Picks', () => {
// Wait for the main content to be displayed
await waitFor(() => {
expect(screen.getByTestId('weekly-picks')).toBeInTheDocument();
expect(screen.getByTestId('week__week-number')).toHaveTextContent('Week 1');
expect(screen.getByTestId('week__entry-name')).toHaveTextContent('Entry 1');
expect(screen.getByTestId('week__week-number')).toHaveTextContent(
'Week 1',
);
expect(screen.getByTestId('week__entry-name')).toHaveTextContent(
'Entry 1',
);
});

expect(screen.queryByTestId('global-spinner')).not.toBeInTheDocument();
Expand Down
Loading