Skip to content

Commit

Permalink
Upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Inkhermit committed Dec 15, 2024
1 parent 3bc819d commit a25e878
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/components/Filter.songwei-genai.test.jsx
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

View workflow job for this annotation

GitHub Actions / Build and unit test

src/components/Filter.songwei-genai.test.jsx > Filter Component > opens the modal and displays filter tags

Error: Invalid Chai property: toBeInTheDocument ❯ src/components/Filter.songwei-genai.test.jsx:33:22

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

View workflow job for this annotation

GitHub Actions / Build and unit test

src/components/Filter.songwei-genai.test.jsx > Filter Component > updates resource with selected tag

TestingLibraryElementError: Found multiple elements with the text: /Dogs/i Here are the matching elements: Ignored nodes: comments, script, style <span class="MuiChip-label MuiChip-labelMedium css-6od3lo-MuiChip-label" > Dogs </span> Ignored nodes: comments, script, style <span class="MuiChip-label MuiChip-labelMedium css-6od3lo-MuiChip-label" > Small Dogs </span> Ignored nodes: comments, script, style <span class="MuiChip-label MuiChip-labelMedium css-6od3lo-MuiChip-label" > Big Dogs </span> (If this is intentional, then use the `*AllBy*` variant of the query (like `queryAllByText`, `getAllByText`, or `findAllByText`)). Ignored nodes: comments, script, style <body style="padding-right: 1024px; overflow: hidden;" > <div aria-hidden="true" > <button class="MuiButtonBase-root MuiButton-root MuiButton-text MuiButton-textPrimary MuiButton-sizeMedium MuiButton-textSizeMedium MuiButton-colorPrimary MuiButton-root MuiButton-text MuiButton-textPrimary MuiButton-sizeMedium MuiButton-textSizeMedium MuiButton-colorPrimary css-1apkmlw-MuiButtonBase-root-MuiButton-root" tabindex="0" type="button" > Filter <span class="MuiButton-icon MuiButton-endIcon MuiButton-iconSizeMedium css-1gulhci-MuiButton-endIcon" > <svg aria-hidden="true" class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-20bmp1-MuiSvgIcon-root" data-testid="TuneIcon" focusable="false" viewBox="0 0 24 24" > <path d="M3 17v2h6v-2zM3 5v2h10V5zm10 16v-2h8v-2h-8v-2h-2v6zM7 9v2H3v2h4v2h2V9zm14 4v-2H11v2zm-6-4h2V7h4V5h-4V3h-2z" /> </svg> </span> </button> </div> <div aria-describedby="modal-modal-description" aria-labelledby="modal-modal-title" class="MuiModal-root css-79ws1d-MuiModal-root" role="presentation" > <div aria-hidden="true" class="MuiBackdrop-root MuiModal-backdrop css-i9fmh8-MuiBackdrop-root-MuiModal-backdrop" style="opacity: 1; webkit-transition: opacity 225ms cubic-bezier(0.4, 0, 0.2, 1) 0ms; transition: opacity 225ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;" /> <div data-testid="sentinelStart" tabindex="0" /> <div class="MuiBox-root css-lbh2pi" tabindex="-1" > <h2 class="MuiTypography-root MuiTypography-h6 css-8zgow4-MuiTypography-root" id="modal-modal-title" > Tags </h2> <div class="MuiButtonBase-root MuiChip-root MuiChip-filled MuiChip-sizeMedium MuiChip-colorDefault MuiChip-clickable MuiChip-clickableColorDefault MuiChip-filledDefault css-12yid6j-MuiButtonBase-root-MuiChip-root" role="button" tabindex="0" > <span class="MuiChip-label MuiChip-labelMedium css-6od3lo-MuiChip-label" > Dogs </span> </div> <div class="MuiButtonBase-root MuiChip-root MuiChip-filled MuiChip-sizeMedium MuiChip-colorDefault MuiChip-clickable MuiChip-clickableColorDefault MuiChip-filledDefault css-12yid6j-MuiButtonBase-root-MuiChip-root" role="button" tabindex="0" > <span class="MuiChip-label MuiChip-labelMedium css-6od3lo-MuiChip-label" > Cats </span> </div> <div class="MuiButtonBase-root MuiChip-root MuiChip-filled MuiChip-sizeMedium MuiChip-colorDefault MuiChip-clickable MuiChip-clickableColorDefault MuiChip-filledDefault css-12yid6j-MuiButtonBase-root-MuiChip-root" role="button" tabindex="0" > <span class="MuiChip-label MuiChip-labelMedium css-6od3lo-MuiChip-label" > Small Pets </span> </div> <div class="MuiButtonBase-root MuiChip-root MuiChip-filled MuiChip-sizeMedium MuiChip-colorDefault MuiChip-clickable MuiChip-clickableColorDefault MuiChip-filledDefault css-12yid6j-MuiButtonBase-root-MuiChip-root" role="button" tabindex="0" > <span class="MuiChip-label MuiChip
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);
});
});
});
60 changes: 60 additions & 0 deletions src/components/Filter.songwei.test.jsx
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);
});
});
});

4 changes: 4 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ export default defineConfig({
test: {
globals: true,
environment: 'jsdom',
mockReset: true,
coverage: {
reporter: ['text', 'html'],
},
}
});

0 comments on commit a25e878

Please sign in to comment.