Skip to content

Commit

Permalink
✨ (payloadWrapper) helpers to wrappe middlewar
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumecrespel committed Jul 11, 2017
1 parent 3dd13fd commit c286384
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 2 deletions.
2 changes: 2 additions & 0 deletions helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions helpers.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions helpers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "trampss-redux-factory/helpers",
"private": true,
"main": "../helpers.js"
}
27 changes: 27 additions & 0 deletions misc/rollup.config.helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import path from 'path'
import fs from 'fs'
import babel from 'rollup-plugin-babel'
import commonjs from 'rollup-plugin-commonjs'
import uglify from 'rollup-plugin-uglify'

const pkg = JSON.parse(fs.readFileSync('./package.json'))
const external = [
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
]

export default {
entry: pkg['jsnext:main'] || `src/${pkg.helpers}`,
dest: pkg.helpers,
sourceMap: path.resolve(pkg.helpers),
moduleName: pkg.amdName || pkg.name,
format: process.env.FORMAT || 'umd',
external,
plugins: [
babel(),
commonjs({
include: 'node_modules/**',
}),
uglify(),
],
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "trampss-redux-factory",
"version": "2.0.1",
"version": "2.1.0",
"main": "index.js",
"helpers": "helpers.js",
"license": "MIT",
"peerDependencies": {
"lodash": "^4.17.4"
Expand Down Expand Up @@ -34,7 +35,9 @@
"scripts": {
"lint:js": "eslint --ext js,jsx src",
"lint": "npm-run-all --parallel lint:*",
"build": "cross-env NODE_ENV=build rollup -c ./misc/rollup.config.js",
"build:core": "cross-env NODE_ENV=build rollup -c ./misc/rollup.config.js",
"build:helpers": "cross-env NODE_ENV=build rollup -c ./misc/rollup.config.helpers.js",
"build": "npm-run-all --parallel build:*",
"test": "jest",
"coveralls": "jest --coverage && cat ./coverage/lcov.info | coveralls"
},
Expand Down
41 changes: 41 additions & 0 deletions src/__snapshots__/helpers.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`helpers/payloadWrapper should not wrappe without wrapper function 1`] = `
Object {
"action": Object {
"payload": "payload",
"type": "TYPE",
},
"state": Object {},
}
`;

exports[`helpers/payloadWrapper should wrappe payload 1`] = `
Object {
"action": Object {
"payload": "PAYLOAD WRAPPED !",
"type": "TYPE",
},
"state": Object {},
}
`;

exports[`helpers/payloadWrapper should wrappe without type 1`] = `
Object {
"action": Object {
"payload": "PAYLOAD WRAPPED !",
"type": "TYPE",
},
"state": Object {},
}
`;

exports[`helpers/payloadWrapper should wrappe without type and wrapper function 1`] = `
Object {
"action": Object {
"payload": "payload",
"type": "TYPE",
},
"state": Object {},
}
`;
13 changes: 13 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const payloadWrapper = actionMatches => wrapper => () => () => (ctx) => {
const { payload, type } = ctx.action
if (!actionMatches || actionMatches.test(type)) {
return {
...ctx,
action: {
...ctx.action,
payload: wrapper ? wrapper(payload) : payload,
},
}
}
return ctx
}
20 changes: 20 additions & 0 deletions src/helpers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-env jest */
import { payloadWrapper } from './helpers'

const ctx = {
action: {
payload: 'payload',
type: 'TYPE',
},
state: {},
}

const actionMatches = /TYPE/
const wrapper = () => 'PAYLOAD WRAPPED !'

describe('helpers/payloadWrapper', () => {
it('should wrappe without type and wrapper function', () => expect(payloadWrapper()()()()(ctx)).toMatchSnapshot())
it('should not wrappe without wrapper function', () => expect(payloadWrapper(actionMatches)()()()(ctx)).toMatchSnapshot())
it('should wrappe without type', () => expect(payloadWrapper()(wrapper)()()(ctx)).toMatchSnapshot())
it('should wrappe payload', () => expect(payloadWrapper(actionMatches)(wrapper)()()(ctx)).toMatchSnapshot())
})

0 comments on commit c286384

Please sign in to comment.