symbol-palette.test.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { expect } from 'chai'
  2. import sinon from 'sinon'
  3. import { screen, render, fireEvent, waitFor } from '@testing-library/react'
  4. import SymbolPalette from '../../../../../frontend/js/features/symbol-palette/components/symbol-palette'
  5. describe('symbol palette', function () {
  6. let clock
  7. before(function () {
  8. clock = sinon.useFakeTimers({
  9. toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval'],
  10. })
  11. })
  12. after(function () {
  13. clock.runAll()
  14. clock.restore()
  15. })
  16. it('handles keyboard interaction', async function () {
  17. this.timeout(10000)
  18. const handleSelect = sinon.stub()
  19. const { container } = render(
  20. <SymbolPalette show handleSelect={handleSelect} />
  21. )
  22. // check the number of tabs
  23. const tabs = await screen.findAllByRole('tab')
  24. expect(tabs).to.have.length(5)
  25. let selectedTab
  26. let symbols
  27. // the first tab should be selected
  28. selectedTab = await screen.getByRole('tab', { selected: true })
  29. expect(selectedTab.textContent).to.equal('Greek')
  30. symbols = await screen.findAllByRole('option')
  31. expect(symbols).to.have.length(39)
  32. // click to select the third tab
  33. tabs[2].click()
  34. selectedTab = await screen.getByRole('tab', { selected: true })
  35. expect(selectedTab.textContent).to.equal('Operators')
  36. symbols = await screen.findAllByRole('option')
  37. expect(symbols).to.have.length(20)
  38. // press the left arrow to select the second tab
  39. fireEvent.keyDown(selectedTab, { key: 'ArrowLeft' })
  40. selectedTab = await screen.getByRole('tab', { selected: true })
  41. expect(selectedTab.textContent).to.equal('Arrows')
  42. symbols = await screen.findAllByRole('option')
  43. expect(symbols).to.have.length(16)
  44. // select the search input
  45. const input = await screen.getByRole('searchbox')
  46. input.click()
  47. // type in the search input
  48. fireEvent.change(input, { target: { value: 'pi' } })
  49. // make sure all scheduled microtasks have executed
  50. clock.runAll()
  51. // wait for the symbols to be filtered
  52. await waitFor(async () => {
  53. symbols = await screen.findAllByRole('option')
  54. expect(symbols).to.have.length(2)
  55. })
  56. // check the search hint is displayed
  57. screen.getByText('Showing search results for "pi"')
  58. // press Tab to select the symbols
  59. fireEvent.keyDown(container, { key: 'Tab' })
  60. // get the selected symbol
  61. let selectedSymbol
  62. selectedSymbol = await screen.getByRole('option', { selected: true })
  63. expect(selectedSymbol.textContent).to.equal('𝜋')
  64. // move to the next symbol
  65. fireEvent.keyDown(selectedSymbol, { key: 'ArrowRight' })
  66. // wait for the symbol to be selected
  67. selectedSymbol = await screen.getByRole('option', { selected: true })
  68. expect(selectedSymbol.textContent).to.equal('Π')
  69. // click on the selected symbol
  70. selectedSymbol.click()
  71. expect(handleSelect).to.have.been.calledWith({
  72. aliases: ['Π'],
  73. category: 'Greek',
  74. character: 'Π',
  75. codepoint: 'U+003A0',
  76. command: '\\Pi',
  77. description: 'Uppercase Greek letter Pi',
  78. notes: 'Use \\prod for the product.',
  79. })
  80. })
  81. })