Skip to content

Commit

Permalink
add test for undefined required prop
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Pollak committed Sep 26, 2019
1 parent 3614a64 commit e717291
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/core/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export function serializeWithSchema(schema, obj) {
if (propDef === false)
return
var jsonValue = propDef.serializer(obj[key], key, obj)
console.warn({schema, obj, key, value: obj[key], jsonValue})
if (jsonValue === SKIP){
return
}
Expand Down
15 changes: 9 additions & 6 deletions test/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ test("it should serialize simple object", t1 => {
t.end()
})

test("it should (de)serialize all fields in the schema even if they're not defined on the object (existing behavior)", t => {
test("it should serialize all fields in the schema even if they're not defined on the object (existing behavior)", t => {
var a = { y: 1337 }
var s = serialize(schema, a)

t.deepEqual(s, { x: undefined })
// Note that this behavior is only one-way: it doesn't set props as undefined on the deserialized object.
t.deepEqual(deserialize(schema, s), {})

var d = { x: 1 }
Expand Down Expand Up @@ -162,26 +163,28 @@ test("it should not serialize values for optional properties", t => {
var schema = {
factory: () => ({}),
props: {
x: optional(primitive())
optionalProp: optional(primitive()),
requiredProp: primitive()
}
}
var a = { y: 1337 }
var s = serialize(schema, a)

t.deepEqual(s, {})
t.deepEqual(s, {requiredProp: undefined})
// Note that this behavior is only one-way: it doesn't set props as undefined on the deserialized object.
t.deepEqual(deserialize(schema, s), {})

var d = { x: 1 }
var d = { optionalProp: 1 }
update(schema, a, d)
t.deepEqual(a, {
y: 1337, x: 1
y: 1337, optionalProp: 1
})

test("it should skip missing attrs", t3 => {
update(schema, a, {}, (err, res) => {
t3.ok(res === a)
t3.notOk(err)
t3.equal(res.x, 1)
t3.equal(res.optionalProp, 1)
t3.end()
})
})
Expand Down

0 comments on commit e717291

Please sign in to comment.