forked from mobxjs/mobx-state-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a21b5e
commit 7d8c203
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |