Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test for async deserialization of mobx observable #115

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ require("./simple")
require("./classes")
require("./typescript/ts.js")
require("./babel/babel-compiled.js")
require("./mobx")
128 changes: 128 additions & 0 deletions test/mobx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
const mobx = require("mobx")
const test = require("tape")
const {
object,
custom,
createModelSchema,
createSimpleSchema,
serialize,
deserialize,
} = require("../")

test("(de-)serialization for mobx observables", t => {
class TodoList {
// mobx.decorate requires pre-existing class properties
data = undefined
constructor() {
this.data = {
list: []
}
}
}

class Todo {
constructor(title) {
this.title = title
}
}

mobx.decorate(TodoList, {
data: mobx.observable,
})

const asyncTodoResolver = (v, ctx, old, cb) => {
setTimeout(() => cb(null, v.map(title => { return new Todo(title) })), 0)
}
const syncTodoResolver = (v, ctx, old, cb) => {
cb(null, v.map(title => { return new Todo(title) }))
}

const asyncModelSchema = createModelSchema(TodoList, {
data: object(createSimpleSchema({
list: custom(
v => v.map(t => t.title),
(v, ctx, old, cb) => asyncTodoResolver(v, ctx, old, cb)
)
}))
})

const syncModelSchema = createModelSchema(TodoList, {
data: object(createSimpleSchema({
list: custom(
v => v.map(t => t.title),
(v, ctx, old, cb) => syncTodoResolver(v, ctx, old, cb)
)
}))
})

test("serialize", t2 => {
const todoList = new TodoList()
todoList.data.list.push(
new Todo("todo 1"),
new Todo("todo 2"),
)
const json = serialize(syncModelSchema, todoList)

t.deepEqual(json, {
data: {
list: [
"todo 1",
"todo 2"
]
}
})
t2.end()
})

test("sync deserialize", t2 => {
const json = {
data: {
list: [
"todo 1",
"todo 2"
]
}
}

deserialize(syncModelSchema, json, (err, todoList) => {
t.ok(todoList instanceof TodoList, "deserialized object is an instance of TodoList")
t.notEqual(todoList.data, undefined, "TodoList.data is not undefined")
const data = todoList.data || undefined
t.ok(Array.isArray(todoList.data.list), "TodoList.data.list is an array")
const list = data.list || undefined
const listL = list ? list.length : undefined

t.equal(list ? list.length : undefined, 2, "TodoList.data.list has two elements")
t.equal(listL ? list[0].title : undefined, "todo 1", "TodoList's 1st todo has title todo 1")
t.equal(listL ? list[1].title : undefined, "todo 2", "TodoList's 2nd todo has title todo 2")
t2.end()
})
})

test("async deserialize", t2 => {
const json = {
data: {
list: [
"todo 1",
"todo 2"
]
}
}

deserialize(asyncModelSchema, json, (err, todoList) => {
t.ok(todoList instanceof TodoList, "deserialized object is an instance of TodoList")
t.notEqual(todoList.data, undefined, "TodoList.data is not undefined")
const data = todoList.data || undefined
t.ok(Array.isArray(todoList.data.list), "TodoList.data.list is an array")
const list = data.list || undefined
const listL = list ? list.length : undefined

t.equal(list ? list.length : undefined, 2, "TodoList.data.list has two elements")
t.equal(listL ? list[0].title : undefined, "todo 1", "TodoList's 1st todo has title todo 1")
t.equal(listL ? list[1].title : undefined, "todo 2", "TodoList's 1st todo has title todo 2")
t2.end()
})
})

t.end()
})