Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
25 changes: 25 additions & 0 deletions tests/stand-alone/map-to/on-change-tests.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
17 changes: 17 additions & 0 deletions tests/stand-alone/map-to/on-change.js
Original file line number Diff line number Diff line change
@@ -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 (
<input
name="username"
onChange={this.onChange}
/>
);
}
}