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

Redux example #53

Merged
merged 2 commits into from
Oct 13, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@

[Pull request](https://github.com/nickovchinnikov/minesweeper/pull/50/files)


## Redux intro

### Pure functions benifits
Expand All @@ -292,5 +291,10 @@

[Pull request](https://github.com/nickovchinnikov/minesweeper/pull/51/files)

###
### Redux basic example
### Referential transparency

[Pull request](https://github.com/nickovchinnikov/minesweeper/pull/52/files)

### Redux basic example

[Pull request](https://github.com/nickovchinnikov/minesweeper/pull/53/files)
23 changes: 23 additions & 0 deletions examples/Redux/ClickCounter.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
import { render, fireEvent, screen } from '@testing-library/react';

import { ClickCounterBasic } from './ClickCounterBasic';

describe('ClickCounterBasic component test', () => {
it('should use custom step when incrementing', async () => {
render(<ClickCounterBasic />);
const decButton = screen.getByTestId('dec');
const incButton = screen.getByTestId('inc');
const count = screen.getByTestId('count');

expect(count).toHaveTextContent('Count: 0');

fireEvent.click(incButton);

expect(count).toHaveTextContent('Count: 1');

fireEvent.click(decButton);

expect(count).toHaveTextContent('Count: 0');
});
});
19 changes: 19 additions & 0 deletions examples/Redux/ClickCounterBasic.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React, { FC, useReducer } from 'react';

import { reducer, initialState, increment, decrement } from './counter';

export const ClickCounterBasic: FC = () => {
const [state, dispatch] = useReducer(reducer, initialState);

return (
<>
<div data-testid="count">Count: {state}</div>
<button data-testid="dec" onClick={() => dispatch(decrement())}>
-
</button>
<button data-testid="inc" onClick={() => dispatch(increment())}>
+
</button>
</>
);
};
10 changes: 10 additions & 0 deletions examples/Redux/counter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { initialState, reducer, increment, decrement } from './counter';

describe('Counter redux module test', () => {
it('Default init with increment action', async () => {
expect(reducer(initialState, increment())).toBe(1);
});
it('Init with value decrement action', async () => {
expect(reducer(10, decrement())).toBe(9);
});
});
22 changes: 22 additions & 0 deletions examples/Redux/counter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
interface Action {
type: string;
}

export const initialState = 0;

const INCREMENT = 'examples/Redux/counter/increment';
const DECREMENT = 'examples/Redux/counter/decrement';

export function reducer(state: number, action: Action): number {
switch (action.type) {
case INCREMENT:
return state + 1;
case DECREMENT:
return state - 1;
default:
throw new Error();
}
}

export const increment = (): Action => ({ type: INCREMENT });
export const decrement = (): Action => ({ type: DECREMENT });
10 changes: 10 additions & 0 deletions examples/Redux/counterSlice.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { reducer, actions, initialState } from './counterSlice';

describe('Counter redux module test', () => {
it('Default init with increment action', async () => {
expect(reducer(initialState, actions.increment())).toBe(1);
});
it('Init with value decrement action', async () => {
expect(reducer(10, actions.decrement())).toBe(9);
});
});
14 changes: 14 additions & 0 deletions examples/Redux/counterSlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createSlice } from '@reduxjs/toolkit';

export const initialState = 0;

const { reducer, actions } = createSlice({
name: 'counter',
initialState,
reducers: {
increment: (state) => state + 1,
decrement: (state) => state - 1,
},
});

export { reducer, actions };
84 changes: 82 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^5.3.0",
"redux": "^4.1.1"
"@reduxjs/toolkit": "^1.6.2"
}
}