Skip to content

Commit

Permalink
Refactoring test for GameOfLifeWithRedux
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Kremnev committed Oct 27, 2020
1 parent 1ecf307 commit 4d51d49
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 183 deletions.
164 changes: 87 additions & 77 deletions src/modules/GameOfLifeWithRedux/GameOfLifeWithRedux.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,31 @@ import { createStore } from 'redux';
import configureStore from 'redux-mock-store';
import { cellGridFillRandom } from './helpers';

describe('GameOfLifeWithRedux with mocked store', () => {
const mockStore = configureStore([]);
let store: any;
describe('Testing component GameOfLifeWithRedux', () => {
describe('GameOfLifeWithRedux with mocked store', () => {
const mockStore = configureStore([]);
let store: any;

beforeEach(() => {
store = mockStore({
game: {
board: cellGridFillRandom(20, 20, () => false),
gameStatus: false,
value: '500',
rows: 20,
columns: 20,
},
beforeEach(() => {
store = mockStore({
game: {
board: cellGridFillRandom(20, 20, () => false),
gameStatus: false,
value: '500',
rows: 20,
columns: 20,
},
});
});
});

it('Should generate action on click', () => {
const wrapper = mount(
<Provider store={store}>
<GameOfLifeWithRedux />
</Provider>,
);
(wrapper.find('Field').props() as any).onClick(100, 999);
expect(store.getActions()).toMatchInlineSnapshot(`
it('Should generate actions setCell on click', () => {
const wrapper = mount(
<Provider store={store}>
<GameOfLifeWithRedux />
</Provider>,
);
(wrapper.find('Field').props() as any).onClick(100, 999);
expect(store.getActions()).toMatchInlineSnapshot(`
Array [
Object {
"payload": Object {
Expand All @@ -41,37 +42,37 @@ describe('GameOfLifeWithRedux with mocked store', () => {
},
]
`);
});
});
});

describe('ReduxScreen with real store', () => {
let store: any;
describe('GameOfLifeWithRedux with real store', () => {
let store: any;

beforeEach(() => {
store = createStore(reducer, {
game: {
board: cellGridFillRandom(20, 20, () => false),
gameStatus: false,
value: '500',
rows: 20,
columns: 20,
},
beforeEach(() => {
store = createStore(reducer, {
game: {
board: cellGridFillRandom(20, 20, () => false),
gameStatus: false,
value: '500',
rows: 20,
columns: 20,
},
});
jest.spyOn(store, 'dispatch');
});
jest.spyOn(store, 'dispatch');
});

it('should generate action on click', () => {
const wrapper = mount(
<Provider store={store}>
<GameOfLifeWithRedux />
</Provider>,
);
it('should generate action setCell on click', () => {
const wrapper = mount(
<Provider store={store}>
<GameOfLifeWithRedux />
</Provider>,
);

(wrapper.find('Field').props() as any).onClick(0, 1);
wrapper.update();
(wrapper.find('Field').props() as any).onClick(1, 1);
(wrapper.find('Field').props() as any).onClick(0, 1);
wrapper.update();
(wrapper.find('Field').props() as any).onClick(1, 1);

expect(store.getState()).toMatchInlineSnapshot(`
expect(store.getState()).toMatchInlineSnapshot(`
Object {
"game": Object {
"board": Array [
Expand Down Expand Up @@ -527,7 +528,8 @@ describe('ReduxScreen with real store', () => {
},
}
`);
expect((store.dispatch as jest.Mock).mock.calls).toMatchInlineSnapshot(`
expect((store.dispatch as jest.Mock).mock.calls)
.toMatchInlineSnapshot(`
Array [
Array [
Object {
Expand All @@ -549,17 +551,20 @@ describe('ReduxScreen with real store', () => {
],
]
`);
});
});

test('Expected actions updateBoard', () => {
const wrapper = mount(
<Provider store={store}>
<GameOfLifeWithRedux />
</Provider>,
);
test('Expected actions updateBoard', () => {
const wrapper = mount(
<Provider store={store}>
<GameOfLifeWithRedux />
</Provider>,
);

(wrapper.find('button[children="Обновить"]').props() as any).onClick();
expect((store.dispatch as jest.Mock).mock.calls).toMatchInlineSnapshot(`
(wrapper
.find('button[children="Обновить"]')
.props() as any).onClick();
expect((store.dispatch as jest.Mock).mock.calls)
.toMatchInlineSnapshot(`
Array [
Array [
Object {
Expand All @@ -569,17 +574,19 @@ describe('ReduxScreen with real store', () => {
],
]
`);
});
});

test('Expected actions clearBoard', () => {
const wrapper = mount(
<Provider store={store}>
<GameOfLifeWithRedux />
</Provider>,
);
test('Expected actions clearBoard', () => {
const wrapper = mount(
<Provider store={store}>
<GameOfLifeWithRedux />
</Provider>,
);

(wrapper.find('button[children="Очистить"]').props() as any).onClick();
expect(store.getState()).toMatchInlineSnapshot(`
(wrapper
.find('button[children="Очистить"]')
.props() as any).onClick();
expect(store.getState()).toMatchInlineSnapshot(`
Object {
"game": Object {
"board": Array [
Expand Down Expand Up @@ -1035,7 +1042,8 @@ describe('ReduxScreen with real store', () => {
},
}
`);
expect((store.dispatch as jest.Mock).mock.calls).toMatchInlineSnapshot(`
expect((store.dispatch as jest.Mock).mock.calls)
.toMatchInlineSnapshot(`
Array [
Array [
Object {
Expand All @@ -1045,19 +1053,20 @@ describe('ReduxScreen with real store', () => {
],
]
`);
});
test('Expected actions changeSpeed', () => {
const wrapper = mount(
<Provider store={store}>
<GameOfLifeWithRedux />
</Provider>,
);

(wrapper.find('input').at(1).props() as any).onChange({
target: { value: 250 },
});
expect(store.dispatch as jest.Mock).toHaveBeenCalledTimes(1);
expect((store.dispatch as jest.Mock).mock.calls).toMatchInlineSnapshot(`
test('Expected actions changeSpeed', () => {
const wrapper = mount(
<Provider store={store}>
<GameOfLifeWithRedux />
</Provider>,
);

(wrapper.find('input').at(1).props() as any).onChange({
target: { value: 250 },
});
expect(store.dispatch as jest.Mock).toHaveBeenCalledTimes(1);
expect((store.dispatch as jest.Mock).mock.calls)
.toMatchInlineSnapshot(`
Array [
Array [
Object {
Expand All @@ -1067,5 +1076,6 @@ describe('ReduxScreen with real store', () => {
],
]
`);
});
});
});
Loading

0 comments on commit 4d51d49

Please sign in to comment.