Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mst middlewares uses jest, moved some packages to the root
- Loading branch information
1 parent
102e2bd
commit dad7ba1
Showing
16 changed files
with
392 additions
and
1,339 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
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
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ node_modules | |
*.log | ||
lib | ||
dist | ||
test-lib/**/*.js | ||
/test-lib/ | ||
coverage | ||
.nyc_output | ||
.idea | ||
|
102 changes: 102 additions & 0 deletions
102
packages/mst-middlewares/__tests__/TimeTraveller.test.ts
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,102 @@ | ||
import { TimeTraveller } from "../src" | ||
import { types, clone } from "mobx-state-tree" | ||
|
||
const TestModel = types | ||
.model({ | ||
x: 1 | ||
}) | ||
.actions(self => ({ | ||
inc() { | ||
self.x += 1 | ||
} | ||
})) | ||
|
||
test("it can time travel", () => { | ||
const m = TestModel.create() | ||
const tt = TimeTraveller.create({}, { targetStore: m }) | ||
|
||
expect(tt.canUndo).toBe(false) | ||
expect(tt.canRedo).toBe(false) | ||
expect(m.x).toBe(1) | ||
|
||
m.inc() | ||
expect(tt.canUndo).toBe(true) | ||
expect(tt.canRedo).toBe(false) | ||
expect(m.x).toBe(2) | ||
|
||
m.inc() | ||
expect(tt.canUndo).toBe(true) | ||
expect(tt.canRedo).toBe(false) | ||
expect(m.x).toBe(3) | ||
|
||
tt.undo() | ||
expect(m.x).toBe(2) | ||
expect(tt.canUndo).toBe(true) | ||
expect(tt.canRedo).toBe(true) | ||
|
||
tt.undo() | ||
expect(m.x).toBe(1) | ||
expect(tt.canUndo).toBe(false) | ||
expect(tt.canRedo).toBe(true) | ||
|
||
tt.redo() | ||
expect(m.x).toBe(2) | ||
expect(tt.canUndo).toBe(true) | ||
expect(tt.canRedo).toBe(true) | ||
|
||
// resets 'future' | ||
m.inc() | ||
expect(m.x).toBe(3) | ||
expect(tt.canUndo).toBe(true) | ||
expect(tt.canRedo).toBe(false) | ||
}) | ||
|
||
test("it can time travel same store and persist state", () => { | ||
const W = types.model({ | ||
model: types.optional(TestModel, {}), | ||
traveler: types.optional(TimeTraveller, { targetPath: "../model" }) | ||
}) | ||
const w = W.create() | ||
const m = w.model | ||
const tt = w.traveler | ||
|
||
expect(tt.canUndo).toBe(false) | ||
expect(tt.canRedo).toBe(false) | ||
expect(m.x).toBe(1) | ||
|
||
m.inc() | ||
expect(tt.canUndo).toBe(true) | ||
expect(tt.canRedo).toBe(false) | ||
expect(m.x).toBe(2) | ||
|
||
m.inc() | ||
expect(tt.canUndo).toBe(true) | ||
expect(tt.canRedo).toBe(false) | ||
expect(m.x).toBe(3) | ||
|
||
tt.undo() | ||
expect(m.x).toBe(2) | ||
expect(tt.canUndo).toBe(true) | ||
expect(tt.canRedo).toBe(true) | ||
|
||
// Clone of the store should inherit the same state! | ||
const w2 = clone(w) | ||
const m2 = w2.model | ||
const tt2 = w2.traveler | ||
|
||
tt2.undo() | ||
expect(m2.x).toBe(1) | ||
expect(tt2.canUndo).toBe(false) | ||
expect(tt2.canRedo).toBe(true) | ||
|
||
tt2.redo() | ||
expect(m2.x).toBe(2) | ||
expect(tt2.canUndo).toBe(true) | ||
expect(tt2.canRedo).toBe(true) | ||
|
||
// resets 'future' | ||
m2.inc() | ||
expect(m2.x).toBe(3) | ||
expect(tt2.canUndo).toBe(true) | ||
expect(tt2.canRedo).toBe(false) | ||
}) |
Oops, something went wrong.