Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add enableReducers and renderProp option to TestContext to help with integration testing #2614

Merged
merged 1 commit into from
Dec 4, 2018
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
36 changes: 36 additions & 0 deletions docs/UnitTesting.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,39 @@ You can then provide additional props, as needed, to your component (such as the

At this point, your component should `mount` without errors and you can unit test your component.


## Enabling reducers to ensure actions are dispatched

If you component relies on a a reducer, e.g. redux-form submission, you can enable reducers using the `enableReducers` prop:

```jsx harmony
myCustomEditView = mount(
<TestContext enableReducers>
<MyCustomEditView />
</TestContext>
);
```

This means that reducers will work as they will within the app. For example, you can now submit a form and redux-form will cause a re-render of your component.


## Spying on the store 'dispatch'

If you are using `mapDispatch` within connected components, it is likely you will want to test that actions have been dispatched with the correct arguments. You can return the `store` being used within the tests using a `renderProp`.

```jsx harmony
let dispatchSpy;
myCustomEditView = mount(
<TestContext>
{({ store }) => {
dispatchSpy = jest.spyOn(store, 'dispatch');
peter-mouland marked this conversation as resolved.
Show resolved Hide resolved
return <MyCustomEditView />
}}
</TestContext>,
);

it('should send the user to another url', () => {
myCustomEditView.find('.next-button').simulate('click');
expect(dispatchSpy).toHaveBeenCalledWith(`/next-url`);
});
```
58 changes: 39 additions & 19 deletions packages/ra-core/src/util/TestContext.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import React from 'react';
import PropTypes from 'prop-types';
import { createStore } from 'redux';
import { createStore, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import { reducer as formReducer } from 'redux-form';
import { TranslationProvider } from 'ra-core';
import merge from 'lodash/merge';

const defaultStore = {
admin: {
resources: {},
references: { possibleValues: {} },
ui: { viewVersion: 1 },
},
form: formReducer(),
i18n: { locale: 'en', messages: {} },
import createAdminStore from '../createAdminStore';

export const defaultStore = {
admin: {
resources: {},
references: { possibleValues: {} },
ui: { viewVersion: 1 },
},
form: formReducer(),
i18n: { locale: 'en', messages: {} },
};

/**
Expand All @@ -28,23 +30,41 @@ const defaultStore = {
* <Show {...defaultShowProps} />
* </AdminContext>
* );
*
* @example
* // in an enzyme test, using jest.
* const wrapper = render(
* <AdminContext store={{ admin: { resources: { post: { data: { 1: {id: 1, title: 'foo' } } } } } }}>
* {({ store }) => {
* dispatchSpy = jest.spyOn(store, 'dispatch');
* return <Show {...defaultShowProps} />
* }}
* </AdminContext>
* );
*/
const TestContext = ({ store, children }) => {
const storeWithDefault = createStore(() => merge(store, defaultStore));
return (
<Provider store={storeWithDefault}>
<TranslationProvider>{children}</TranslationProvider>
</Provider>
);
const TestContext = ({ store, enableReducers, children }) => {
const storeWithDefault = enableReducers
? createAdminStore({ initialState: merge(defaultStore, store) })
: createStore(() => merge(defaultStore, store));

const renderChildren = () => (typeof children === 'function' ? children({ store: storeWithDefault }) : children);

return (
<Provider store={storeWithDefault}>
<TranslationProvider>{renderChildren()}</TranslationProvider>
</Provider>
);
};

TestContext.propTypes = {
store: PropTypes.object,
children: PropTypes.node,
store: PropTypes.object.isRequired,
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
enableReducers: PropTypes.bool,
};

TestContext.defaultProps = {
store: {},
store: {},
enableReducers: false,
};

export default TestContext;
100 changes: 100 additions & 0 deletions packages/ra-core/src/util/TestContext.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import assert from 'assert';
import { shallow, mount } from 'enzyme';
import React from 'react';
import { submit } from 'redux-form';

import TestContext, { defaultStore } from './TestContext';

const primedStore = {
"admin": {
"auth": {
"isLoggedIn": false,
},
"loading": 0,
"notifications": [],
"record": {},
"references": {
"oneToMany": {},
"possibleValues": {},
},
"resources": {},
"saving": false,
"ui": {
"viewVersion": 1,
},
},
"form": {},
"i18n": {
"loading": false,
"locale": "en",
"messages": {},
},
"router": {
"location": null,
},
}

describe('TestContext.js', () => {
let testStore;

it('should render the given children', () => {
const component = shallow(<TestContext><span>foo</span></TestContext>);
assert.equal(component.html(), '<span>foo</span>');
});

it('should return a default store as a renderProp', () => {
let component = shallow(
<TestContext>
{({ store }) => {
testStore = store;
return <span>foo</span>;
}}
</TestContext>
);
assert.equal(component.html(), '<span>foo</span>');
assert.equal(typeof testStore, 'object');
assert.equal(typeof testStore.dispatch, 'function');
assert.deepStrictEqual(testStore.getState(), defaultStore);
});

describe('enableReducers options', () => {
it('should update the state when set to TRUE', () => {
shallow(
<TestContext enableReducers={true}>
{({ store }) => {
testStore = store;
return <span>foo</span>;
}}
</TestContext>
);
assert.deepStrictEqual(testStore.getState(), primedStore);

testStore.dispatch(submit('foo'));

assert.deepStrictEqual(testStore.getState(), {
...primedStore,
"form": {
"foo": {
"triggerSubmit": true,
},
},
});
});

it('should NOT update the state when set to FALSE (default)', () => {
shallow(
<TestContext >
{({ store }) => {
testStore = store;
return <span>foo</span>;
}}
</TestContext>
);
assert.deepStrictEqual(testStore.getState(), defaultStore);

testStore.dispatch(submit('foo'));

assert.deepStrictEqual(testStore.getState(), defaultStore);
});
});
});