|
2 | 2 |
|
3 | 3 | import { mount, ReactWrapper } from 'enzyme';
|
4 | 4 | import * as React from 'react';
|
| 5 | +import { act } from 'react'; |
5 | 6 |
|
6 | 7 | import { JsonViewer } from '../';
|
7 | 8 | import { withTheme } from '../testProviders';
|
@@ -50,5 +51,54 @@ describe('Components', () => {
|
50 | 51 | expect(flatDataComponent.html()).not.toContain('Expand all');
|
51 | 52 | expect(flatDataComponent.html()).not.toContain('Collapse all');
|
52 | 53 | });
|
| 54 | + |
| 55 | + describe('Keyboard Navigation', () => { |
| 56 | + let component: ReactWrapper; |
| 57 | + const data = { |
| 58 | + a: 1, |
| 59 | + b: { |
| 60 | + c: |
| 61 | + // Long string to test horizontal scrolling |
| 62 | + Array(100).fill('hello').join(''), |
| 63 | + }, |
| 64 | + }; |
| 65 | + |
| 66 | + beforeEach(() => { |
| 67 | + component = mount(withTheme(<JsonViewer data={data} />)); |
| 68 | + ClipboardService.copySelected = origCopySelected; |
| 69 | + }); |
| 70 | + |
| 71 | + test('should handle arrow key navigation', () => { |
| 72 | + const prismDiv = component.find('div[tabIndex=0]'); |
| 73 | + const divElement = prismDiv.getDOMNode(); |
| 74 | + |
| 75 | + // Mock scrollLeft before events |
| 76 | + Object.defineProperty(divElement, 'scrollLeft', { |
| 77 | + get: jest.fn(() => 0), |
| 78 | + set: jest.fn(), |
| 79 | + }); |
| 80 | + |
| 81 | + // Trigger events inside act() |
| 82 | + act(() => { |
| 83 | + divElement.dispatchEvent( |
| 84 | + new KeyboardEvent('keydown', { |
| 85 | + key: 'ArrowRight', |
| 86 | + bubbles: true, |
| 87 | + }), |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + act(() => { |
| 92 | + divElement.dispatchEvent( |
| 93 | + new KeyboardEvent('keydown', { |
| 94 | + key: 'ArrowLeft', |
| 95 | + bubbles: true, |
| 96 | + }), |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + expect(divElement.scrollLeft).toBe(0); |
| 101 | + }); |
| 102 | + }); |
53 | 103 | });
|
54 | 104 | });
|
0 commit comments