Skip to content
This repository has been archived by the owner on Feb 19, 2023. It is now read-only.

Commit

Permalink
feat(#105): added test cases for ListView and Container
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Gasser committed May 6, 2019
1 parent d598f2d commit 16ca23c
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/images/detail/__tests__/Container.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getImage } from '../../../redux/images';

const { select, mapDispatchToProps, mapKeyToValue } = __testables__;

describe('auth register container test suite', () => {
describe('image details container test suite', () => {
const expectedInitialState = {
image: undefined,
labels: [],
Expand Down
5 changes: 5 additions & 0 deletions src/images/list/Container.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ const mapDispatchToProps = ({
listImages,
});

export const __testables__ = {
select,
mapDispatchToProps,
};

export default connect(select, mapDispatchToProps)(ListView);
1 change: 0 additions & 1 deletion src/images/list/ListView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ ListView.propTypes = {
PropTypes.shape({})
).isRequired,
addImageRequest: HOCRequestPropTypes.isRequired,
listImages: PropTypes.func.isRequired,
};

ListView.defaultProps = {};
Expand Down
38 changes: 38 additions & 0 deletions src/images/list/__tests__/Container.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { __testables__ } from '../Container';
import { listImages } from '../../../redux/images';

const { select, mapDispatchToProps, mapKeyToValue } = __testables__;

describe('images list container test suite', () => {
const expectedInitialState = {
images: [],
addImageRequest: undefined,
};

const initialProps = {
match: {
params: {
id: null,
},
},
location: {
search: '',
},
};

it('should return container state', () => {
const state = select({}, initialProps);
expect(state).toEqual(expectedInitialState);

expect(Object.keys(state)).toEqual([
'images',
'addImageRequest',
]);
});

it('should return redux actions', () => {
expect(mapDispatchToProps).toEqual({
listImages,
});
});
});
94 changes: 94 additions & 0 deletions src/images/list/__tests__/ListView.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* globals testUtils */
import React from 'react';
import { mount, shallow } from 'enzyme';
import { Provider } from 'react-redux';
import toJson from 'enzyme-to-json';
import 'jest-styled-components';

import ListView from '../ListView';
import ImageList from '../ImageList';
import AddImageButton from '../../AddImageButton';

import * as Paths from '../../../paths';

const { createMockStore } = testUtils;

describe('ListView test suite', () => {

const initialProps = {
history: {
push: jest.fn(),
},
images: [],
onImageClick: jest.fn(),
addImageRequest: {
error: null,
loading: false,
},
};

const mockStore = createMockStore();

afterEach(() => {
// clear mocks
initialProps.history.push.mockClear();
initialProps.onImageClick.mockClear();
});

const getListView = (props) => {
const store = mockStore();
const provider = mount(
<Provider store={store}>
<ListView
{...initialProps}
{...props}
/>
</Provider>
);

const wrapper = provider.find(ListView);
return wrapper;
};

it('should render', () => {
const wrapper = getListView();
expect(wrapper.exists()).toBeTruthy();
});

it('should render AddImageButton', () => {
const wrapper = getListView();
expect(wrapper.exists()).toBeTruthy();

const addImageButton = wrapper.find(AddImageButton);
expect(addImageButton.exists()).toBeTruthy();
});

it('should render ImageList with props', () => {
const wrapper = getListView();
expect(wrapper.exists()).toBeTruthy();

const imageList = wrapper.find(ImageList);
expect(imageList.exists()).toBeTruthy();

const props = imageList.props();
expect(props.images).toEqual(initialProps.images);
expect(props.addImageRequest).toEqual(initialProps.addImageRequest);
});

it('should move to image detail view after image upload', () => {
const imageId = '2a2bdf23-e73f-4a2a-913e-da29926a195c';
const wrapper = getListView();
expect(wrapper.exists()).toBeTruthy();

const imageList = wrapper.find(ImageList);
expect(imageList.exists()).toBeTruthy();

expect(initialProps.history.push).not.toHaveBeenCalled();

// fire event
imageList.props().onImageClick({ id: imageId });

expect(initialProps.history.push).toHaveBeenCalled();
expect(initialProps.history.push).toHaveBeenCalledWith(Paths.GET_IMAGES_DETAIL(imageId));
});
});
2 changes: 1 addition & 1 deletion src/redux/images/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const imagesStateSelector = state => state.images || {};
export const imagesListSelector = createSelector(
imagesStateSelector,
({ ids, byId }) => {
if (ids.length === 0) {
if (!ids || !byId || ids.length === 0) {
return [];
}

Expand Down

0 comments on commit 16ca23c

Please sign in to comment.