Skip to content

Commit

Permalink
Add snapshot tests to array.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
davetapley committed Sep 19, 2023
1 parent 7d8c203 commit 9c708de
Showing 1 changed file with 68 additions and 11 deletions.
79 changes: 68 additions & 11 deletions packages/mobx-state-tree/__tests__/core/array.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import {
unprotect,
onSnapshot,
onPatch,
clone,
isAlive,
IJsonPatch,
applyPatch,
getPath,
applySnapshot,
cast,
clone,
detach,
getPath,
getSnapshot,
types,
IJsonPatch,
isAlive,
onPatch,
onSnapshot,
setLivelinessChecking,
detach,
cast
types,
unprotect
} from "../../src"
import { observable, autorun, configure } from "mobx"
import { autorun, configure, observable } from "mobx"

const createTestFactories = () => {
const ItemFactory = types.optional(
Expand Down Expand Up @@ -55,6 +55,63 @@ test("it should emit snapshots", () => {
doc.push(ItemFactory.create())
expect(snapshots).toEqual([[{ to: "world" }]])
})
test("it should not emit snapshot on idempotent assignment to array of numbers", () => {
const store = types
.model({
todos: types.array(types.number)
})
.create({
todos: [1, 2, 3, 4]
})
unprotect(store)
let snapshots = []

onSnapshot(store, (snapshot) => snapshots.push(snapshot))

expect(snapshots.length).toEqual(0)

store.todos[0] = 1
expect(snapshots.length).toEqual(0)

store.todos.replace([1, 2, 3, 4])
expect(snapshots.length).toEqual(0)

store.todos[0] = 2
expect(snapshots.length).toEqual(1)
})

test("it should emit snapshot on idempotent assignment of objects", () => {
const Task = types.model("Task", {
x: types.string
})
const a = Task.create({ x: "a" }),
b = Task.create({ x: "b" }),
c = Task.create({ x: "c" }),
d = Task.create({ x: "d" })
const store = types
.model({
todos: types.array(Task)
})
.create({
todos: [a, b, c, d]
})
unprotect(store)
let snapshots = []

onSnapshot(store, (snapshot) => snapshots.push(snapshot))

expect(snapshots.length).toEqual(0)

store.todos[0].x = "a"
expect(snapshots.length).toEqual(0)

store.todos[0] = { x: "a" }
expect(snapshots.length).toEqual(1)

store.todos.replace([a, b, c, d])
expect(snapshots.length).toEqual(2)
})

test("it should apply snapshots", () => {
const { Factory, ItemFactory } = createTestFactories()
const doc = Factory.create()
Expand Down

0 comments on commit 9c708de

Please sign in to comment.