Skip to content

Commit

Permalink
working-tests-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Pollak committed Sep 18, 2019
1 parent 2ebdacd commit c8b5871
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
7 changes: 5 additions & 2 deletions src/core/deserialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ function deserializeStarProps(context, schema, propDef, obj, json) {
key + "': " + value)
obj[key] = value
} else if (propDef.pattern.test(key)) {
obj[key] = deserializeObjectWithSchema(context, propDef, value, context.callback, {})
var value = deserializeObjectWithSchema(context, propDef, value, context.callback || GUARDED_NOOP, {})
// deserializeObjectWithSchema returns undefined on error
if (value !== undefined) {
obj[key] = value;
}
}
}
}
Expand Down Expand Up @@ -138,7 +142,6 @@ export function deserializePropsWithSchema(context, modelSchema, json, target) {
deserializeProp(propDef, jsonValue, propName)
}
}

if (propName === "*") {
deserializeStarProps(context, modelSchema, propDef, target, json)
return
Expand Down
15 changes: 3 additions & 12 deletions src/core/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,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 All @@ -74,20 +73,13 @@ export function serializeWithSchema(schema, obj) {

export function serializeStarProps(schema, propDef, obj, target) {
checkStarSchemaInvariant(propDef)
for (var key in obj) {
let hasOwnProp = obj.hasOwnProperty(key)
//console.log({obj, key, hasOwnProp})
if (hasOwnProp) {
if (!(key in schema.props)) {
for (var key in obj) if (obj.hasOwnProperty(key)) if (!(key in schema.props)) {
let onlyPrimitives = propDef === true
let pattern = !onlyPrimitives && propDef.pattern
let matchesPattern = pattern && pattern.test(key)
//console.log({propDef, obj, key, pattern, onlyPrimitives, matchesPattern})
if (onlyPrimitives || matchesPattern) {
var value = obj[key]
//console.log({propDef, obj, key, value});
if (onlyPrimitives) {
// when serializing only serialize primitive props. Assumes other props (without schema) are local state that doesn't need serialization
if (isPrimitive(value)) {
target[key] = value
}
Expand All @@ -96,12 +88,11 @@ export function serializeStarProps(schema, propDef, obj, target) {
if (jsonValue === SKIP) {
return
}
target[/*propDef.jsonname ||*/ key] = jsonValue
// todo: propDef.jsonname could be a transform function on key
target[key] = jsonValue
}
}
}
}
}
}

/**
Expand Down
20 changes: 10 additions & 10 deletions test/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,40 +122,40 @@ test("it should respect `*` : true (primitive) prop schemas", t => {
t.end()
})

test("it should respect `*` : schema prop schemas", t => {
test.only("it should respect `*` : schema prop schemas", t => {
var schema = {
factory: () => ({}),
pattern: /^\d.\d+$/,
props: {
x: primitive()
x: optional(primitive())
}
}

var s = _.createSimpleSchema({ "*" : schema} )
t.deepEqual(_.serialize(s, { "1.0": {x: 42}, "2.10": {x: 17 } }), { "1.0": {x: 42}, "2.10": {x: 17 } })
t.deepEqual(_.deserialize(s, { "1.0": {x: 42}, "2.10": {x: 17 } }), { "1.0": {x: 42}, "2.10": {x: 17 } })

t.deepEqual(_.serialize(s, { a: new Date(), d: 2 }), { d: 2 })
t.deepEqual(_.serialize(s, { a: new Date(), d: 2 }), {})
t.deepEqual(_.serialize(s, { a: {}, "2.10": {x: 17 } }), { "2.10": {x: 17 }})

t.throws(() => _.deserialize(s, { "1.0": new Date() }), /encountered non primitive value while deserializing/)
t.throws(() => _.deserialize(s, { "1.0": {} }), /encountered non primitive value while deserializing/)
// deserialize silently ignores properties that aren't objects:
// if (json === null || json === undefined || typeof json !== "object")
// return void callback(null, null)
t.deepEqual(_.deserialize(s, { "1.0": "not an object" }), {})

var s2 = _.createSimpleSchema({
"*" : schema,
"1.0": _.date()
})
t.doesNotThrow(() => _.serialize(s2, { "1.0": new Date(), d: 2 }))
t.deepEqual(_.serialize(s2, { c: {}, d: 2 }), { "1.0": undefined, d: 2 })

t.doesNotThrow(() => _.deserialize(s2, { "1.0": new Date().getTime() }), /bla/)
t.throws(() => _.deserialize(s2, { c: {}, d: 2 }), /encountered non primitive value while deserializing/)
t.deepEqual(_.serialize(s2, { c: {}, "2.0": {x: 2} }), { "1.0": undefined, "2.0": {x: 2} })

// don't assign aliased attrs
var s3 = _.createSimpleSchema({
a: _.alias("1.0", true),
"*" : schema,
})
t.deepEqual(_.deserialize(s3, { b: 4, a: 5}), { a: 4 })
t.deepEqual(_.deserialize(s3, { b: 4, "1.0": 5, "2.0": {x: 2}}), { a: 5, "2.0": {x: 2}})


t.end()
Expand Down

0 comments on commit c8b5871

Please sign in to comment.