From 37f29a481cddf4cb64445a7d4ddece902932ad4b Mon Sep 17 00:00:00 2001 From: JulienIzz Date: Fri, 2 Feb 2024 10:51:29 +0100 Subject: [PATCH] test: testing virtualized grid --- .../SpatialNavigationVirtualizedGrid.test.tsx | 143 +++ ...ialNavigationVirtualizedGrid.test.tsx.snap | 1133 +++++++++++++++++ 2 files changed, 1276 insertions(+) create mode 100644 packages/lib/src/spatial-navigation/components/tests/SpatialNavigationVirtualizedGrid.test.tsx create mode 100644 packages/lib/src/spatial-navigation/components/tests/SpatialNavigationVirtualizedGrid.test.tsx.snap diff --git a/packages/lib/src/spatial-navigation/components/tests/SpatialNavigationVirtualizedGrid.test.tsx b/packages/lib/src/spatial-navigation/components/tests/SpatialNavigationVirtualizedGrid.test.tsx new file mode 100644 index 00000000..85ae4c24 --- /dev/null +++ b/packages/lib/src/spatial-navigation/components/tests/SpatialNavigationVirtualizedGrid.test.tsx @@ -0,0 +1,143 @@ +import { RenderResult, cleanup, render, screen } from '@testing-library/react-native'; +import { ItemWithIndex } from '../virtualizedList/VirtualizedList'; +import { PropsTestButton, TestButton } from './TestButton'; +import { SpatialNavigationRoot } from '../Root'; +import './helpers/configureTestRemoteControl'; + +import { DefaultFocus } from '../../context/DefaultFocusContext'; +import { SpatialNavigationVirtualizedGrid } from '../virtualizedGrid/SpatialNavigationVirtualizedGrid'; +import testRemoteControlManager from './helpers/testRemoteControlManager'; + +export const expectButtonToHaveFocus = (component: RenderResult, text: string) => { + const element = component.getByRole('button', { name: text }); + expect(element).toHaveAccessibilityState({ selected: true }); +}; + +describe('SpatialNavigationVirtualizedGrid', () => { + afterEach(cleanup); + + const renderItem = ({ item }: { item: PropsTestButton & ItemWithIndex }) => ( + + ); + + function createDataArray(numberOfItems: number) { + const data = []; + for (let i = 0; i < numberOfItems; i++) { + data.push({ + title: `button ${i + 1}`, + onSelect: () => undefined, + index: i, + }); + } + return data; + } + + const renderGrid = () => + render( + + + + + , + ); + + it('renders the correct number of item', () => { + const component = renderGrid(); + + expect(screen).toMatchSnapshot(); + + expect(screen.getByText('button 1')).toBeTruthy(); + expectButtonToHaveFocus(component, 'button 1'); + expect(screen.getByText('button 2')).toBeTruthy(); + expect(screen.getByText('button 3')).toBeTruthy(); + expect(screen.getByText('button 4')).toBeTruthy(); + expect(screen.getByText('button 5')).toBeTruthy(); + expect(screen.getByText('button 6')).toBeTruthy(); + expect(screen.getByText('button 7')).toBeTruthy(); + expect(screen.getByText('button 8')).toBeTruthy(); + expect(screen.getByText('button 9')).toBeTruthy(); + expect(screen.getByText('button 10')).toBeTruthy(); + expect(screen.getByText('button 11')).toBeTruthy(); + expect(screen.getByText('button 12')).toBeTruthy(); + expect(screen.getByText('button 13')).toBeTruthy(); + expect(screen.getByText('button 14')).toBeTruthy(); + expect(screen.getByText('button 15')).toBeTruthy(); + expect(screen.queryByText('button 16')).toBeFalsy(); + }); + + it('renders the correct number of item', () => { + const component = renderGrid(); + + testRemoteControlManager.handleRight(); + + expect(screen.getByText('button 1')).toBeTruthy(); + expect(screen.getByText('button 2')).toBeTruthy(); + expectButtonToHaveFocus(component, 'button 2'); + expect(screen.getByText('button 3')).toBeTruthy(); + expect(screen.getByText('button 4')).toBeTruthy(); + expect(screen.getByText('button 5')).toBeTruthy(); + expect(screen.getByText('button 6')).toBeTruthy(); + expect(screen.getByText('button 7')).toBeTruthy(); + expect(screen.getByText('button 8')).toBeTruthy(); + expect(screen.getByText('button 9')).toBeTruthy(); + expect(screen.getByText('button 10')).toBeTruthy(); + expect(screen.getByText('button 11')).toBeTruthy(); + expect(screen.getByText('button 12')).toBeTruthy(); + expect(screen.getByText('button 13')).toBeTruthy(); + expect(screen.getByText('button 14')).toBeTruthy(); + expect(screen.getByText('button 15')).toBeTruthy(); + expect(screen.queryByText('button 16')).toBeFalsy(); + + testRemoteControlManager.handleDown(); + + expect(screen.getByText('button 1')).toBeTruthy(); + expect(screen.getByText('button 2')).toBeTruthy(); + expect(screen.getByText('button 3')).toBeTruthy(); + expect(screen.getByText('button 4')).toBeTruthy(); + expect(screen.getByText('button 5')).toBeTruthy(); + expectButtonToHaveFocus(component, 'button 5'); + expect(screen.getByText('button 6')).toBeTruthy(); + expect(screen.getByText('button 7')).toBeTruthy(); + expect(screen.getByText('button 8')).toBeTruthy(); + expect(screen.getByText('button 9')).toBeTruthy(); + expect(screen.getByText('button 10')).toBeTruthy(); + expect(screen.getByText('button 11')).toBeTruthy(); + expect(screen.getByText('button 12')).toBeTruthy(); + expect(screen.getByText('button 13')).toBeTruthy(); + expect(screen.getByText('button 14')).toBeTruthy(); + expect(screen.getByText('button 15')).toBeTruthy(); + expect(screen.queryByText('button 16')).toBeFalsy(); + + testRemoteControlManager.handleDown(); + + expect(screen.queryByText('button 1')).toBeFalsy(); + expect(screen.queryByText('button 2')).toBeFalsy(); + expect(screen.queryByText('button 3')).toBeFalsy(); + expect(screen.getByText('button 4')).toBeTruthy(); + expect(screen.getByText('button 5')).toBeTruthy(); + expect(screen.getByText('button 6')).toBeTruthy(); + expect(screen.getByText('button 7')).toBeTruthy(); + expect(screen.getByText('button 8')).toBeTruthy(); + expectButtonToHaveFocus(component, 'button 8'); + expect(screen.getByText('button 9')).toBeTruthy(); + expect(screen.getByText('button 10')).toBeTruthy(); + expect(screen.getByText('button 11')).toBeTruthy(); + expect(screen.getByText('button 12')).toBeTruthy(); + expect(screen.getByText('button 13')).toBeTruthy(); + expect(screen.getByText('button 14')).toBeTruthy(); + expect(screen.getByText('button 15')).toBeTruthy(); + expect(screen.getByText('button 16')).toBeTruthy(); + expect(screen.getByText('button 17')).toBeTruthy(); + expect(screen.getByText('button 18')).toBeTruthy(); + expect(screen.queryByText('button 19')).toBeFalsy(); + + expect(screen).toMatchSnapshot(); + }); +}); diff --git a/packages/lib/src/spatial-navigation/components/tests/SpatialNavigationVirtualizedGrid.test.tsx.snap b/packages/lib/src/spatial-navigation/components/tests/SpatialNavigationVirtualizedGrid.test.tsx.snap new file mode 100644 index 00000000..7d262b45 --- /dev/null +++ b/packages/lib/src/spatial-navigation/components/tests/SpatialNavigationVirtualizedGrid.test.tsx.snap @@ -0,0 +1,1133 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`SpatialNavigationVirtualizedGrid renders the correct number of item 1`] = ` + + + + + + + + button 1 + + + + + + + button 2 + + + + + + + button 3 + + + + + + + + + + + button 4 + + + + + + + button 5 + + + + + + + button 6 + + + + + + + + + + + button 7 + + + + + + + button 8 + + + + + + + button 9 + + + + + + + + + + + button 10 + + + + + + + button 11 + + + + + + + button 12 + + + + + + + + + + + button 13 + + + + + + + button 14 + + + + + + + button 15 + + + + + + + +`; + +exports[`SpatialNavigationVirtualizedGrid renders the correct number of item 2`] = ` + + + + + + + + button 4 + + + + + + + button 5 + + + + + + + button 6 + + + + + + + + + + + button 7 + + + + + + + button 8 + + + + + + + button 9 + + + + + + + + + + + button 10 + + + + + + + button 11 + + + + + + + button 12 + + + + + + + + + + + button 13 + + + + + + + button 14 + + + + + + + button 15 + + + + + + + + + + + button 16 + + + + + + + button 17 + + + + + + + button 18 + + + + + + + +`;