Skip to content

Commit

Permalink
Check weight log date in test
Browse files Browse the repository at this point in the history
  • Loading branch information
pkirilin committed Sep 29, 2024
1 parent ecad028 commit 85ca8fd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/frontend/src/features/logWeight/ui/LogWeightButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const LogWeightButton: FC<Props> = ({ weightLogsRequest }) => {

const onSubmit: SubmitHandler<FormValues> = async ({ weight }) => {
const { error } = await addWeightLog({
date: dateLib.formatToISOStringWithoutTime(new Date()),
date: dateLib.formatToISOStringWithoutTime(dateLib.getCurrentDate()),
value: weight,
});

Expand Down
1 change: 1 addition & 0 deletions src/frontend/src/shared/lib/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export const formatToISOStringWithoutTime = format('yyyy-MM-dd');
export const formatToUserFriendlyString = format('d MMM yyyy');
export const getStartOfMonth = startOfMonth();
export const getEndOfMonth = endOfMonth();
export const getCurrentDate = (): Date => new Date();
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { RootProvider } from '@/app/RootProvider';
import { configureStore } from '@/app/store';
import { dateLib } from '@/shared/lib';
import { WeightLogsList } from './WeightLogsList';

beforeEach(() => {
vi.spyOn(dateLib, 'getCurrentDate').mockReturnValue(new Date('2022-01-30'));
});

afterEach(() => {
vi.restoreAllMocks();
});

test('I can log my current weight', async () => {
const store = configureStore();
const user = userEvent.setup();
Expand All @@ -21,5 +30,5 @@ test('I can log my current weight', async () => {
await user.clear(screen.getByPlaceholderText(/weight/i));
await user.type(screen.getByPlaceholderText(/weight/i), '75');
await user.click(screen.getByRole('button', { name: /save/i }));
expect(await screen.findByText(/75 kg/i)).toBeVisible();
expect(await screen.findByRole('listitem', { name: /75 kg on 30 Jan 2022/i })).toBeVisible();
});
18 changes: 10 additions & 8 deletions src/frontend/src/widgets/WeightLogsList/ui/WeightLogsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ export const WeightLogsList: FC<Props> = ({ weightLogsRequest }) => {
/>
</ListItem>
)}
{weightLogs.map(log => (
<ListItem key={log.date} disableGutters disablePadding>
<ListItemText
primary={`${log.value} kg`}
secondary={dateLib.formatToUserFriendlyString(log.date)}
/>
</ListItem>
))}
{weightLogs
.map(({ value, date }) => ({
weight: value,
date: dateLib.formatToUserFriendlyString(date),
}))
.map(({ weight, date }) => (
<ListItem key={date} disableGutters disablePadding aria-label={`${weight} kg on ${date}`}>
<ListItemText primary={`${weight} kg`} secondary={date} />
</ListItem>
))}
</List>
);
};

0 comments on commit 85ca8fd

Please sign in to comment.