forked from microsoft/fluentui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: updates should be synchronous in unit tests (microsoft#30014)
* fix: updates should be synchronous in unit tests React testing platforms will often output errors when state updates happen outside `act` Since there is nothing obvious to wait for we just avoid debouncing in unit test environments. Adds tests to make sure that the synchronous update only happens in test environment * changefile * Update packages/react-components/react-overflow/src/components/Overflow.test.tsx
- Loading branch information
Showing
4 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@fluentui-priority-overflow-01b11133-40d3-440a-b665-57874bffc86f.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "fix: updates should be synchronous in unit tests", | ||
"packageName": "@fluentui/priority-overflow", | ||
"email": "lingfangao@hotmail.com", | ||
"dependentChangeType": "patch" | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/react-components/priority-overflow/src/debounce.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { debounce } from './debounce'; | ||
|
||
describe('debounce', () => { | ||
const tick = () => new Promise(res => setTimeout(res, 0)); | ||
|
||
beforeAll(() => { | ||
// Remove NODE_ENV = 'test' to properly test debounce feature | ||
process.env.NODE_ENV = 'production'; | ||
}); | ||
|
||
afterAll(() => { | ||
process.env.NODE_ENV = 'test'; | ||
}); | ||
|
||
it('will only run once per tick', async () => { | ||
let count = 0; | ||
const debounced = debounce(() => count++); | ||
|
||
for (let i = 0; i < 1000; i++) { | ||
debounced(); | ||
} | ||
|
||
await tick(); | ||
|
||
expect(count).toBe(1); | ||
|
||
for (let i = 0; i < 1000; i++) { | ||
debounced(); | ||
} | ||
|
||
await tick(); | ||
|
||
expect(count).toBe(2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/react-components/react-overflow/src/components/Overflow.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import * as React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import { Overflow } from './Overflow'; | ||
import { OverflowItem } from './OverflowItem'; | ||
describe('Overflow', () => { | ||
beforeAll(() => { | ||
// https://github.com/jsdom/jsdom/issues/3368 | ||
global.ResizeObserver = class ResizeObserver { | ||
public observe() { | ||
// do nothing | ||
} | ||
public unobserve() { | ||
// do nothing | ||
} | ||
public disconnect() { | ||
// do nothing | ||
} | ||
}; | ||
}); | ||
|
||
it('should not throw on console.error', async () => { | ||
// Updates to overflow state are batched with a microtask debouncer (see createOverflowManager) | ||
// This means that unit tests will often warn on updates happening outside of act | ||
// There's no real way to fix this nicely because there's nothing obvious to wait for since the | ||
// update happens in a microtask. | ||
// | ||
// The current debounce implementation is synchronous when NODE_ENV === 'test' | ||
// This test is a canary to make sure unit tests don't emit warnings | ||
console.error = message => { | ||
throw new Error(message); | ||
}; | ||
|
||
render( | ||
<Overflow minimumVisible={1}> | ||
<div> | ||
<OverflowItem id="1"> | ||
<button>foo</button> | ||
</OverflowItem> | ||
<OverflowItem id="2"> | ||
<button>foo</button> | ||
</OverflowItem> | ||
<OverflowItem id="3"> | ||
<button>foo</button> | ||
</OverflowItem> | ||
</div> | ||
</Overflow>, | ||
); | ||
}); | ||
}); |