-
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
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { render, screen, fireEvent, waitFor } from '@testing-library/react'; | ||
import { describe, it, expect, vi } from 'vitest'; | ||
import Filter from './Filter'; | ||
import { useAuthState } from '../firebase'; | ||
import { filterData } from '../utils'; | ||
|
||
vi.mock('../firebase', () => ({ | ||
useAuthState: vi.fn(), | ||
})); | ||
|
||
vi.mock('../utils', () => ({ | ||
filterData: vi.fn(), | ||
})); | ||
|
||
describe('Filter Component', () => { | ||
const mockUser = { uid: '12345' }; | ||
const mockSetResource = vi.fn(); | ||
const mockResource = [{ key: 'test_walker_1', value: { name: 'Test Walker' } }]; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
useAuthState.mockReturnValue({ user: mockUser }); | ||
filterData.mockResolvedValue(mockResource); | ||
}); | ||
|
||
it('opens the modal and displays filter tags', () => { | ||
render(<Filter resource={mockResource} setResource={mockSetResource} />); | ||
|
||
const filterButton = screen.getByText(/Filter/i); | ||
fireEvent.click(filterButton); // Open modal | ||
|
||
const modalTitle = screen.getByText(/Tags/i); | ||
expect(modalTitle).toBeInTheDocument(); | ||
Check failure on line 33 in src/components/Filter.songwei-genai.test.jsx GitHub Actions / Build and unit testsrc/components/Filter.songwei-genai.test.jsx > Filter Component > opens the modal and displays filter tags
|
||
|
||
const dogsTag = screen.getByText(/Dogs/i); | ||
const catsTag = screen.getByText(/Cats/i); | ||
expect(dogsTag).toBeInTheDocument(); | ||
expect(catsTag).toBeInTheDocument(); | ||
}); | ||
|
||
it('updates resource with selected tag', async () => { | ||
render(<Filter resource={mockResource} setResource={mockSetResource} />); | ||
|
||
fireEvent.click(screen.getByText(/Filter/i)); // Open modal | ||
|
||
const dogsTag = screen.getByText(/Dogs/i); | ||
Check failure on line 46 in src/components/Filter.songwei-genai.test.jsx GitHub Actions / Build and unit testsrc/components/Filter.songwei-genai.test.jsx > Filter Component > updates resource with selected tag
|
||
fireEvent.click(dogsTag); // Select 'Dogs' tag | ||
|
||
const doneButton = screen.getByText(/Done/i); | ||
fireEvent.click(doneButton); // Close modal and apply filter | ||
|
||
await waitFor(() => { | ||
expect(filterData).toHaveBeenCalledWith( | ||
mockResource, | ||
mockUser, | ||
expect.arrayContaining([{ key: 'Dogs', value: true }]) | ||
); | ||
expect(mockSetResource).toHaveBeenCalledWith(mockResource); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { render, screen, fireEvent, waitFor } from '@testing-library/react'; | ||
import { describe, it, expect, vi, beforeEach } from 'vitest'; | ||
import Filter from './Filter'; | ||
import { useAuthState } from '../firebase'; | ||
import { filterData } from '../utils'; | ||
|
||
vi.mock('../firebase', () => ({ | ||
useAuthState: vi.fn(), | ||
})); | ||
|
||
vi.mock('../utils', () => ({ | ||
filterData: vi.fn(), | ||
})); | ||
|
||
describe('Filter Component', () => { | ||
const mockUser = { uid: '12345' }; | ||
const mockSetResource = vi.fn(); | ||
const mockResource = [{ key: 'test_walker_1', value: { name: 'Test Walker' } }]; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
useAuthState.mockReturnValue({ user: mockUser }); | ||
filterData.mockResolvedValue(mockResource); | ||
}); | ||
|
||
it('opens the modal and displays filter tags', () => { | ||
render(<Filter resource={mockResource} setResource={mockSetResource} />); | ||
|
||
fireEvent.click(screen.getByText(/Filter/i)); | ||
|
||
screen.getByText(/Tags/i); | ||
|
||
|
||
screen.getByText(/^Dogs$/); | ||
screen.getByText(/^Cats$/); | ||
screen.getByText(/^Small Pets$/); | ||
}); | ||
|
||
|
||
it('updates resource when the "Dogs" tag is selected and Done is clicked', async () => { | ||
render(<Filter resource={mockResource} setResource={mockSetResource} />); | ||
|
||
fireEvent.click(screen.getByText(/Filter/i)); | ||
|
||
const dogsTag = screen.getAllByRole('button', { name: /^Dogs$/ })[0]; | ||
fireEvent.click(dogsTag); | ||
|
||
fireEvent.click(screen.getByText(/Done/i)); | ||
|
||
await waitFor(() => { | ||
expect(filterData).toHaveBeenCalledWith( | ||
mockResource, | ||
mockUser, | ||
expect.arrayContaining([{ key: 'Dogs', value: true }]) | ||
); | ||
expect(mockSetResource).toHaveBeenCalledWith(mockResource); | ||
}); | ||
}); | ||
}); | ||
|
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