Created
December 28, 2017 15:29
-
-
Save aaronksaunders/13327354d7b27f001109df9936fad86e to your computer and use it in GitHub Desktop.
mobx-state-tree tests
This file contains hidden or 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
import { getSnapshot } from 'mobx-state-tree'; | |
import { assert, expect } from 'chai'; | |
//import { Users } from './../src/store'; | |
import 'mocha' | |
import { Users, User, Boutique, ClothingItem } from './../src/store'; | |
describe('Boutique', function () { | |
let b = null; | |
it('should create Boutique', function () { | |
b = Boutique.create({ | |
id: "Boutique-1", | |
name: "Boutique-1", | |
link: "www.boutique-1.store", | |
owner: User.create({ | |
id: '10', | |
name: 'Aaron Saunders' | |
}), | |
items: [] | |
}) | |
assert.exists(b); | |
assert.deepEqual(getSnapshot(b), { | |
id: "Boutique-1", | |
name: "Boutique-1", | |
link: "www.boutique-1.store", | |
owner: "10", | |
items: [] | |
}) | |
}); | |
it('should add item to Boutique', function () { | |
let item = ClothingItem.create({ | |
id: "clothing-10", | |
caption: "clothing item 10 caption", | |
owner: b, | |
name: "clothing item 10" | |
}) | |
b.addItem(item) | |
assert.deepEqual(getSnapshot(b), { | |
id: "Boutique-1", | |
name: "Boutique-1", | |
link: "www.boutique-1.store", | |
owner: "10", | |
items: [{ | |
...item, | |
owner: b.id | |
}] | |
}) | |
}) | |
it('should update item in Boutique', function () { | |
// get the first item | |
let item = b.items[0] | |
let updatedProps = { | |
caption: "clothing item 10 caption - updated", | |
name: "clothing item 10 - updated" | |
} | |
item.update(updatedProps) | |
assert.deepEqual(getSnapshot(b), { | |
id: "Boutique-1", | |
name: "Boutique-1", | |
link: "www.boutique-1.store", | |
owner: "10", | |
items: [{ | |
...item, | |
...updatedProps, | |
owner: b.id | |
}] | |
}) | |
}) | |
it('should have 2 items in Boutique', function () { | |
// get the first item and fix owner reference | |
let item1 = getSnapshot(b.items[0]) | |
// create and add second item | |
let item2 = ClothingItem.create({ | |
id: "clothing-11", | |
caption: "clothing item 11 caption", | |
owner: b, | |
name: "clothing item 11" | |
}) | |
b.addItem(item2) | |
assert.deepEqual(getSnapshot(b), { | |
id: "Boutique-1", | |
name: "Boutique-1", | |
link: "www.boutique-1.store", | |
owner: "10", | |
items: [{ | |
...item1, | |
owner: b.id | |
}, { | |
...item2, | |
owner: b.id | |
}] | |
}) | |
assert.equal(b.items.length, 2) | |
}) | |
it('should remove 1 item from Boutique', function () { | |
let item1 = getSnapshot(b.items[0]) | |
let item2 = b.items[1]; | |
b.removeItem(item2) | |
assert.deepEqual(getSnapshot(b), { | |
id: "Boutique-1", | |
name: "Boutique-1", | |
link: "www.boutique-1.store", | |
owner: "10", | |
items: [{ | |
...item1, | |
owner: b.id | |
}] | |
}) | |
assert.equal(b.items.length, 1) | |
}) | |
it('should remove 1 item from Boutique', function () { | |
let item1 = b.items[0] | |
item1.delete(); | |
assert.deepEqual(getSnapshot(b), { | |
id: "Boutique-1", | |
name: "Boutique-1", | |
link: "www.boutique-1.store", | |
owner: "10", | |
items: [] | |
}) | |
assert.equal(b.items.length, 0) | |
}) | |
}) | |
describe('UserStore', function () { | |
it('should create store', function () { | |
assert.exists(Users.create({ | |
selectedUser: "", | |
users: [] | |
})); | |
}); | |
it('should create store with one user', function () { | |
let user1 = User.create({ | |
id: '10', | |
name: 'Aaron Saunders' | |
}) | |
let userStore = Users.create({ | |
selectedUser: "", | |
users: [user1] | |
}) | |
assert.exists(userStore); | |
assert.equal(userStore.users.length, 1) | |
}); | |
it('should update user in store', function () { | |
let user1 = User.create({ | |
id: '10', | |
name: 'Aaron Saunders' | |
}) | |
let userStore = Users.create({ | |
selectedUser: "", | |
users: [user1] | |
}) | |
user1.update({ name: "Aaron K. Saunders" }) | |
assert.deepEqual(getSnapshot(user1), { | |
id: '10', | |
name: 'Aaron K. Saunders' | |
}) | |
}); | |
it('should create store with two users', function () { | |
let user1 = User.create({ | |
id: '10', | |
name: 'Aaron Saunders' | |
}) | |
let user2 = User.create({ | |
id: '11', | |
name: 'Andreas Saunders' | |
}) | |
let userStore = Users.create({ | |
selectedUser: "", | |
users: [user1, user2] | |
}) | |
assert.exists(userStore); | |
assert.equal(userStore.users.length, 2) | |
assert.deepEqual(getSnapshot(userStore.users), [user1, user2]) | |
}); | |
it('should select user properly', function () { | |
let user1 = User.create({ | |
id: '10', | |
name: 'Aaron Saunders' | |
}) | |
let user2 = User.create({ | |
id: '11', | |
name: 'Andreas Saunders' | |
}) | |
let userStore = Users.create({ | |
selectedUser: "", | |
users: [user1, user2] | |
}) | |
assert.exists(userStore); | |
assert.equal(userStore.users.length, 2) | |
userStore.selectUser(user2) | |
assert.equal(userStore.selectedUser, user2) | |
}); | |
it('should NOT select user properly', function () { | |
let user1 = User.create({ | |
id: '10', | |
name: 'Aaron Saunders' | |
}) | |
let user2 = User.create({ | |
id: '11', | |
name: 'Andreas Saunders' | |
}) | |
let userStore = Users.create({ | |
selectedUser: "", | |
users: [user1] | |
}) | |
assert.exists(userStore); | |
userStore.selectUser(user2) | |
expect(() => { | |
assert.equal(userStore.selectedUser, user2) | |
}).throws("Failed to resolve reference of type User") | |
}); | |
}); |
This file contains hidden or 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
// ({ | |
// "babel": { | |
// "env": "dev" | |
// }, | |
// "ts": { | |
// "compilerOptions": { | |
// target: "es6", | |
// } | |
// } | |
// }) | |
import { types, getSnapshot, applyPatch, applySnapshot, destroy, getRoot } from 'mobx-state-tree' | |
import { type } from 'os'; | |
export const User = types | |
.model("User", { | |
id: types.identifier(types.string), | |
name: types.string, | |
}) | |
.actions((self) => { | |
return { | |
update: (_changes) => { | |
let changes = { ...self, ..._changes } | |
applySnapshot(self, changes) | |
} | |
} | |
}) | |
export const Users = types | |
.model("Users", { | |
selectedUser: types.optional(types.reference(User), ""), | |
users: types.optional(types.array(User), []), | |
}) | |
.actions((self) => { | |
return { | |
selectUser: (_user) => { | |
console.log(_user.id) | |
self.selectedUser = _user.id | |
} | |
} | |
}) | |
export const ClothingItem = types | |
.model("ClothingItem", { | |
id: types.identifier(types.string), | |
owner: types.optional(types.reference(types.late(() => Boutique)), ""), | |
name: types.string, | |
caption: types.string, | |
}) | |
.actions((self) => { | |
return { | |
update: (_changes) => { | |
let changes = { ...self, ..._changes } | |
applySnapshot(self, changes) | |
}, | |
delete: () => { | |
getRoot(self).removeItem(self) | |
} | |
} | |
}) | |
export const Boutique = types | |
.model("Boutique", { | |
id: types.identifier(types.string), | |
name: types.string, | |
link: types.string, | |
owner: types.optional(types.reference(User), ""), | |
items: types.optional(types.array(ClothingItem), []), | |
}) | |
.actions((self) => { | |
return { | |
addItem: (_item: typeof ClothingItem.Type) => { | |
self.items.push(_item) | |
}, | |
removeItem: (_item: typeof ClothingItem.Type) => { | |
destroy(_item) | |
} | |
} | |
}) | |
let user1 = User.create({ | |
id: '10', | |
name: 'Aaron Saunders' | |
}) | |
let userStore = Users.create({ | |
selectedUser: "", | |
users: [user1] | |
}) | |
let x = getSnapshot(userStore)/*?*/ | |
const MainStore = types | |
.model("MainStore", { | |
users: types.maybe(Users), | |
boutiques: types.optional(types.array(Boutique), []), | |
}) | |
export default MainStore |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment