-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #363 from SWE574-Fall2023-Group1/feature/362-unit-…
…test-for-tag-search Add unit test for tag search #362
- Loading branch information
Showing
1 changed file
with
54 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,54 @@ | ||
import React from 'react'; | ||
import { render, fireEvent, waitFor, screen } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
import axios from 'axios'; | ||
import TagSearch from './TagSearch'; | ||
|
||
jest.mock('axios'); | ||
|
||
const mockOnTagSelect = jest.fn(); | ||
|
||
describe('TagSearch Component', () => { | ||
beforeEach(() => { | ||
axios.get.mockResolvedValue({ data: { tags: [{ label: 'Sample Tag', id: 'tag1', description: 'Description for Sample Tag' }] } }); | ||
}); | ||
|
||
it('renders without crashing', () => { | ||
render(<TagSearch onTagSelect={mockOnTagSelect} currentTheme="light" />); | ||
}); | ||
|
||
it('handles input changes and suggestions', async () => { | ||
render(<TagSearch onTagSelect={mockOnTagSelect} currentTheme="light" />); | ||
const searchInput = screen.getByLabelText('Search tags'); | ||
fireEvent.change(searchInput, { target: { value: 'Sa' } }); | ||
expect(axios.get).not.toHaveBeenCalled(); | ||
fireEvent.change(searchInput, { target: { value: 'Sample' } }); | ||
await waitFor(() => expect(axios.get).toHaveBeenCalled()); | ||
}); | ||
|
||
it('adds a tag when button is clicked', async () => { | ||
render(<TagSearch onTagSelect={mockOnTagSelect} currentTheme="light" />); | ||
|
||
const searchInput = screen.getByLabelText('Search tags'); | ||
const labelInput = screen.getByLabelText('Label'); | ||
const addButton = screen.getByText('Add Tag'); | ||
|
||
fireEvent.change(searchInput, { target: { value: 'Sample' } }); | ||
await waitFor(() => expect(axios.get).toHaveBeenCalled()); | ||
|
||
const option = await screen.findByText('Sample Tag'); | ||
fireEvent.click(option); | ||
|
||
fireEvent.change(labelInput, { target: { value: 'User Label' } }); | ||
|
||
fireEvent.click(addButton); | ||
|
||
await waitFor(() => expect(mockOnTagSelect).toHaveBeenCalledWith({ | ||
name: 'Sample Tag', | ||
label: 'User Label', | ||
wikidata_id: 'tag1', | ||
description: 'Description for Sample Tag', | ||
id: 'tag1' | ||
})); | ||
}); | ||
}); |