-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
111 additions
and
10 deletions.
There are no files selected for viewing
104 changes: 101 additions & 3 deletions
104
src/components/habit/edit-habit/EditHabitDialog.test.tsx
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 |
---|---|---|
@@ -1,7 +1,105 @@ | ||
import EditHabitDialog from './EditHabitDialog'; | ||
jest.mock('@hooks', () => ({ | ||
useTraits: jest | ||
.fn() | ||
.mockReturnValue({ traitsMap: { 1: { slug: 'trait-slug' } } }), | ||
})); | ||
|
||
jest.mock('@context', () => ({ | ||
useHabits: jest.fn().mockReturnValue({ updateHabit: jest.fn() }), | ||
})); | ||
|
||
jest.mock('@supabase/auth-helpers-react', () => ({ | ||
useUser: jest | ||
.fn() | ||
.mockReturnValue({ id: '4c6b7c3b-ec2f-45fb-8c3a-df16f7a4b3aa' }), | ||
})); | ||
|
||
import { useHabits } from '@context'; | ||
import { act, fireEvent, render, waitFor } from '@testing-library/react'; | ||
import React from 'react'; | ||
|
||
import EditHabitDialog, { type EditHabitDialogProps } from './EditHabitDialog'; | ||
|
||
describe(EditHabitDialog.name, () => { | ||
it('should be tested', () => { | ||
expect(true).toBeTruthy(); | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
const props: EditHabitDialogProps = { | ||
open: true, | ||
onClose: jest.fn(), | ||
habit: { | ||
id: 7, | ||
createdAt: '2024-02-21T17:30:08.886124+00:00', | ||
updatedAt: null, | ||
userId: '4c6b7c3b-ec2f-45fb-8c3a-df16f7a4b3aa', | ||
name: 'Habit Name', | ||
description: 'Habit description', | ||
traitId: 1, | ||
iconPath: '', | ||
}, | ||
}; | ||
|
||
it('should set values from props', () => { | ||
const { getByRole, getByLabelText } = render( | ||
<EditHabitDialog {...props} /> | ||
); | ||
expect(getByRole('dialog')).toBeDefined(); | ||
expect(getByLabelText('Name')).toHaveProperty('value', 'Habit Name'); | ||
expect(getByLabelText('Description (optional)')).toHaveProperty( | ||
'value', | ||
'Habit description' | ||
); | ||
}); | ||
|
||
it('should not render if habit is not provided', () => { | ||
const { queryByRole } = render(<EditHabitDialog {...props} habit={null} />); | ||
expect(queryByRole('dialog')).toBeNull(); | ||
}); | ||
|
||
it('should not render if open is false', () => { | ||
const { queryByRole } = render(<EditHabitDialog {...props} open={false} />); | ||
expect(queryByRole('dialog')).toBeNull(); | ||
}); | ||
|
||
it('should call onClose when closed', async () => { | ||
const onClose = jest.fn(); | ||
const { getByTestId } = render( | ||
<EditHabitDialog {...props} onClose={onClose} /> | ||
); | ||
const closeIcon = getByTestId('close-icon'); | ||
fireEvent.click(closeIcon); | ||
expect(onClose).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should call updateHabit when submitted', async () => { | ||
const mockUpdateHabit = jest.fn(); | ||
(useHabits as jest.Mock).mockReturnValue({ updateHabit: mockUpdateHabit }); | ||
const { getByRole, getByLabelText } = render( | ||
<EditHabitDialog {...props} /> | ||
); | ||
const nameInput = getByLabelText('Name'); | ||
const descriptionInput = getByLabelText('Description (optional)'); | ||
const form = getByRole('form'); | ||
const updatedAt = new Date(); | ||
updatedAt.setMilliseconds(0); | ||
act(() => { | ||
fireEvent.change(nameInput, { target: { value: 'New Habit Name' } }); | ||
fireEvent.change(descriptionInput, { | ||
target: { value: 'New habit description' }, | ||
}); | ||
fireEvent.submit(form); | ||
}); | ||
await waitFor(() => | ||
expect(mockUpdateHabit).toHaveBeenCalledWith(7, { | ||
createdAt: '2024-02-21T17:30:08.886124+00:00', | ||
updatedAt: updatedAt.toISOString(), | ||
userId: '4c6b7c3b-ec2f-45fb-8c3a-df16f7a4b3aa', | ||
name: 'New Habit Name', | ||
description: 'New habit description', | ||
traitId: 1, | ||
iconPath: '', | ||
}) | ||
); | ||
}); | ||
}); |
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
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
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