Skip to content

Commit

Permalink
Add unit test for missing observable changes before useEffect runs
Browse files Browse the repository at this point in the history
This is a test to check that observable changes made between the
first component render and commit are not lost.

It currently fails (and did so before the change in PR mobxjs#119)
  • Loading branch information
Royston-Shufflebotham-i2 committed Apr 4, 2019
1 parent e055f95 commit 9c06f46
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test/useObserver.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as mobx from "mobx"
import * as React from "react"
import { act, cleanup, render } from "react-testing-library"

import ReactDOM from "react-dom"
import { useObserver } from "../src"
import { resetCleanupScheduleForTests } from "../src/useObserver"

Expand Down Expand Up @@ -42,6 +43,34 @@ test("uncommitted observing components should not attempt state changes", () =>
}
})

test("observable changes before first commit are not lost", () => {
const store = mobx.observable({ value: "initial" })

const TestComponent = () => useObserver(() => <div>{store.value}</div>)

// Render our observing component wrapped in StrictMode, but using
// raw ReactDOM.render (not react-testing-library render) because we
// don't want the useEffect calls to have run just yet...
const rootNode = document.createElement("div")
ReactDOM.render(
<React.StrictMode>
<TestComponent />
</React.StrictMode>,
rootNode
)

// Change our observable. This is happening between the initial render of
// our component and its initial commit, so it isn't fully mounted yet.
// We want to ensure that the change isn't lost.
store.value = "changed"

act(() => {
// no-op
})

expect(rootNode.textContent).toBe("changed")
})

test("uncommitted components should not leak observations", async () => {
resetCleanupScheduleForTests()

Expand Down

0 comments on commit 9c06f46

Please sign in to comment.