project-search.test.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. let sendMBSpy: sinon.SinonSpy
  9. beforeEach(function () {
  10. sendMBSpy = sinon.spy(eventTracking, 'sendMB')
  11. fetchMock.reset()
  12. })
  13. afterEach(function () {
  14. fetchMock.reset()
  15. sendMBSpy.restore()
  16. })
  17. it('renders the search form', function () {
  18. render(<SearchForm inputValue="" setInputValue={() => {}} />)
  19. screen.getByRole('search')
  20. screen.getByRole('textbox', { name: /search projects/i })
  21. })
  22. it('calls clear text when clear button is clicked', function () {
  23. const setInputValueMock = sinon.stub()
  24. render(<SearchForm inputValue="abc" setInputValue={setInputValueMock} />)
  25. const input = screen.getByRole<HTMLInputElement>('textbox', {
  26. name: /search projects/i,
  27. })
  28. expect(input.value).to.equal('abc')
  29. const clearBtn = screen.getByRole('button', { name: 'clear search' })
  30. fireEvent.click(clearBtn)
  31. expect(setInputValueMock).to.be.calledWith('')
  32. })
  33. it('changes text', function () {
  34. const setInputValueMock = sinon.stub()
  35. render(<SearchForm inputValue="" setInputValue={setInputValueMock} />)
  36. const input = screen.getByRole('textbox', { name: /search projects/i })
  37. const value = 'abc'
  38. fireEvent.change(input, { target: { value } })
  39. expect(sendMBSpy).to.have.been.calledOnce
  40. expect(sendMBSpy).to.have.been.calledWith('project-list-page-interaction', {
  41. action: 'search',
  42. page: '/',
  43. })
  44. expect(setInputValueMock).to.be.calledWith(value)
  45. })
  46. })