Skip to content

Commit

Permalink
Merge pull request #6 from JaapRood/fix/iterable-schema-options
Browse files Browse the repository at this point in the history
Fix the forwarding of options to item schema's with IterableSchema
  • Loading branch information
Jaap van Hardeveld authored Apr 3, 2017
2 parents fb814a7 + f0270bb commit 0fa4dc1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
10 changes: 8 additions & 2 deletions src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ internals.IterableSchema = function(iterable, itemSchema) {

_assign(this, {
getItemSchema: () => itemSchema,
factory: (val) => iterable(val).map(itemSchema.factory || internals.idenity),
serialize: (val) => val.map(itemSchema.serialize || internals.idenity).toJS()
factory: (val, ...otherArgs) => {
const factory = itemSchema.factory || internals.idenity
return iterable(val).map((item) => factory(item, ...otherArgs))
},
serialize: (val, ...otherArgs) => {
const serialize = itemSchema.serialize || internals.idenity
return val.map((item) => serialize(item, ...otherArgs)).toJS()
}
})
}

Expand Down
58 changes: 43 additions & 15 deletions test/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,20 @@ Test('Schema.isType', function(t) {
})

Test('Schema.listOf', function(t) {
t.plan(7)
t.plan(11)

const factoryOptions = { optionsFor: 'factory' }
const serializeOptions = { optionsFor: 'serialize' }

const itemType = {
factory: (val) => val + 'modified',
serialize: (val) => val.substr(0, val.lastIndexOf('modified')) + 'serialized'
factory: (val, options) => {
t.deepEqual(factoryOptions, options, 'options are forwarded to the item schema factory')
return val + 'modified'
},
serialize: (val, options) => {
t.deepEqual(serializeOptions, options, 'options are forwarded to the item schema serializer')
return val.substr(0, val.lastIndexOf('modified')) + 'serialized'
}
}
t.doesNotThrow(function() {
const listOfSchema = Schema.listOf(itemType)
Expand All @@ -31,24 +40,33 @@ Test('Schema.listOf', function(t) {
const expectedFactoryResult = Immutable.List(["somemodified", 'arraymodified'])
const expectedSerializeResult = ["someserialized", "arrayserialized"]

const factoryResult = listOfSchema.factory(source)
const factoryResult = listOfSchema.factory(source, factoryOptions)

t.ok(Immutable.List.isList(factoryResult), 'casts an array to a List')
t.ok(expectedFactoryResult.equals(factoryResult), 'maps each value of the array with the item schema `factory` method')

const serializeResult = listOfSchema.serialize(factoryResult)
const serializeResult = listOfSchema.serialize(factoryResult, serializeOptions)

t.ok(Array.isArray(serializeResult), 'serializes to an array')
t.deepEqual(serializeResult, expectedSerializeResult, 'maps each value of the List with the item schema `serialize` method')
}, 'accepts an Iterable constructor and type definition')
})

Test('Schema.setOf', function(t) {
t.plan(7)
t.plan(11)

const factoryOptions = { optionsFor: 'factory' }
const serializeOptions = { optionsFor: 'serialize' }

const itemType = {
factory: (val) => val + 'modified',
serialize: (val) => val.substr(0, val.lastIndexOf('modified')) + 'serialized'
factory: (val, options) => {
t.deepEqual(factoryOptions, options, 'options are forwarded to the item schema factory')
return val + 'modified'
},
serialize: (val, options) => {
t.deepEqual(serializeOptions, options, 'options are forwarded to the item schema serializer')
return val.substr(0, val.lastIndexOf('modified')) + 'serialized'
}
}
t.doesNotThrow(function() {
const setOfSchema = Schema.setOf(itemType)
Expand All @@ -60,25 +78,35 @@ Test('Schema.setOf', function(t) {
const expectedFactoryResult = Immutable.Set(["somemodified", 'arraymodified'])
const expectedSerializeResult = ["someserialized", "arrayserialized"]

const factoryResult = setOfSchema.factory(source)
const factoryResult = setOfSchema.factory(source, factoryOptions)

t.ok(Immutable.Set.isSet(factoryResult), 'casts an array to a Set')
t.ok(expectedFactoryResult.equals(factoryResult), 'maps each value of the array with the item schema `factory` method')

const serializeResult = setOfSchema.serialize(factoryResult)
const serializeResult = setOfSchema.serialize(factoryResult, serializeOptions)

t.ok(Array.isArray(serializeResult), 'serializes to an array')
t.deepEqual(serializeResult, expectedSerializeResult, 'maps each value of the Set with the item schema `serialize` method')
}, 'accepts an Iterable constructor and type definition')
})

Test('Schema.orderedSetOf', function(t) {
t.plan(7)
t.plan(11)

const factoryOptions = { optionsFor: 'factory' }
const serializeOptions = { optionsFor: 'serialize' }

const itemType = {
factory: (val) => val + 'modified',
serialize: (val) => val.substr(0, val.lastIndexOf('modified')) + 'serialized'
factory: (val, options) => {
t.deepEqual(factoryOptions, options, 'options are forwarded to the item schema factory')
return val + 'modified'
},
serialize: (val, options) => {
t.deepEqual(serializeOptions, options, 'options are forwarded to the item schema serializer')
return val.substr(0, val.lastIndexOf('modified')) + 'serialized'
}
}

t.doesNotThrow(function() {
const orderedSetOfSchema = Schema.orderedSetOf(itemType)

Expand All @@ -89,12 +117,12 @@ Test('Schema.orderedSetOf', function(t) {
const expectedFactoryResult = Immutable.OrderedSet(["somemodified", 'arraymodified'])
const expectedSerializeResult = ["someserialized", "arrayserialized"]

const factoryResult = orderedSetOfSchema.factory(source)
const factoryResult = orderedSetOfSchema.factory(source, factoryOptions)

t.ok(Immutable.OrderedSet.isOrderedSet(factoryResult), 'casts an array to an OrderedSet')
t.ok(expectedFactoryResult.equals(factoryResult), 'maps each value of the array with the item schema `factory` method')

const serializeResult = orderedSetOfSchema.serialize(factoryResult)
const serializeResult = orderedSetOfSchema.serialize(factoryResult, serializeOptions)

t.ok(Array.isArray(serializeResult), 'serializes to an array')
t.deepEqual(serializeResult, expectedSerializeResult, 'maps each value of the OrderedSet with the item schema `serialize` method')
Expand Down

0 comments on commit 0fa4dc1

Please sign in to comment.