-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
197 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/data-point/lib/entity-types/__snapshots__/factory.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`creating a model with createEntityFactory should create an entity factory that returns an object with the expected shape 1`] = ` | ||
Object { | ||
"model:nuts": Object { | ||
"value": [Function], | ||
}, | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* @param {String} _type | ||
* @return {Function} | ||
*/ | ||
function createEntityFactory (_type) { | ||
const type = _type.toLowerCase() | ||
/** | ||
* @param {String} name | ||
* @param {Object} spec | ||
* @return {Object} | ||
*/ | ||
function Factory (name, spec) { | ||
const id = `${type}:${name}` | ||
const result = { [id]: spec } | ||
// these properties are non-enumerable | ||
// so that we can do nifty things like: | ||
// dataPoint.create({ entities: { ...Hash('name', spec) } }) | ||
Object.defineProperty(result, 'id', { value: id }) | ||
Object.defineProperty(result, 'type', { value: type }) | ||
Object.defineProperty(result, 'name', { value: name }) | ||
Object.defineProperty(result, 'spec', { value: spec }) | ||
return result | ||
} | ||
|
||
Factory.type = type | ||
return Factory | ||
} | ||
|
||
module.exports.createEntityFactory = createEntityFactory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* eslint-env jest */ | ||
|
||
const { createEntityFactory } = require('./factory') | ||
|
||
describe('creating a model with createEntityFactory', () => { | ||
const factory = createEntityFactory('model') | ||
const spec = { value: input => input } | ||
const instance = factory('nuts', spec) | ||
|
||
test('createEntityFactory return value should be a function', () => { | ||
expect(createEntityFactory).toBeInstanceOf(Function) | ||
}) | ||
|
||
test('createEntityFactory return value should have the correct "type" property', () => { | ||
expect(factory.type).toBe('model') | ||
}) | ||
|
||
test('should create an entity factory that returns an object with the expected shape', () => { | ||
// the 'model:nuts' key should appear in this snapshot, | ||
// but other keys shouldn't because they're non-enumerable | ||
expect(instance).toMatchSnapshot() | ||
}) | ||
|
||
test('model should have the correct "id" property', () => { | ||
expect(instance.id).toBe('model:nuts') | ||
}) | ||
|
||
test('model should have the correct "type" property', () => { | ||
expect(instance.type).toBe('model') | ||
}) | ||
|
||
test('model should have the correct "name" property', () => { | ||
expect(instance.name).toBe('nuts') | ||
}) | ||
|
||
test('model should have the correct "spec" property', () => { | ||
expect(instance.spec).toEqual(spec) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const mapValues = require('lodash/mapValues') | ||
|
||
const { createEntityFactory } = require('./factory') | ||
|
||
const definitions = { | ||
Entry: require('./entity-entry'), | ||
Model: require('./entity-model'), | ||
Reducer: require('./entity-transform'), | ||
Collection: require('./entity-collection'), | ||
Hash: require('./entity-hash'), | ||
Request: require('./entity-request'), | ||
Control: require('./entity-control'), | ||
Schema: require('./entity-schema') | ||
} | ||
|
||
module.exports = { | ||
definitions, | ||
factories: mapValues(definitions, (value, key) => { | ||
return createEntityFactory(key.toLowerCase()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters