Skip to content

Commit

Permalink
fix search box is clear when click back button
Browse files Browse the repository at this point in the history
  • Loading branch information
aliraza556 committed Mar 6, 2024
1 parent 14319bb commit e91b688
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/pages/people/peopleList/PeopleList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const PeopleList = observer(() => {

function goBack() {
ui.setSelectingPerson(0);
ui.setSearchText('');
history.replace('/p');
}

Expand Down
46 changes: 46 additions & 0 deletions src/pages/people/peopleList/__tests__/PeopleList.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import '@testing-library/jest-dom';
import { fireEvent, render } from '@testing-library/react';
import { createMemoryHistory } from 'history';
import React from 'react';
import { Router } from 'react-router-dom';
import { useStores } from '../../../../store';
import { PeopleList } from '../../peopleList';

jest.mock('store', () => ({
useStores: jest.fn()
}));

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useHistory: () => ({
replace: jest.fn()
})
}));

describe('PeopleList Component', () => {
it('clears the search bar when back arrow button is clicked', async () => {
const mockSetSearchText = jest.fn();
(useStores as jest.Mock).mockReturnValue({
ui: {
searchText: 'John Doe',
setSearchText: mockSetSearchText,
setSelectingPerson: jest.fn(),
setSelectedPerson: jest.fn()
},
main: {
getPeople: jest.fn(),
people: []
}
});

const history = createMemoryHistory();
const { getByText, getByPlaceholderText } = render(
<Router history={history}>
<PeopleList />
</Router>
);
expect(getByPlaceholderText('Search')).toHaveValue('John Doe');
fireEvent.click(getByText('Back'));
expect(mockSetSearchText).toHaveBeenCalledWith('');
});
});

0 comments on commit e91b688

Please sign in to comment.