Skip to content

Commit

Permalink
feat(list): test for deregistering callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Coread committed Feb 4, 2025
1 parent ac7bd63 commit 51adeed
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/components/List/List.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { RefObject, useImperativeHandle, useRef, forwardRef, useMemo } from 'react';
import React, { RefObject, useImperativeHandle, useRef, forwardRef } from 'react';
import classnames from 'classnames';

import { DEFAULTS, STYLE } from './List.constants';
Expand Down
34 changes: 34 additions & 0 deletions src/components/ListItemBase/ListItemBase.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,40 @@ describe('ListItemBase', () => {
expect(getByTestId('list-item-1')).toHaveFocus();
});

it('if an item index changes for a particular item, the old item should not be remembered', async () => {
const { getByTestId, rerender } = render(
<List shouldFocusOnPress listSize={2}>
<ListItemBase key={'base0'} data-testid="list-item-1" itemIndex={0}>
<ButtonPill>1</ButtonPill>
</ListItemBase>
<ListItemBase key={'base1'} data-testid="list-item-2" itemIndex={1}>
<ButtonPill>2</ButtonPill>
</ListItemBase>
</List>
);

simulateOnPress(getByTestId, 'list-item-2');

expect(getByTestId('list-item-2')).toHaveFocus();

rerender(
<List shouldFocusOnPress listSize={2}>
<ListItemBase key={'base0'} data-testid="list-item-1" itemIndex={3}>
<ButtonPill>1</ButtonPill>
</ListItemBase>
<ListItemBase key={'base1'} data-testid="list-item-2" itemIndex={1}>
<ButtonPill>2</ButtonPill>
</ListItemBase>
</List>
);

// press up arrow key
fireEvent.keyDown(getByTestId('list-item-2'), { key: 'ArrowUp' });

// If the old item index was remembered, the focus would be on list-item-1
expect(getByTestId('list-item-2')).toHaveFocus();
});

it('should not focus on press with focusChild even if shouldFocusOnPress is set', async () => {
const { getByTestId } = render(
<List shouldFocusOnPress listSize={1}>
Expand Down

0 comments on commit 51adeed

Please sign in to comment.