Skip to content

Commit

Permalink
Add and test injecting defaults to use in a state factory and it's pa…
Browse files Browse the repository at this point in the history
…rse function
  • Loading branch information
JaapRood committed May 3, 2018
1 parent e5a8bcd commit 84462b9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/factory.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Invariant = require('invariant')
const Immutable = require('immutable')
const ShortId = require('shortid')
const _assign = require('lodash.assign')
const _isPlainObject = require('lodash.isplainobject')
const _isFunction = require('lodash.isfunction')
const _isArray = require('lodash.isarray')
Expand Down Expand Up @@ -35,11 +36,13 @@ internals.factory = function(rawEntity={}, options={}) {
, 'Raw entity, when passed, must be a plain object or Immutable Iterable')
Invariant(_isPlainObject(options), 'options, when passed, must be a plain object')
Invariant(!options.parse || _isFunction(options.parse), 'The `parse` prop of the options, when passed, must be a function')
Invariant(!options.defaults || exports.isDefaults(options.defaults), 'The `defaults` prop of the options, when passed, must be plain object or Immutable Iterable')

var parse = options.parse || this.parse || ((attrs) => attrs)
var defaults = Immutable.Map(options.defaults || this.defaults() || {})

// merge with with defaults and cast any nested native selections to Seqs
var entity = this.defaults().merge(Immutable.Map(rawEntity)).map((value, key) => {
var entity = defaults.merge(Immutable.Map(rawEntity)).map((value, key) => {
if (Immutable.Iterable.isIterable(value)) {
return value
}
Expand Down
28 changes: 25 additions & 3 deletions test/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Test('state.defaults', function(t) {
})

Test('state.factory', function(t) {
t.plan(6 + 3 + 2 + 1);
t.plan(6 + 3 + 2 + 1 + 3);


t.doesNotThrow(function() {
Expand Down Expand Up @@ -105,10 +105,23 @@ Test('state.factory', function(t) {
var state = State.create('test-state', { a: 1, b: 3});
state.factory.call(null)
}, 'can be called without context')

t.doesNotThrow(function() {
var state = State.create('test-state', { a: 1, b: 2})
var defaultsOption = { b: 3, c: {}, d: 5 }

var instance = state.factory({}, { defaults: defaultsOption })

t.deepEqual(
_.omit(instance.toJSON(), ['__typeName', '__cid']),
defaultsOption
, 'returned instance uses defaults passed as an option')
t.notOk(instance.has('a'), 'when defaults option is passed, state level defaults are not used')
}, 'accepts an object of defaults as an option')
});

Test('state.factory - parse', function(t) {
t.plan(10);
t.plan(12);



Expand All @@ -130,9 +143,18 @@ Test('state.factory - parse', function(t) {
f: Immutable.OrderedSet()
};

var testDefaults = {
a: 4
}

var instance = state.factory(rawInstance, {
parse: function(attrs) {
defaults: testDefaults,

parse: function(attrs, options) {
t.ok(Immutable.Map.isMap(attrs), 'parse function is called with attributes as a Map');
t.ok(_.isPlainObject(options), 'parse function is called with options');
t.deepEqual(options.defaults, testDefaults, 'options contains the defaults option passed to the factory');


return attrs.map(function(value, key) {
if (key === 'a') {
Expand Down

0 comments on commit 84462b9

Please sign in to comment.