Skip to content

Commit

Permalink
Test whether options are forwarded to the item schemas of IterableSch…
Browse files Browse the repository at this point in the history
…ema schema types
  • Loading branch information
JaapRood committed Apr 3, 2017
1 parent fb814a7 commit 62fbff0
Showing 1 changed file with 43 additions and 15 deletions.
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 62fbff0

Please sign in to comment.