Permalink
Browse files

Fixing tests for default props noop

  • Loading branch information...
mfrachet committed Oct 31, 2017
1 parent b145ca5 commit 7c6d7aaaae5b370d17f8e0f5efb28fcee04fcbb4
Showing with 25 additions and 6 deletions.
  1. +20 −6 src/__tests__/reactNativeAbHoc.test.js
  2. +5 −0 src/reactNativeAbHoc.js
@@ -1,7 +1,7 @@
import React from 'react';
import { View, Text, AsyncStorage } from 'react-native';
import { shallow } from 'enzyme';
import ReactNativeAbHoc from './../reactNativeAbHoc';
import ReactNativeAbHoc, { noop } from './../reactNativeAbHoc';
const getWrapper = (Component, props = {}) => shallow(<Component {...props} />);
@@ -11,6 +11,7 @@ describe('ReactNativeAbHoc', () => {
let B;
let C;
let Component;
let spyOnSelectVariant;
beforeEach(() => {
const asyncMap = {};
@@ -30,6 +31,7 @@ describe('ReactNativeAbHoc', () => {
});
beforeEach(() => {
spyOnSelectVariant = jest.fn();
A = () => (
<View>
<Text>A</Text>
@@ -84,36 +86,48 @@ describe('ReactNativeAbHoc', () => {
});
describe('ReactNativeAbHoc#lifecycle', () => {
it('should return null on noop default function', () => expect(noop()).toEqual(null));
it('should render a forced component', async () => {
const wrapper = await getWrapper(Component, { variant: 'B' });
const wrapper = await getWrapper(Component, {
variant: 'B',
onVariantSelect: spyOnSelectVariant,
});
expect(AsyncStorage.setItem).toHaveBeenCalledWith('abhoc-variant-Experiment', 'B');
expect(spyOnSelectVariant).toHaveBeenCalledWith('B');
expect(wrapper.state()).toEqual({ variant: 'B', component: B });
expect(wrapper.find(View).prop('variant')).toEqual('B');
});
it('should render a random component', async (done) => {
Math.random = jest.fn().mockImplementation(() => 0.8);
const wrapper = await getWrapper(Component, { simpleProps: 'test' });
const wrapper = await getWrapper(Component, {
simpleProps: 'test',
onVariantSelect: spyOnSelectVariant,
});
expect(AsyncStorage.getItem).toHaveBeenCalledWith('abhoc-variant-Experiment');
expect(AsyncStorage.setItem).toHaveBeenCalledWith('abhoc-variant-Experiment', 'C');
expect(wrapper.find(View).prop('simpleProps')).toEqual('test');
setTimeout(() => {
expect(spyOnSelectVariant).toHaveBeenCalledWith('C');
expect(wrapper.state()).toEqual({ variant: 'C', component: C });
done();
}, 0);
});
it('should render a random (existing) component', async (done) => {
Math.random = jest.fn().mockImplementation(() => 0.8);
AsyncStorage.setItem('abhoc-variant-Experiment', 'C');
AsyncStorage.setItem.mockReset();
const wrapper = await getWrapper(Component, { simpleProps: 'test' });
const wrapper = await getWrapper(Component, {
simpleProps: 'test',
onVariantSelect: spyOnSelectVariant,
});
expect(AsyncStorage.getItem).toHaveBeenCalledWith('abhoc-variant-Experiment');
expect(AsyncStorage.setItem).not.toHaveBeenCalled();
expect(wrapper.find(View).prop('simpleProps')).toEqual('test');
setTimeout(() => {
expect(spyOnSelectVariant).toHaveBeenCalledWith('C');
expect(wrapper.state()).toEqual({ variant: 'C', component: C });
done();
}, 0);
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import { View, AsyncStorage } from 'react-native';
export const LOCAL_STORAGE_KEY = 'abhoc-variant';
export const noop = () => null;
export default (experiment, ...variants) => {
class AbHoc extends React.Component {
@@ -39,6 +40,7 @@ export default (experiment, ...variants) => {
async persistVariant({ variant, component }) {
await AsyncStorage.setItem(`${LOCAL_STORAGE_KEY}-${experiment}`, variant);
this.props.onVariantSelect(variant);
return this.setState({ variant, component });
}
@@ -49,6 +51,7 @@ export default (experiment, ...variants) => {
return this.persistVariant({ variant, component });
}
const localVariant = AbHoc.findVariant(localVariantKey);
this.props.onVariantSelect(localVariant.variant);
return this.setState({ ...localVariant });
}
@@ -61,10 +64,12 @@ export default (experiment, ...variants) => {
AbHoc.propTypes = {
variant: PropTypes.string,
onVariantSelect: PropTypes.func,
};
AbHoc.defaultProps = {
variant: null,
onVariantSelect: noop,
};
return AbHoc;

0 comments on commit 7c6d7aa

Please sign in to comment.