rexk / mocha-mix Goto Github PK
View Code? Open in Web Editor NEWAn easy way to test and mock your React Components.
License: Other
An easy way to test and mock your React Components.
License: Other
Mocking with withContext
introduces extra complexity for mocha-mix with very few benefits.
If the Components needs to be tested with Context Object, recommend users to create ContextWrapperComponent for test components and use them with render
method
import FluxibleComponent from 'fluxible-addons-react/FluxibleComponent';
import {createMockComponentContext} from 'fluxible/utils';
import assert from 'assert';
import MochaMix, { render } from 'mocha-mix';
// do all describe and before and after
...
var context = createMockComponentContext({ stores: [MockStore] });
var fluxibleContext = createMockComponentContext({ stores: [MockStore] });
var TestComponent = MochaMix.mix({
require: './TestComponent'
});
var rendered = render(
<FluxbileComponent context={context}>
<TestComponent { ...props } />
</FluxibleComponent>
);
assert.ok(rendered);
...
Above approach gives following advantages.
Examples are great way to start the module, but extensive API documentation can help out more advanced use cases.
Key phrase to consider:
Use either Code Climate or Coveralls for code coverage report
assertNotRender throws TypeError: Cannot read property 'replace' of undefined
, if it is used with as follows:
MochaMix.assertNotRender(
<DataGridHeader />,
'datagrid-td datagrid-flex'
);
This is due to
/**
* Assert component render to not matches the unexpectedHTML
* @method assertNotRender
*
* @param {ReactComponent} Component class
* @param {Object} props used to initialize the component
* @param {String} unexpectedHTML can contain wildcards "+" or "*"
*
* @example
* // The code bellow will raise an error if the component renders
* // a div with any attributes and the string "something" inside any
* // inner element.
* assertNotRender(MyComponent, {}, "<div+>*something*</div>");
**/
function assertNotRender(Component, props, unexpectedHTML) {
if (isUndefined(unexpectedHTML)) {
expectedHTML = props; // wrong value assignment
props = undefined;
}
var expected = renderToString(Component, props);
var regEx = buildRegEx(unexpectedHTML);
assert(
!regEx.test(expected),
'Expected: ' + expected + ' to NOT match: ' + regEx
);
}
module.exports = {
renderMatch: assertRender,
renderNotMatch: assertNotRender
};
Fix it ASAP.
useCleanCache: false
causes modules to use old cached module when test are conducted across multiple files.
React shallowRenderer returns a output with following code.
https://github.com/facebook/react/blob/master/src/test/ReactTestUtils.js#L351-L357
Write down, everyone who helped mocha-mix for 2.0
https://github.com/rexk/mocha-mix/blob/master/lib/mocha-mix.js#L56
following change will allow all simulated events to be captured through mock classes
function createStubReactClass(tagName, name) {
return React.createClass({
displayName: (name || '') + ' stub',
render: function () {
return React.createElement(tagName);
}
});
}
function createStubReactClass(tagName, name) {
return React.createClass({
displayName: (name || '') + ' stub',
render: function () {
return React.createElement(
tagName,
this.props,
this.props.children
);
}
});
}
import MochaMix from 'mocha-mix';
MochaMix.mix({
require: './MyComponent',
mocks: {
SubComp1: './SubComp1',
SubComp2: {
require: './SubComp2',
tagName: 'input'
},
lodash: {
require: 'lodash',
react: false,
mock: function() { console.log('I am lodash mock') }
}
}
});
import MochaMix, { generateStub } from 'mocha-mix';
import MyMockComponent from './my-mock';
MochaMix.mix({
require: './MyComponent',
mocks: {
SubComp1: './SubComp1',
SubComp2: {
require: './SubComp2',
tagName: 'input'
},
SubComp3: {
require: './SubComp3',
// provide simple stub generator component for further flexibility
mock: generateStub('INPUT')
},
SubComp4: {
require: './SubComp4',
mock: MyMockComponent
},
lodash: {
require: 'lodash',
// no need to provide react flag
// if mock variable is provided, it will override default stub component (div)
mock: function() { console.log('I am lodash mock') }
}
}
});
UPDATE REAMD.md to explain context helper.
Right now, all react stubs are div
elements.
Add an ability to change tags other than div
so that it is easier to simulate events.
In order to support, es6 module system, I have two possible interface in mind.
var mix = MochaMix.mix({
require: './MyComponent'
});
// equivalent to import MyComponent from './MyComponent';
var MyComponent = mix.import({ wildCard: true });
// equivalent to import * as MyWildCardComponent from './MyComponent';
var MyWildCardComponent = mix.import({ wildCard: true });
var mix = MochaMix.mix({
require: './MyComponent'
});
// equivalent to import MyComponent from './MyComponent';
var MyComponent = mix.import();
// equivalent to import * as MyWildCardComponent from './MyComponent';
var MyWildCardComponent = mix.importWildCard();
Following suggestions from #18
https://github.com/rstacruz/mocha-jsdom/blob/master/index.js#L92-L106
Adopting following snippet will allow mocha-mix to run on any version of jsdom.
As a result, mocha-mix will be allow to run iojs, nodejs <= 0.12.x, and node.js >= 4.x
mockery
's useCleanCache
option causes componentWillMount to cause
TypeError: Cannot read property '_currentElement' of null
Probably due to _currentElement being loaded using require statement.
Proposal:
disable useCleanCache
from before
provide afterEach
with mockery.resetCache()
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.