Skip to content

Instantly share code, notes, and snippets.

@Keksike
Last active October 3, 2017 07:19
Show Gist options
  • Save Keksike/8afd4787f80c08e4e6d36c24a7886510 to your computer and use it in GitHub Desktop.
Save Keksike/8afd4787f80c08e4e6d36c24a7886510 to your computer and use it in GitHub Desktop.
react-native enzyme test
import React from 'react';
import createTestApp from '../testHelper';
import MyContainer from '../../app/modules/MyContainer';
describe('<MyContainer>', () => {
let view;
let store;
beforeEach(async () => {
const container = createTestApp(<MyContainer/>)
view = container.view;
store = container.store;
});
console.log(view);
console.log(store);
it('should test', () => {
expect('foo').toBe('foo');
});
});
// this file is ran before any other test files
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({adapter: new Adapter()});
// This will mutate `react-native`'s require cache with `react-native-mock-render`'s.
require('react-native-mock-render/mock');
// do this so we can use Enzyme mount with React-Native
// https://blog.joinroot.com/mounting-react-native-components-with-enzyme-and-jsdom/
const jsdom = require('jsdom').jsdom;
global.document = jsdom('');
global.window = document.defaultView;
Object.keys(document.defaultView).forEach((property) => {
if (typeof global[property] === 'undefined') {
global[property] = document.defaultView[property];
}
});
import React from 'react';
import Enzyme, {mount} from 'enzyme';
import {Provider} from 'react-redux';
import {applyMiddleware, createStore, compose} from 'redux';
import * as reduxLoop from 'redux-loop';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({adapter: new Adapter()});
import middleware from '../app/redux/middleware';
import reducer from '../app/redux/reducer';
const enhancer = compose(
applyMiddleware(...middleware),
reduxLoop.install()
);
export default function createTestApp(element, initialState) {
const store = createStore(reducer, initialState, enhancer);
const view = mount(
<Provider store={store}>
{element}
</Provider>
);
return {view, store};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment