diff --git a/packages/mobx-state-tree/__tests__/core/date.test.ts b/packages/mobx-state-tree/__tests__/core/date.test.ts new file mode 100644 index 000000000..0c6e51fbb --- /dev/null +++ b/packages/mobx-state-tree/__tests__/core/date.test.ts @@ -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) +})