forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp.test.tsx
78 lines (68 loc) · 2.07 KB
/
exp.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import React, { createContext, StrictMode } from "react"
import { inject, observer, Observer, enableStaticRendering, useStaticRendering } from "../src"
import { render, act } from "@testing-library/react"
import {
getObserverTree,
_resetGlobalState,
action,
computed,
observable,
transaction,
makeObservable
} from "mobx"
import { withConsole } from "./utils/withConsole"
/**
* some test suite is too tedious
*/
afterEach(() => {
jest.useRealTimers()
})
// let consoleWarnMock: jest.SpyInstance | undefined
// afterEach(() => {
// consoleWarnMock?.mockRestore()
// })
test("TODO", async () => {
let renderCount = 0
const Child = observer(function Child({ children }) {
renderCount++
// Accesses Parent's this.props
return children()
})
@observer
class Parent extends React.Component<any> {
// intentionally stable, so test breaks when you disable observable props (comment line 239 in observerClass)
renderCallback = () => {
return this.props.x
}
render() {
// Access observable props as part of child
return <Child>{this.renderCallback}</Child>
}
}
function Root() {
const [x, setX] = React.useState(0)
// Send new props to Parent
return (
<div onClick={() => setX(x => x + 1)}>
<Parent x={x} />
</div>
)
}
// const app = (
// <StrictMode>
// <Root />
// </StrictMode>
// )
const app = <Root />
const { unmount, container } = render(app)
expect(container).toHaveTextContent("0")
console.log("After initial render", renderCount) // is 1, because during initial render the Child isn't subscribed yet
await new Promise(resolve => setTimeout(() => resolve(null), 1000))
act(() => {
console.log("changing state")
container.querySelector("div")?.click()
})
expect(container).toHaveTextContent("1")
console.log("After state change", renderCount) // is 3, should be 2
unmount()
})