Skip to content

Commit 1d660cc

Browse files
committed
test(react-tags): tighten regression test assertions and drop narration comments
1 parent 042f39b commit 1d660cc

5 files changed

Lines changed: 38 additions & 43 deletions

File tree

packages/react-components/react-tags/library/src/components/InteractionTag/useInteractionTag.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ describe('useInteractionTagBase_unstable', () => {
5555
const ref = React.createRef<HTMLDivElement>();
5656
const { result } = renderHook(() => useInteractionTagBase_unstable({}, ref), { wrapper: wrap() });
5757

58-
expect((result.current as unknown as { appearance?: unknown }).appearance).toBeUndefined();
59-
expect((result.current as unknown as { shape?: unknown }).shape).toBeUndefined();
60-
expect((result.current as unknown as { size?: unknown }).size).toBeUndefined();
58+
expect(result.current).not.toHaveProperty('appearance');
59+
expect(result.current).not.toHaveProperty('shape');
60+
expect(result.current).not.toHaveProperty('size');
6161
});
6262

6363
it('should force disabled when TagGroupContext.disabled is true regardless of props', () => {

packages/react-components/react-tags/library/src/components/InteractionTagPrimary/useInteractionTagPrimary.test.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ import { renderHook } from '@testing-library/react-hooks';
22
import * as React from 'react';
33

44
import { InteractionTagContextProvider } from '../../contexts/interactionTagContext';
5+
import type { InteractionTagContextValue } from '../../contexts/interactionTagContext';
56
import { useInteractionTagPrimary_unstable, useInteractionTagPrimaryBase_unstable } from './useInteractionTagPrimary';
67

7-
const baseContext = {
8-
appearance: 'filled' as const,
8+
const baseContext: InteractionTagContextValue = {
9+
appearance: 'filled',
910
disabled: false,
1011
handleTagDismiss: () => ({}),
1112
interactionTagPrimaryId: 'fui-InteractionTagPrimary-_test_',
1213
selected: false,
1314
selectedValues: [],
14-
shape: 'rounded' as const,
15-
size: 'medium' as const,
15+
shape: 'rounded',
16+
size: 'medium',
1617
value: 'test',
1718
};
1819

@@ -51,11 +52,11 @@ describe('useInteractionTagPrimaryBase_unstable', () => {
5152
const ref = React.createRef<HTMLButtonElement>();
5253
const { result } = renderHook(() => useInteractionTagPrimaryBase_unstable({}, ref), { wrapper: wrap() });
5354

54-
expect((result.current as unknown as { appearance?: unknown }).appearance).toBeUndefined();
55-
expect((result.current as unknown as { shape?: unknown }).shape).toBeUndefined();
56-
expect((result.current as unknown as { size?: unknown }).size).toBeUndefined();
57-
expect((result.current as unknown as { avatarShape?: unknown }).avatarShape).toBeUndefined();
58-
expect((result.current as unknown as { avatarSize?: unknown }).avatarSize).toBeUndefined();
55+
expect(result.current).not.toHaveProperty('appearance');
56+
expect(result.current).not.toHaveProperty('shape');
57+
expect(result.current).not.toHaveProperty('size');
58+
expect(result.current).not.toHaveProperty('avatarShape');
59+
expect(result.current).not.toHaveProperty('avatarSize');
5960
});
6061

6162
it('should set aria-pressed when context has handleTagSelect (selectable group)', () => {
@@ -71,7 +72,7 @@ describe('useInteractionTagPrimaryBase_unstable', () => {
7172
const { result } = renderHook(() => useInteractionTagPrimaryBase_unstable({}, ref), {
7273
wrapper: wrap({ selected: true, handleTagSelect: undefined }),
7374
});
74-
expect(result.current.root['aria-pressed']).toBeUndefined();
75+
expect(result.current.root).not.toHaveProperty('aria-pressed');
7576
});
7677

7778
it('should default hasSecondaryAction to false', () => {

packages/react-components/react-tags/library/src/components/InteractionTagSecondary/useInteractionTagSecondary.test.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ import { renderHook } from '@testing-library/react-hooks';
22
import * as React from 'react';
33

44
import { InteractionTagContextProvider } from '../../contexts/interactionTagContext';
5+
import type { InteractionTagContextValue } from '../../contexts/interactionTagContext';
56
import {
67
useInteractionTagSecondary_unstable,
78
useInteractionTagSecondaryBase_unstable,
89
} from './useInteractionTagSecondary';
910

10-
const baseContext = {
11-
appearance: 'filled' as const,
11+
const baseContext: InteractionTagContextValue = {
12+
appearance: 'filled',
1213
disabled: false,
1314
handleTagDismiss: () => ({}),
1415
interactionTagPrimaryId: 'fui-InteractionTagPrimary-_test_',
1516
selected: false,
1617
selectedValues: [],
17-
shape: 'rounded' as const,
18-
size: 'medium' as const,
18+
shape: 'rounded',
19+
size: 'medium',
1920
value: 'test',
2021
};
2122

@@ -64,14 +65,14 @@ describe('useInteractionTagSecondaryBase_unstable', () => {
6465
it('should NOT inject DismissRegular children by default (icon injection lives in the styled hook)', () => {
6566
const ref = React.createRef<HTMLButtonElement>();
6667
const { result } = renderHook(() => useInteractionTagSecondaryBase_unstable({}, ref), { wrapper: wrap() });
67-
expect(result.current.root.children).toBeUndefined();
68+
expect(result.current.root).not.toHaveProperty('children');
6869
});
6970

7071
it('should attach onClick and onKeyDown handlers', () => {
7172
const ref = React.createRef<HTMLButtonElement>();
7273
const { result } = renderHook(() => useInteractionTagSecondaryBase_unstable({}, ref), { wrapper: wrap() });
73-
expect(result.current.root.onClick).toBeDefined();
74-
expect(result.current.root.onKeyDown).toBeDefined();
74+
expect(result.current.root.onClick).toEqual(expect.any(Function));
75+
expect(result.current.root.onKeyDown).toEqual(expect.any(Function));
7576
});
7677

7778
it('should build aria-labelledby from interactionTagPrimaryId and own id', () => {
@@ -85,9 +86,9 @@ describe('useInteractionTagSecondaryBase_unstable', () => {
8586
it('should NOT expose design-only fields (appearance/shape/size)', () => {
8687
const ref = React.createRef<HTMLButtonElement>();
8788
const { result } = renderHook(() => useInteractionTagSecondaryBase_unstable({}, ref), { wrapper: wrap() });
88-
expect((result.current as unknown as { appearance?: unknown }).appearance).toBeUndefined();
89-
expect((result.current as unknown as { shape?: unknown }).shape).toBeUndefined();
90-
expect((result.current as unknown as { size?: unknown }).size).toBeUndefined();
89+
expect(result.current).not.toHaveProperty('appearance');
90+
expect(result.current).not.toHaveProperty('shape');
91+
expect(result.current).not.toHaveProperty('size');
9192
});
9293

9394
it('should call handleTagDismiss on Delete/Backspace keyDown via context', () => {

packages/react-components/react-tags/library/src/components/Tag/useTag.test.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ describe('useTag_unstable', () => {
2525
const ref = React.createRef<HTMLElement>();
2626
const { result } = renderHook(() => useTag_unstable({ dismissible }, ref), { wrapper: wrap() });
2727
if (dismissible) {
28-
expect(result.current.root.onClick).toBeDefined();
28+
expect(result.current.root.onClick).toEqual(expect.any(Function));
2929
} else {
30-
expect(result.current.root.onClick).toBeUndefined();
30+
expect(result.current.root).not.toHaveProperty('onClick');
3131
}
3232
});
3333

@@ -70,26 +70,25 @@ describe('useTagBase_unstable', () => {
7070
it('should NOT attach onClick/onKeyDown handlers when not dismissible', () => {
7171
const ref = React.createRef<HTMLElement>();
7272
const { result } = renderHook(() => useTagBase_unstable({}, ref), { wrapper: wrap() });
73-
expect(result.current.root.onClick).toBeUndefined();
74-
expect(result.current.root.onKeyDown).toBeUndefined();
73+
expect(result.current.root).not.toHaveProperty('onClick');
74+
expect(result.current.root).not.toHaveProperty('onKeyDown');
7575
});
7676

7777
it('should attach onClick/onKeyDown handlers when dismissible', () => {
7878
const ref = React.createRef<HTMLElement>();
7979
const { result } = renderHook(() => useTagBase_unstable({ dismissible: true }, ref), { wrapper: wrap() });
8080
const root = result.current.root as React.ButtonHTMLAttributes<HTMLButtonElement>;
81-
expect(root.onClick).toBeDefined();
82-
expect(root.onKeyDown).toBeDefined();
81+
expect(root.onClick).toEqual(expect.any(Function));
82+
expect(root.onKeyDown).toEqual(expect.any(Function));
8383
expect(root.type).toBe('button');
8484
});
8585

8686
it('should NOT inject a default dismissIcon children (icon injection lives in the styled hook)', () => {
8787
const ref = React.createRef<HTMLElement>();
8888
const { result } = renderHook(() => useTagBase_unstable({ dismissible: true }, ref), { wrapper: wrap() });
8989

90-
// dismissIcon slot is rendered by default when dismissible, but no children are injected by base
9190
expect(result.current.dismissIcon).toBeDefined();
92-
expect(result.current.dismissIcon?.children).toBeUndefined();
91+
expect(result.current.dismissIcon).not.toHaveProperty('children');
9392
});
9493

9594
it('should set aria-selected when TagGroupContext role is listbox', () => {

packages/react-components/react-tags/library/src/components/TagGroup/useTagGroup.test.tsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import type { TabsterDOMAttribute } from '@fluentui/react-tabster';
44

55
import { useTagGroup_unstable, useTagGroupBase_unstable } from './useTagGroup';
66

7-
// Slot props are a discriminated union that doesn't expose `data-*` index access at
8-
// the type level; cast to a plain record for the data-attribute assertions below.
97
type RootRecord = Record<string, unknown>;
108

119
describe('useTagGroup_unstable', () => {
@@ -21,9 +19,7 @@ describe('useTagGroup_unstable', () => {
2119
const ref = React.createRef<HTMLDivElement>();
2220
const { result } = renderHook(() => useTagGroup_unstable({}, ref));
2321

24-
// useTagGroup_unstable wires up useArrowNavigationGroup via the base hook's
25-
// arrowNavigationProps option; Tabster's contract is a data-tabster attribute.
26-
expect((result.current.root as RootRecord)['data-tabster']).toBeDefined();
22+
expect((result.current.root as RootRecord)['data-tabster']).toEqual(expect.any(String));
2723
});
2824

2925
it('should default role to toolbar', () => {
@@ -38,7 +34,7 @@ describe('useTagGroupBase_unstable', () => {
3834
it('should NOT include arrow-navigation props when options omitted (true headless mode)', () => {
3935
const ref = React.createRef<HTMLDivElement>();
4036
const { result } = renderHook(() => useTagGroupBase_unstable({}, ref));
41-
expect((result.current.root as RootRecord)['data-tabster']).toBeUndefined();
37+
expect(result.current.root).not.toHaveProperty('data-tabster');
4238
});
4339

4440
it('should spread arrowNavigationProps option onto root when supplied', () => {
@@ -59,8 +55,6 @@ describe('useTagGroupBase_unstable', () => {
5955
result.current.handleTagDismiss(event, { value: 'v1' });
6056

6157
expect(onDismiss).toHaveBeenCalledWith(event, { value: 'v1' });
62-
// innerRef hasn't been attached to a DOM node in the renderHook environment,
63-
// so the container argument is null - we still expect the callback to be invoked.
6458
expect(onAfterTagDismiss).toHaveBeenCalledWith(null);
6559
});
6660

@@ -80,17 +74,17 @@ describe('useTagGroupBase_unstable', () => {
8074
it('should NOT expose design-only fields (size, appearance) on base state', () => {
8175
const ref = React.createRef<HTMLDivElement>();
8276
const { result } = renderHook(() => useTagGroupBase_unstable({}, ref));
83-
expect((result.current as unknown as { size?: unknown }).size).toBeUndefined();
84-
expect((result.current as unknown as { appearance?: unknown }).appearance).toBeUndefined();
77+
expect(result.current).not.toHaveProperty('size');
78+
expect(result.current).not.toHaveProperty('appearance');
8579
});
8680

8781
it('should provide handleTagSelect only when onTagSelect is supplied', () => {
8882
const ref = React.createRef<HTMLDivElement>();
8983
const without = renderHook(() => useTagGroupBase_unstable({}, ref));
90-
expect(without.result.current.handleTagSelect).toBeUndefined();
84+
expect(without.result.current.handleTagSelect).toBe(undefined);
9185

9286
const onTagSelect = jest.fn();
9387
const withSelect = renderHook(() => useTagGroupBase_unstable({ onTagSelect }, ref));
94-
expect(withSelect.result.current.handleTagSelect).toBeDefined();
88+
expect(withSelect.result.current.handleTagSelect).toEqual(expect.any(Function));
9589
});
9690
});

0 commit comments

Comments
 (0)