mirror of
https://github.com/Redocly/redoc.git
synced 2025-03-09 20:35:48 +03:00
feat: add keyboard navigation support to JsonViewer component
This commit is contained in:
parent
3a748022be
commit
f42118f92d
|
@ -41,6 +41,7 @@ const Json = (props: JsonProps) => {
|
|||
<OptionsContext.Consumer>
|
||||
{options => (
|
||||
<PrismDiv
|
||||
tabIndex={0}
|
||||
className={props.className}
|
||||
// tslint:disable-next-line
|
||||
ref={node => setNode(node!)}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
import { mount, ReactWrapper } from 'enzyme';
|
||||
import * as React from 'react';
|
||||
import { act } from 'react';
|
||||
|
||||
import { JsonViewer } from '../';
|
||||
import { withTheme } from '../testProviders';
|
||||
|
@ -50,5 +51,54 @@ describe('Components', () => {
|
|||
expect(flatDataComponent.html()).not.toContain('Expand all');
|
||||
expect(flatDataComponent.html()).not.toContain('Collapse all');
|
||||
});
|
||||
|
||||
describe('Keyboard Navigation', () => {
|
||||
let component: ReactWrapper;
|
||||
const data = {
|
||||
a: 1,
|
||||
b: {
|
||||
c:
|
||||
// Long string to test horizontal scrolling
|
||||
Array(100).fill('hello').join(''),
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
component = mount(withTheme(<JsonViewer data={data} />));
|
||||
ClipboardService.copySelected = origCopySelected;
|
||||
});
|
||||
|
||||
test('should handle arrow key navigation', () => {
|
||||
const prismDiv = component.find('div[tabIndex=0]');
|
||||
const divElement = prismDiv.getDOMNode();
|
||||
|
||||
// Mock scrollLeft before events
|
||||
Object.defineProperty(divElement, 'scrollLeft', {
|
||||
get: jest.fn(() => 0),
|
||||
set: jest.fn(),
|
||||
});
|
||||
|
||||
// Trigger events inside act()
|
||||
act(() => {
|
||||
divElement.dispatchEvent(
|
||||
new KeyboardEvent('keydown', {
|
||||
key: 'ArrowRight',
|
||||
bubbles: true,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
act(() => {
|
||||
divElement.dispatchEvent(
|
||||
new KeyboardEvent('keydown', {
|
||||
key: 'ArrowLeft',
|
||||
bubbles: true,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
expect(divElement.scrollLeft).toBe(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user