forked from mobxjs/mobx-state-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.test.ts
58 lines (52 loc) · 1.5 KB
/
date.test.ts
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
import { configure } from "mobx"
import {
onSnapshot,
onPatch,
applyPatch,
applySnapshot,
getSnapshot,
types,
unprotect,
isStateTreeNode,
SnapshotOut,
IJsonPatch,
IAnyModelType,
detach
} from "../../src"
const createTestFactories = () => {
const Factory = types.model({
date: types.Date
})
return { Factory }
}
// === FACTORY TESTS ===
test("it should create a factory", () => {
const { Factory } = createTestFactories()
const now = new Date()
const snapshot = getSnapshot(Factory.create({ date: now }))
expect(snapshot).toEqual({ date: now.getTime() })
})
// === PATCHES TESTS ===
test("it should emit replace patch for different date", () => {
const { Factory } = createTestFactories()
const date1 = new Date()
const date2 = new Date(date1)
date2.setHours(date1.getHours() + 1)
const doc = Factory.create({ date: date1 })
unprotect(doc)
let patches: IJsonPatch[] = []
onPatch(doc, (patch) => patches.push(patch))
doc.date = date2
expect(patches).toEqual([{ op: "replace", path: "/date", value: date2.getTime() }])
})
test("it should not emit replace patch for same date", () => {
const { Factory } = createTestFactories()
const date1 = new Date()
const date2 = new Date(date1)
const doc = Factory.create({ date: date1 })
unprotect(doc)
let patches: IJsonPatch[] = []
onPatch(doc, (patch) => patches.push(patch))
doc.date = date2
expect(patches.length).toEqual(0)
})