Skip to content

Instantly share code, notes, and snippets.

@zhabinskiy
Created June 15, 2018 07:58
Show Gist options
  • Save zhabinskiy/a8ca6876484239df6067181f520770be to your computer and use it in GitHub Desktop.
Save zhabinskiy/a8ca6876484239df6067181f520770be to your computer and use it in GitHub Desktop.
Jest + Enzyme example from one of my recent projects
import React from 'react';
import { StaticRouter } from 'react-router';
import 'jest-styled-components';
import renderer from 'react-test-renderer';
import { configure, mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
/* Redux */
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import reducer from '../reducers';
/* Components */
import Room from '../index';
configure({ adapter: new Adapter() });
import {
room,
roomInArbitrationModeSingle,
roomInArbitrationModeMultiple,
unregistred,
authed,
captain,
admin,
match,
translations,
} from '../testData';
function generateStore(userState, roomState, translate) {
const preloadedState = {
user: userState,
tournamentsRoom: roomState,
locale: {
languages: [{ code: 'en', active: true }],
translations: translate,
options: {},
},
};
const store = createStore(reducer, preloadedState);
return store;
}
describe('Tournament Room', () => {
it('matches snapshot', () => {
const treeUnregistredState = renderer
.create(
<Provider store={generateStore(unregistred, room, translations)}>
<StaticRouter context={{}}>
<Room match={match} />
</StaticRouter>
</Provider>
)
.toJSON();
expect(treeUnregistredState).toMatchSnapshot();
const treeAuthedState = renderer
.create(
<Provider store={generateStore(authed, room, translations)}>
<StaticRouter context={{}}>
<Room match={match} />
</StaticRouter>
</Provider>
)
.toJSON();
expect(treeAuthedState).toMatchSnapshot();
const treeCaptainState = renderer
.create(
<Provider store={generateStore(captain, room, translations)}>
<StaticRouter context={{}}>
<Room match={match} />
</StaticRouter>
</Provider>
)
.toJSON();
expect(treeCaptainState).toMatchSnapshot();
const treeAdminState = renderer
.create(
<Provider store={generateStore(admin, room, translations)}>
<StaticRouter context={{}}>
<Room match={match} />
</StaticRouter>
</Provider>
)
.toJSON();
expect(treeAdminState).toMatchSnapshot();
});
it('Unregistred@Room w/o Admin UI', () => {
const wrapper = mount(
<Provider store={generateStore(unregistred, room, translations)}>
<StaticRouter context={{}}>
<Room match={match} />
</StaticRouter>
</Provider>
);
expect(wrapper.find('AdminControls')).toHaveLength(0);
expect(wrapper.find('GameStatus')).toHaveLength(0);
expect(wrapper.find('PreviousGames')).toHaveLength(0);
});
it('Authed@Room w/o Admin UI', () => {
const wrapper = mount(
<Provider store={generateStore(authed, room, translations)}>
<StaticRouter context={{}}>
<Room match={match} />
</StaticRouter>
</Provider>
);
expect(wrapper.find('AdminControls')).toHaveLength(0);
expect(wrapper.find('GameStatus')).toHaveLength(0);
expect(wrapper.find('PreviousGames')).toHaveLength(0);
});
it('Captain@Room w/o Admin UI', () => {
const wrapper = mount(
<Provider store={generateStore(captain, room, translations)}>
<StaticRouter context={{}}>
<Room match={match} />
</StaticRouter>
</Provider>
);
expect(wrapper.find('AdminControls')).toHaveLength(0);
expect(wrapper.find('GameStatus')).toHaveLength(0);
expect(wrapper.find('PreviousGames')).toHaveLength(0);
});
it('Admin@Room w/ Admin UI', () => {
const wrapper = mount(
<Provider store={generateStore(admin, room, translations)}>
<StaticRouter context={{}}>
<Room match={match} />
</StaticRouter>
</Provider>
);
expect(wrapper.find('AdminControls')).toHaveLength(1);
expect(wrapper.find('GameStatus')).toHaveLength(0);
expect(wrapper.find('PreviousGames')).toHaveLength(0);
});
it('Admin@Room in Arbitration Mode', () => {
const wrapper = mount(
<Provider
store={generateStore(admin, roomInArbitrationModeSingle, translations)}
>
<StaticRouter context={{}}>
<Room match={match} />
</StaticRouter>
</Provider>
);
expect(wrapper.find('AdminControls')).toHaveLength(1);
expect(wrapper.find('GameStatus')).toHaveLength(1);
expect(wrapper.find('PreviousGames')).toHaveLength(0);
});
it('Admin@Room in Arbitration Mode + Previous Games', () => {
const wrapper = mount(
<Provider
store={generateStore(
admin,
roomInArbitrationModeMultiple,
translations
)}
>
<StaticRouter context={{}}>
<Room match={match} />
</StaticRouter>
</Provider>
);
expect(wrapper.find('AdminControls')).toHaveLength(1);
expect(wrapper.find('GameStatus')).toHaveLength(1);
expect(wrapper.find('PreviousGames')).toHaveLength(1);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment