From 4bf86dcce91360c180305bf22a9fff42d4ed1e0c Mon Sep 17 00:00:00 2001 From: JulienIzz Date: Thu, 1 Feb 2024 15:37:08 +0100 Subject: [PATCH] test: testing virtualized list --- .../SpatialNavigationVirtualizedList.test.tsx | 102 ++++++++ ...ialNavigationVirtualizedList.test.tsx.snap | 222 ++++++++++++++++++ .../components/tests/TestButton.tsx | 2 +- 3 files changed, 325 insertions(+), 1 deletion(-) create mode 100644 packages/lib/src/spatial-navigation/components/tests/SpatialNavigationVirtualizedList.test.tsx create mode 100644 packages/lib/src/spatial-navigation/components/tests/SpatialNavigationVirtualizedList.test.tsx.snap diff --git a/packages/lib/src/spatial-navigation/components/tests/SpatialNavigationVirtualizedList.test.tsx b/packages/lib/src/spatial-navigation/components/tests/SpatialNavigationVirtualizedList.test.tsx new file mode 100644 index 00000000..19ec4c9f --- /dev/null +++ b/packages/lib/src/spatial-navigation/components/tests/SpatialNavigationVirtualizedList.test.tsx @@ -0,0 +1,102 @@ +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 { SpatialNavigationVirtualizedList } from '../virtualizedList/SpatialNavigationVirtualizedList'; +import { DefaultFocus } from '../../context/DefaultFocusContext'; +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('SpatialNavigationVirtualizedList', () => { + afterEach(cleanup); + + const renderItem = ({ item }: { item: PropsTestButton & ItemWithIndex }) => ( + + ); + + const data = [ + { title: 'button 1', onSelect: () => undefined, index: 0 }, + { title: 'button 2', onSelect: () => undefined, index: 1 }, + { title: 'button 3', onSelect: () => undefined, index: 2 }, + { title: 'button 4', onSelect: () => undefined, index: 3 }, + { title: 'button 5', onSelect: () => undefined, index: 4 }, + { title: 'button 6', onSelect: () => undefined, index: 5 }, + { title: 'button 7', onSelect: () => undefined, index: 6 }, + { title: 'button 8', onSelect: () => undefined, index: 7 }, + { title: 'button 9', onSelect: () => undefined, index: 8 }, + { title: 'button 10', onSelect: () => undefined, index: 9 }, + ]; + + const renderList = () => + render( + + + + + , + ); + + it('renders the correct number of item', () => { + const component = renderList(); + + expect(screen).toMatchSnapshot(); + + const button1 = screen.getByText('button 1'); + expect(button1).toBeTruthy(); + expectButtonToHaveFocus(component, 'button 1'); + + const button2 = screen.getByText('button 2'); + expect(button2).toBeTruthy(); + + const button3 = screen.getByText('button 3'); + expect(button3).toBeTruthy(); + + const button4 = screen.getByText('button 4'); + expect(button4).toBeTruthy(); + + const button5 = screen.getByText('button 5'); + expect(button5).toBeTruthy(); + + const button6 = screen.queryByText('button 6'); + expect(button6).toBeFalsy(); + }); + + it('handles correctly RIGHT and RENDERS new elements accordingly while deleting elements that are too far from scroll', () => { + const component = renderList(); + + testRemoteControlManager.handleRight(); + expectButtonToHaveFocus(component, 'button 2'); + + expect(screen.getByText('button 1')).toBeTruthy(); + expect(screen.getByText('button 5')).toBeTruthy(); + expect(screen.queryByText('button 6')).toBeFalsy(); + + testRemoteControlManager.handleRight(); + expectButtonToHaveFocus(component, 'button 3'); + + expect(screen.queryByText('button 1')).toBeFalsy(); + expect(screen.getByText('button 2')).toBeTruthy(); + expect(screen.getByText('button 6')).toBeTruthy(); + expect(screen.queryByText('button 7')).toBeFalsy(); + + testRemoteControlManager.handleRight(); + expectButtonToHaveFocus(component, 'button 4'); + + expect(screen.queryByText('button 2')).toBeFalsy(); + expect(screen.getByText('button 3')).toBeTruthy(); + expect(screen.getByText('button 7')).toBeTruthy(); + expect(screen.queryByText('button 8')).toBeFalsy(); + }); +}); diff --git a/packages/lib/src/spatial-navigation/components/tests/SpatialNavigationVirtualizedList.test.tsx.snap b/packages/lib/src/spatial-navigation/components/tests/SpatialNavigationVirtualizedList.test.tsx.snap new file mode 100644 index 00000000..87221e27 --- /dev/null +++ b/packages/lib/src/spatial-navigation/components/tests/SpatialNavigationVirtualizedList.test.tsx.snap @@ -0,0 +1,222 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`SpatialNavigationVirtualizedList renders the correct number of item 1`] = ` + + + + + + button 1 + + + + + + + button 2 + + + + + + + button 3 + + + + + + + button 4 + + + + + + + button 5 + + + + + +`; diff --git a/packages/lib/src/spatial-navigation/components/tests/TestButton.tsx b/packages/lib/src/spatial-navigation/components/tests/TestButton.tsx index 97a1d3d6..88568fd8 100644 --- a/packages/lib/src/spatial-navigation/components/tests/TestButton.tsx +++ b/packages/lib/src/spatial-navigation/components/tests/TestButton.tsx @@ -2,7 +2,7 @@ import styled from '@emotion/native'; import { Text } from 'react-native'; import { SpatialNavigationNode } from '../Node'; -type PropsTestButton = { +export type PropsTestButton = { onSelect: () => void; title: string; };