From d6ede2a0e6d32084be541185a48382c3c7637c8a Mon Sep 17 00:00:00 2001 From: Craig Bilner Date: Thu, 28 Jan 2016 21:31:45 +0000 Subject: [PATCH 1/2] Add onChange example and test args --- src/index.js | 10 ++++++++- tests/stand-alone/map-to/on-change-tests.js | 25 +++++++++++++++++++++ tests/stand-alone/map-to/on-change.js | 17 ++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests/stand-alone/map-to/on-change-tests.js create mode 100644 tests/stand-alone/map-to/on-change.js diff --git a/src/index.js b/src/index.js index fd5867e..cf30591 100644 --- a/src/index.js +++ b/src/index.js @@ -26,21 +26,29 @@ const propFunc = function (propToTest) { return this; }; +const withArgs = function () { + this.mapTestArgs = [].slice.call(arguments); + return this; +}; + const mapsTo = function (method) { const symbolToTest = Symbol(method); this.reactClass.prototype[method].reset(); - this.props[this.propToTest](symbolToTest); + this.props[this.propToTest].apply(this.reactClass, this.mapTestArgs.concat(symbolToTest)); return this.reactClass.prototype[method].lastCall.args.indexOf(symbolToTest) > -1; }; const flavourComponentMethods = { propFunc, + withArgs, mapsTo, }; const flavourComponentInit = function (opts) { + this.mapTestArgs = []; + if (opts.instance.props) { this.style = opts.instance.props.style; diff --git a/tests/stand-alone/map-to/on-change-tests.js b/tests/stand-alone/map-to/on-change-tests.js new file mode 100644 index 0000000..f1f44d5 --- /dev/null +++ b/tests/stand-alone/map-to/on-change-tests.js @@ -0,0 +1,25 @@ +import assert from 'assert'; +import OnChangeComponent from './on-change'; +import ReactTester from '../../../src/index'; + +const tester = ReactTester.create().use(OnChangeComponent); +const MAP_TEST = tester.addFlavour('MAP_TEST', {}); + +describe('the OnChangeComponent should', () => { + it('map the onChange method to the inputs onChange provided test arguments', () => { + const actual = + MAP_TEST + .component + .propFunc('onChange') + .withArgs({ + target: { + name: 'test name', + value: 'test value', + }, + }) + .mapsTo('onChange'); + const expected = true; + + assert.deepEqual(actual, expected); + }); +}); diff --git a/tests/stand-alone/map-to/on-change.js b/tests/stand-alone/map-to/on-change.js new file mode 100644 index 0000000..f8b9271 --- /dev/null +++ b/tests/stand-alone/map-to/on-change.js @@ -0,0 +1,17 @@ +import React, { Component } from 'react'; + +export default class OnChangeComponent extends Component { + onChange(evt) { + const { name, value } = evt.target; + console.log(name, value); // eslint-disable-line no-console + } + + render() { + return ( + + ); + } +} From 88a496ad6fe37d07fb14c74f7c29b13d9ff12613 Mon Sep 17 00:00:00 2001 From: Craig Bilner Date: Thu, 28 Jan 2016 21:41:38 +0000 Subject: [PATCH 2/2] Update README --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cc31139..ef032f8 100644 --- a/README.md +++ b/README.md @@ -110,15 +110,17 @@ const expected = stampTypes.FIRST_CLASS; assert.deepEqual(actual, expected); ``` -### .propFunc(propName{string}), .mapsTo(methodName{string}) => isMapped{boolean} +### .propFunc(propName{string}), .withArgs(arguments{array}) [optional], .mapsTo(methodName{string}) => isMapped{boolean} -propFunc takes a string which is the prop of a dumb component to which you are passing a function +`propFunc` takes a string which is the prop of a dumb component to which you are passing a function -mapTo takes a string which maps to the method on the smart component class +`mapTo` takes a string which maps to the method on the smart component class + +if your `mapTo` method expects some specific arguments, these can be passed as an array using `withArgs` the returned value will be a boolean indicating if the given prop function maps to the expected class method -here we test that the stamp dumb component was correctly given the smart component's handleOnClick method +here we test that the stamp dumb component was correctly given the smart component's `handleOnClick` method ```javascript const isMapped =