Skip to content

Commit

Permalink
Add date.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
davetapley committed Sep 11, 2023
1 parent 2a21b5e commit 7d8c203
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions packages/mobx-state-tree/__tests__/core/date.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)
})

0 comments on commit 7d8c203

Please sign in to comment.