This repository has been archived by the owner on Dec 31, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add test for proper reaction disposal in StrictMode * Use jest-mock-console instead of mocking console.error by hand * Move test into separate useObserver test file I've tweaked the test name to reflect what it's really checking for. * Add a second test to verify that reactions are disposed after unmount N.B. Depending on the implementation of a fix, this test might need some extra delays or act() calls to trigger a delayed cleanup before asserting that all is clean. * Remove 'leak' test as it's a separately-solvable issue. (I'll move this test into another PR.) * Track whether the current component has been committed before forcing update * Remove the now-unused useUnmount
- Loading branch information
Showing
3 changed files
with
57 additions
and
13 deletions.
There are no files selected for viewing
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
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
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,42 @@ | ||
import mockConsole from "jest-mock-console" | ||
import * as mobx from "mobx" | ||
import * as React from "react" | ||
import { act, cleanup, render } from "react-testing-library" | ||
|
||
import { useObserver } from "../src" | ||
|
||
afterEach(cleanup) | ||
|
||
test("uncommitted observing components should not attempt state changes", () => { | ||
const store = mobx.observable({ count: 0 }) | ||
|
||
const TestComponent = () => useObserver(() => <div>{store.count}</div>) | ||
|
||
// Render our observing component wrapped in StrictMode | ||
const rendering = render( | ||
<React.StrictMode> | ||
<TestComponent /> | ||
</React.StrictMode> | ||
) | ||
|
||
// That will have caused our component to have been rendered | ||
// more than once, but when we unmount it'll only unmount once. | ||
rendering.unmount() | ||
|
||
// Trigger a change to the observable. If the reactions were | ||
// not disposed correctly, we'll see some console errors from | ||
// React StrictMode because we're calling state mutators to | ||
// trigger an update. | ||
const restoreConsole = mockConsole() | ||
try { | ||
act(() => { | ||
store.count++ | ||
}) | ||
|
||
// Check to see if any console errors were reported. | ||
// tslint:disable-next-line: no-console | ||
expect(console.error).not.toHaveBeenCalled() | ||
} finally { | ||
restoreConsole() | ||
} | ||
}) |