-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(data-point/reducer-types): Add the ReducerConstant type (#173)
closes #173
- Loading branch information
1 parent
d1515c0
commit 7a95d38
Showing
22 changed files
with
611 additions
and
161 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const assert = require('assert') | ||
const mock = require('./entity-request-options.mock') | ||
|
||
const DataPoint = require('..') | ||
|
||
const c = DataPoint.helpers.constant | ||
const dataPoint = DataPoint.create() | ||
|
||
dataPoint.addEntities({ | ||
'request:searchPeople': { | ||
url: 'https://swapi.co/api/people', | ||
// options is a "ReducerObject", but | ||
// values at any level can be wrapped | ||
// as constants (or just wrap the whole | ||
// object if all the values are static) | ||
options: { | ||
'content-type': c('application/json'), // constant | ||
qs: { | ||
// get path `searchTerm` from input | ||
// to dataPoint.resolve | ||
search: '$searchTerm' | ||
} | ||
} | ||
} | ||
}) | ||
|
||
// this will mock the remote service | ||
mock() | ||
|
||
const input = { | ||
searchTerm: 'r2' | ||
} | ||
|
||
// the second parameter to transform is the input value | ||
dataPoint.resolve('request:searchPeople', input).then(output => { | ||
assert.equal(output.results[0].name, 'R2-D2') | ||
console.dir(output, { colors: true }) | ||
}) |
19 changes: 19 additions & 0 deletions
19
packages/data-point/examples/entity-request-options.mock.js
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,19 @@ | ||
const nock = require('nock') | ||
|
||
module.exports = () => { | ||
nock('https://swapi.co') | ||
.get('/api/people') | ||
.query({ | ||
search: 'r2' | ||
}) | ||
.reply(200, { | ||
count: 1, | ||
next: null, | ||
previous: null, | ||
results: [ | ||
{ | ||
name: 'R2-D2' | ||
} | ||
] | ||
}) | ||
} |
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
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
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
33 changes: 33 additions & 0 deletions
33
packages/data-point/lib/reducer-types/reducer-helpers/reducer-constant/factory.js
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,33 @@ | ||
const REDUCER_CONSTANT = 'ReducerConstant' | ||
|
||
module.exports.type = REDUCER_CONSTANT | ||
|
||
const HELPER_NAME = 'constant' | ||
|
||
module.exports.name = HELPER_NAME | ||
|
||
/** | ||
* @class | ||
* @property {string} type | ||
* @property {*} value | ||
*/ | ||
function ReducerConstant () { | ||
this.type = REDUCER_CONSTANT | ||
this.value = undefined | ||
} | ||
|
||
module.exports.ReducerConstant = ReducerConstant | ||
|
||
/** | ||
* @param {Function} createReducer | ||
* @param {*} value | ||
* @return {ReducerConstant} | ||
*/ | ||
function create (createReducer, value) { | ||
const reducer = new ReducerConstant() | ||
reducer.value = value | ||
|
||
return reducer | ||
} | ||
|
||
module.exports.create = create |
23 changes: 23 additions & 0 deletions
23
packages/data-point/lib/reducer-types/reducer-helpers/reducer-constant/factory.test.js
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,23 @@ | ||
/* eslint-env jest */ | ||
|
||
const Factory = require('./factory') | ||
const createReducer = require('../../index').create | ||
|
||
describe('ReducerConstant#factory', () => { | ||
test('reducer with undefined as value', () => { | ||
const reducer = Factory.create(createReducer) | ||
expect(reducer).toBeInstanceOf(Factory.ReducerConstant) | ||
expect(reducer.type).toBe(Factory.type) | ||
expect(reducer.value).toBe(undefined) | ||
}) | ||
test('reducers with different value types', () => { | ||
expect(Factory.create(createReducer, null).value).toBe(null) | ||
expect(Factory.create(createReducer, 100).value).toBe(100) | ||
expect(Factory.create(createReducer, 'string').value).toBe('string') | ||
expect(Factory.create(createReducer, ['a', 'b']).value).toEqual(['a', 'b']) | ||
expect(Factory.create(createReducer, { a: 1, b: 2 }).value).toEqual({ | ||
a: 1, | ||
b: 2 | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.