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
27 changes: 26 additions & 1 deletion api/apiFunctions.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { mock } from 'node:test';
import {
recoverPassword,
registerAccount,
resetPassword,
resetRecoveredPassword,
updateEntryName,
updateUserEmail,
} from './apiFunctions';
import { IUser } from './apiFunctions.interface';
Expand All @@ -24,6 +24,7 @@ jest.mock('./apiFunctions', () => {
getAllLeagues: jest.fn(),
getUserDocumentId: jest.fn(),
addUserToLeague: jest.fn(),
updateEntryName: jest.fn(),
};
});

Expand Down Expand Up @@ -497,4 +498,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: 28 additions & 1 deletion api/apiFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ export async function createEntry({
}

/**

* Update an entry
* @param props - The entry data
* @param props.entryId - The entry ID
Expand Down Expand Up @@ -427,6 +426,34 @@ 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) {
throw error;
}
}

/**
* 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.getByTestId('cancel-editing-button');
const acceptButton = screen.getByTestId('save-entry-name-button');

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.getByTestId('cancel-editing-button'));

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