useHotkeys.spec.jsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import {reactHooks} from 'sentry-test/reactTestingLibrary';
  2. import {getKeyCode} from 'sentry/utils/getKeyCode';
  3. import {useHotkeys} from 'sentry/utils/useHotkeys';
  4. describe('useHotkeys', function () {
  5. let events = {};
  6. beforeEach(() => {
  7. // Empty our events before each test case
  8. events = {};
  9. // Define the addEventListener method with a Jest mock function
  10. document.addEventListener = jest.fn((event, callback) => {
  11. events[event] = callback;
  12. });
  13. document.removeEventListener = jest.fn(event => {
  14. delete events[event];
  15. });
  16. });
  17. it('simple match', function () {
  18. let didGetCalled = false;
  19. expect(events.keydown).toBeUndefined();
  20. reactHooks.renderHook(() =>
  21. useHotkeys([
  22. {
  23. match: 'ctrl+s',
  24. callback: () => {
  25. didGetCalled = true;
  26. },
  27. },
  28. ])
  29. );
  30. expect(events.keydown).toBeDefined();
  31. expect(didGetCalled).toEqual(false);
  32. events.keydown({keyCode: getKeyCode('s'), ctrlKey: true});
  33. expect(didGetCalled).toEqual(true);
  34. });
  35. it('multi match', function () {
  36. let didGetCalled = false;
  37. expect(events.keydown).toBeUndefined();
  38. reactHooks.renderHook(() =>
  39. useHotkeys([
  40. {
  41. match: ['ctrl+s', 'cmd+m'],
  42. callback: () => {
  43. didGetCalled = true;
  44. },
  45. },
  46. ])
  47. );
  48. expect(events.keydown).toBeDefined();
  49. expect(didGetCalled).toEqual(false);
  50. events.keydown({keyCode: getKeyCode('s'), ctrlKey: true});
  51. expect(didGetCalled).toEqual(true);
  52. didGetCalled = false;
  53. events.keydown({keyCode: getKeyCode('m'), metaKey: true});
  54. expect(didGetCalled).toEqual(true);
  55. });
  56. it('complex match', function () {
  57. let didGetCalled = false;
  58. expect(events.keydown).toBeUndefined();
  59. reactHooks.renderHook(() =>
  60. useHotkeys([
  61. {
  62. match: ['cmd+control+option+shift+x'],
  63. callback: () => {
  64. didGetCalled = true;
  65. },
  66. },
  67. ])
  68. );
  69. expect(events.keydown).toBeDefined();
  70. expect(didGetCalled).toEqual(false);
  71. events.keydown({
  72. keyCode: getKeyCode('x'),
  73. altKey: true,
  74. metaKey: true,
  75. shiftKey: true,
  76. ctrlKey: true,
  77. });
  78. expect(didGetCalled).toEqual(true);
  79. });
  80. it('rerender', function () {
  81. let didGetCalled = false;
  82. expect(events.keydown).toBeUndefined();
  83. const {rerender} = reactHooks.renderHook(
  84. p =>
  85. useHotkeys([
  86. {
  87. match: p.match,
  88. callback: () => {
  89. didGetCalled = true;
  90. },
  91. },
  92. ]),
  93. {
  94. initialProps: {
  95. match: 'ctrl+s',
  96. },
  97. }
  98. );
  99. expect(events.keydown).toBeDefined();
  100. expect(didGetCalled).toEqual(false);
  101. events.keydown({keyCode: getKeyCode('s'), ctrlKey: true});
  102. expect(didGetCalled).toEqual(true);
  103. didGetCalled = false;
  104. rerender({match: 'cmd+m'});
  105. events.keydown({keyCode: getKeyCode('s'), ctrlKey: true});
  106. expect(didGetCalled).toEqual(false);
  107. events.keydown({keyCode: getKeyCode('m'), metaKey: true});
  108. expect(didGetCalled).toEqual(true);
  109. });
  110. });