Skip to content

Commit 13c85ad

Browse files
committed
test(react-headless-components-preview): switch usePositioning style assertions to toHaveStyle
1 parent b64a9f1 commit 13c85ad

1 file changed

Lines changed: 10 additions & 19 deletions

File tree

  • packages/react-components/react-headless-components-preview/library/src/hooks/usePositioning

packages/react-components/react-headless-components-preview/library/src/hooks/usePositioning/usePositioning.test.tsx

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,51 +40,47 @@ describe('usePositioning', () => {
4040
result.current.containerRef(node);
4141

4242
expect(node.style.getPropertyValue('position-anchor')).toMatch(/^--popover-anchor-/);
43-
expect(node.style.getPropertyValue('position-area')).toBe('block-end span-inline-end');
43+
expect(node).toHaveStyle({ positionArea: 'block-end span-inline-end' });
4444
});
4545

4646
it('containerRef writes position: absolute by default and clears the UA inset/margin defaults', () => {
4747
const result = mountHook();
4848
const node = document.createElement('div');
4949
result.current.containerRef(node);
5050

51-
expect(node.style.position).toBe('absolute');
52-
expect(node.style.inset).toBe('auto');
53-
expect(node.style.margin).toBe('0px');
51+
expect(node).toHaveStyle({ position: 'absolute', inset: 'auto', margin: '0px' });
5452
});
5553

5654
it('containerRef honors strategy: "fixed"', () => {
5755
const result = mountHook({ strategy: 'fixed' });
5856
const node = document.createElement('div');
5957
result.current.containerRef(node);
6058

61-
expect(node.style.position).toBe('fixed');
59+
expect(node).toHaveStyle({ position: 'fixed' });
6260
});
6361

6462
it('containerRef writes data-placement matching (position, align)', () => {
6563
const result = mountHook({ position: 'below', align: 'start' });
6664
const node = document.createElement('div');
6765
result.current.containerRef(node);
6866

69-
expect(node.getAttribute('data-placement')).toBe('below-start');
67+
expect(node).toHaveAttribute('data-placement', 'below-start');
7068
});
7169

7270
it('containerRef sets position-try-fallbacks to the three-try flip chain by default', () => {
7371
const result = mountHook();
7472
const node = document.createElement('div');
7573
result.current.containerRef(node);
7674

77-
expect(node.style.getPropertyValue('position-try-fallbacks')).toBe(
78-
'flip-block, flip-inline, flip-block flip-inline',
79-
);
75+
expect(node).toHaveStyle({ positionTryFallbacks: 'flip-block, flip-inline, flip-block flip-inline' });
8076
});
8177

8278
it('containerRef uses custom fallbackPositions verbatim when provided', () => {
8379
const result = mountHook({ fallbackPositions: ['below-start', 'after'] });
8480
const node = document.createElement('div');
8581
result.current.containerRef(node);
8682

87-
expect(node.style.getPropertyValue('position-try-fallbacks')).toBe('block-end span-inline-end, inline-end');
83+
expect(node).toHaveStyle({ positionTryFallbacks: 'block-end span-inline-end, inline-end' });
8884
});
8985

9086
it('containerRef removes position-try-fallbacks when pinned', () => {
@@ -101,17 +97,15 @@ describe('usePositioning', () => {
10197
const node = document.createElement('div');
10298
result.current.containerRef(node);
10399

104-
expect(node.style.getPropertyValue('position-area')).toBe('center');
105-
expect(node.style.getPropertyValue('align-self')).toBe('end');
106-
expect(node.style.getPropertyValue('justify-self')).toBe('start');
100+
expect(node).toHaveStyle({ positionArea: 'center', alignSelf: 'end', justifySelf: 'start' });
107101
});
108102

109103
it('containerRef writes place-self: anchor-center for center alignment (crbug 438334710 workaround)', () => {
110104
const result = mountHook({ position: 'above', align: 'center' });
111105
const node = document.createElement('div');
112106
result.current.containerRef(node);
113107

114-
expect(node.style.getPropertyValue('place-self')).toBe('anchor-center');
108+
expect(node).toHaveStyle({ placeSelf: 'anchor-center' });
115109
});
116110

117111
it('containerRef does not write place-self for non-center alignments', () => {
@@ -127,20 +121,17 @@ describe('usePositioning', () => {
127121
it('containerRef writes matchTargetSize width via anchor-size()', () => {
128122
const result = mountHook({ matchTargetSize: 'width' });
129123
const node = document.createElement('div');
130-
const spy = jest.spyOn(node.style, 'setProperty');
131124
result.current.containerRef(node);
132125

133-
expect(spy).toHaveBeenCalledWith('width', 'anchor-size(width)');
134-
spy.mockRestore();
126+
expect(node).toHaveStyle({ width: 'anchor-size(width)' });
135127
});
136128

137129
it('containerRef applies offset as logical margins', () => {
138130
const result = mountHook({ position: 'below', offset: { mainAxis: 8, crossAxis: 4 } });
139131
const node = document.createElement('div');
140132
result.current.containerRef(node);
141133

142-
expect(node.style.marginBlockStart).toBe('8px');
143-
expect(node.style.marginInlineStart).toBe('4px');
134+
expect(node).toHaveStyle({ marginBlockStart: '8px', marginInlineStart: '4px' });
144135
});
145136
});
146137

0 commit comments

Comments
 (0)