forked from mobxjs/mobx
-
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.
Mock console.warn in tests and verify warnings are called in the righ…
…t place (mobxjs#3352) * Mock console.warn in tests and verify warnings are called in the right place * Add afterEach to correctly restore console.warn mock. Remove test for useObserver * Fix accidentally removed code * Fix accidentally removed code * Rename test * Make branch up-to-date with master * Remove outdated snapshots * Don't change not related files * Don't change not related files * Don't change not related files * Turn off git hooks locally to avoid touching not related files * Do not touch unrelated files * Mock only once
- Loading branch information
Showing
16 changed files
with
350 additions
and
72 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
24 changes: 24 additions & 0 deletions
24
...es/mobx-react-lite/__tests__/__snapshots__/useAsObservableSource.deprecated.test.tsx.snap
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,24 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`base useAsObservableSource should work with <Observer> 1`] = ` | ||
[MockFunction] { | ||
"calls": Array [ | ||
Array [ | ||
"[mobx-react-lite] 'useAsObservableSource' is deprecated, please store the values directly in an observable, for example by using 'useLocalObservable', and sync future updates using 'useEffect' when needed. See the README for examples.", | ||
], | ||
Array [ | ||
"[mobx-react-lite] 'useLocalStore' is deprecated, use 'useLocalObservable' instead.", | ||
], | ||
], | ||
"results": Array [ | ||
Object { | ||
"type": "return", | ||
"value": undefined, | ||
}, | ||
Object { | ||
"type": "return", | ||
"value": undefined, | ||
}, | ||
], | ||
} | ||
`; |
33 changes: 33 additions & 0 deletions
33
packages/mobx-react-lite/__tests__/__snapshots__/useLocalStore.deprecated.test.tsx.snap
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,33 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`base useLocalStore should work 1`] = ` | ||
[MockFunction] { | ||
"calls": Array [ | ||
Array [ | ||
"[mobx-react-lite] 'useLocalStore' is deprecated, use 'useLocalObservable' instead.", | ||
], | ||
], | ||
"results": Array [ | ||
Object { | ||
"type": "return", | ||
"value": undefined, | ||
}, | ||
], | ||
} | ||
`; | ||
|
||
exports[`is used to keep observable within component body with props and useObserver 1`] = ` | ||
[MockFunction] { | ||
"calls": Array [ | ||
Array [ | ||
"[mobx-react-lite] 'useAsObservableSource' is deprecated, please store the values directly in an observable, for example by using 'useLocalObservable', and sync future updates using 'useEffect' when needed. See the README for examples.", | ||
], | ||
], | ||
"results": Array [ | ||
Object { | ||
"type": "return", | ||
"value": undefined, | ||
}, | ||
], | ||
} | ||
`; |
74 changes: 74 additions & 0 deletions
74
packages/mobx-react-lite/__tests__/enforceActions.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,74 @@ | ||
import * as mobx from "mobx" | ||
import { _resetGlobalState } from "mobx" | ||
import * as React from "react" | ||
import { useEffect } from "react" | ||
import { observer, useLocalObservable } from "mobx-react" | ||
import { render } from "@testing-library/react" | ||
|
||
let consoleWarnMock: jest.SpyInstance | undefined | ||
afterEach(() => { | ||
consoleWarnMock?.mockRestore() | ||
}) | ||
|
||
afterEach(() => { | ||
_resetGlobalState() | ||
}) | ||
|
||
describe("enforcing actions", () => { | ||
it("'never' should work", () => { | ||
consoleWarnMock = jest.spyOn(console, "warn").mockImplementation(() => {}) | ||
mobx.configure({ enforceActions: "never" }) | ||
|
||
const Parent = observer(() => { | ||
const obs = useLocalObservable(() => ({ | ||
x: 1 | ||
})) | ||
useEffect(() => { | ||
obs.x++ | ||
}, []) | ||
|
||
return <div>{obs.x}</div> | ||
}) | ||
|
||
render(<Parent />) | ||
expect(consoleWarnMock).not.toBeCalled() | ||
}) | ||
|
||
it("'observed' should work", () => { | ||
consoleWarnMock = jest.spyOn(console, "warn").mockImplementation(() => {}) | ||
mobx.configure({ enforceActions: "observed" }) | ||
|
||
const Parent = observer(() => { | ||
const obs = useLocalObservable(() => ({ | ||
x: 1 | ||
})) | ||
useEffect(() => { | ||
obs.x++ | ||
}, []) | ||
|
||
return <div>{obs.x}</div> | ||
}) | ||
|
||
render(<Parent />) | ||
expect(consoleWarnMock).toBeCalledTimes(1) | ||
}) | ||
|
||
it("'always' should work", () => { | ||
consoleWarnMock = jest.spyOn(console, "warn").mockImplementation(() => {}) | ||
mobx.configure({ enforceActions: "always" }) | ||
|
||
const Parent = observer(() => { | ||
const obs = useLocalObservable(() => ({ | ||
x: 1 | ||
})) | ||
useEffect(() => { | ||
obs.x++ | ||
}, []) | ||
|
||
return <div>{obs.x}</div> | ||
}) | ||
|
||
render(<Parent />) | ||
expect(consoleWarnMock).toBeCalledTimes(1) | ||
}) | ||
}) |
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
Oops, something went wrong.