project-search.test.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import sinon from 'sinon'
  2. import { render, screen, fireEvent } from '@testing-library/react'
  3. import { expect } from 'chai'
  4. import SearchForm from '../../../../../frontend/js/features/project-list/components/search-form'
  5. import * as eventTracking from '../../../../../frontend/js/infrastructure/event-tracking'
  6. import fetchMock from 'fetch-mock'
  7. describe('Project list search form', function () {
  8. beforeEach(function () {
  9. fetchMock.reset()
  10. })
  11. afterEach(function () {
  12. fetchMock.reset()
  13. })
  14. it('renders the search form', function () {
  15. render(<SearchForm inputValue="" setInputValue={() => {}} />)
  16. screen.getByRole('search')
  17. screen.getByRole('textbox', { name: /search projects/i })
  18. })
  19. it('calls clear text when clear button is clicked', function () {
  20. const setInputValueMock = sinon.stub()
  21. render(<SearchForm inputValue="abc" setInputValue={setInputValueMock} />)
  22. const input = screen.getByRole<HTMLInputElement>('textbox', {
  23. name: /search projects/i,
  24. })
  25. expect(input.value).to.equal('abc')
  26. const clearBtn = screen.getByRole('button', { name: 'clear search' })
  27. fireEvent.click(clearBtn)
  28. expect(setInputValueMock).to.be.calledWith('')
  29. })
  30. it('changes text', function () {
  31. const setInputValueMock = sinon.stub()
  32. const sendSpy = sinon.spy(eventTracking, 'send')
  33. render(<SearchForm inputValue="" setInputValue={setInputValueMock} />)
  34. const input = screen.getByRole('textbox', { name: /search projects/i })
  35. const value = 'abc'
  36. fireEvent.change(input, { target: { value } })
  37. expect(sendSpy).to.be.calledOnceWith(
  38. 'project-list-page-interaction',
  39. 'project-search',
  40. 'keydown'
  41. )
  42. expect(setInputValueMock).to.be.calledWith(value)
  43. sendSpy.restore()
  44. })
  45. })