-
Notifications
You must be signed in to change notification settings - Fork 96
Lesson 15 #27
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
Merged
Lesson 15 #27
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c290eb8
Use actionCreators
vvscode 9bb49e8
Install react-redux
vvscode 4582644
npm audit fix
vvscode cf85dcb
Setup react-redux for ReduxScreen
vvscode 2fc1739
Apply connect to NextMove component
vvscode d974d7c
Rename mapStateToProps for ReduxScreen
vvscode 7b5669b
Use mapDispatchToProps for ReduxScreen
vvscode c82c743
Use bindActionCreators for ReduxScreen
vvscode dc55514
Fix action dispatching for ReduxScreen
vvscode 7e1967d
Define mapDispatchToProps as object
vvscode 07787dd
Basic test for connected component
vvscode 48fda7d
Integration test for ReduxScreen with real store
vvscode 3122c49
Keep both optoins together
vvscode c084627
Update README
vvscode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,15 @@ | ||
| import React from 'react'; | ||
| import { withRedux } from '@/utils/withRedux'; | ||
| import { TicTacToeGameState } from '@/rdx/reducer'; | ||
| import React from "react"; | ||
| import { TicTacToeGameState } from "@/rdx/reducer"; | ||
| import { connect } from "react-redux"; | ||
|
|
||
| const RawNextMove: React.FC<{ nextMove: string }> = ({ nextMove }) => <h2>Next move is for {nextMove}</h2>; | ||
| const RawNextMove: React.FC<{ nextMove: string }> = ({ nextMove }) => ( | ||
| <h2>Next move is for {nextMove}</h2> | ||
| ); | ||
|
|
||
| export const NextMove = withRedux(RawNextMove, (state: TicTacToeGameState) => ({ | ||
| nextMove: state.nextMove | ||
| })); | ||
| function mapStateToProps(state: TicTacToeGameState) { | ||
| return { | ||
| nextMove: state.nextMove, | ||
| }; | ||
| } | ||
|
|
||
| export const NextMove = connect(mapStateToProps)(RawNextMove); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| export const X_MOVE = "X_MOVE"; | ||
| export const O_MOVE = "O_MOVE"; | ||
|
|
||
| export type Coordinates = { x: number; y: number }; | ||
|
|
||
| export function xMove(payload: Coordinates) { | ||
| return { | ||
| type: X_MOVE, | ||
| payload, | ||
| }; | ||
| } | ||
|
|
||
| export function oMove(payload: Coordinates) { | ||
| return { | ||
| type: O_MOVE, | ||
| payload, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import React from "react"; | ||
| import { ReduxScreen } from "./ReduxScreen"; | ||
| import { Provider } from "react-redux"; | ||
| import { mount } from "enzyme"; | ||
| import { reducer } from "@/rdx/reducer"; | ||
| import { createStore } from "redux"; | ||
| import configureStore from "redux-mock-store"; | ||
|
|
||
| describe("ReduxScreen with mocked store", () => { | ||
| const mockStore = configureStore([]); | ||
|
|
||
| let store: any; | ||
|
|
||
| beforeEach(() => { | ||
| store = mockStore({ | ||
| nextMove: "x", | ||
| gameField: [[]], | ||
| }); | ||
| }); | ||
|
|
||
| it("should generate action on click", () => { | ||
| const wrapper = mount( | ||
| <Provider store={store}> | ||
| <ReduxScreen /> | ||
| </Provider> | ||
| ); | ||
|
|
||
| (wrapper.find("Field").props() as any).onClick(100, 999); | ||
|
|
||
| expect(store.getActions()).toMatchInlineSnapshot(` | ||
| Array [ | ||
| Object { | ||
| "payload": Object { | ||
| "x": 100, | ||
| "y": 999, | ||
| }, | ||
| "type": "X_MOVE", | ||
| }, | ||
| ] | ||
| `); | ||
| }); | ||
| }); | ||
|
|
||
| describe("ReduxScreen with real store", () => { | ||
| let store: any; | ||
|
|
||
| beforeEach(() => { | ||
| store = createStore(reducer, { | ||
| nextMove: "x", | ||
| gameField: [ | ||
| ["", ""], | ||
| ["", ""], | ||
| ], | ||
| }); | ||
| jest.spyOn(store, "dispatch"); | ||
| }); | ||
|
|
||
| it("should generate action on click", () => { | ||
| const wrapper = mount( | ||
| <Provider store={store}> | ||
| <ReduxScreen /> | ||
| </Provider> | ||
| ); | ||
|
|
||
| (wrapper.find("Field").props() as any).onClick(0, 1); | ||
| wrapper.update(); // we need this if we're going to compare snapshots | ||
| (wrapper.find("Field").props() as any).onClick(1, 1); | ||
|
|
||
| expect(store.getState()).toMatchInlineSnapshot(` | ||
| Object { | ||
| "gameField": Array [ | ||
| Array [ | ||
| "", | ||
| "", | ||
| ], | ||
| Array [ | ||
| "x", | ||
| "o", | ||
| ], | ||
| ], | ||
| "nextMove": "x", | ||
| } | ||
| `); | ||
| expect((store.dispatch as jest.Mock).mock.calls).toMatchInlineSnapshot(` | ||
| Array [ | ||
| Array [ | ||
| Object { | ||
| "payload": Object { | ||
| "x": 0, | ||
| "y": 1, | ||
| }, | ||
| "type": "X_MOVE", | ||
| }, | ||
| ], | ||
| Array [ | ||
| Object { | ||
| "payload": Object { | ||
| "x": 1, | ||
| "y": 1, | ||
| }, | ||
| "type": "O_MOVE", | ||
| }, | ||
| ], | ||
| ] | ||
| `); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,44 @@ | ||
| import React from 'react'; | ||
| import * as actionTypes from '@/rdx/types'; | ||
| import { Field } from '@/components/InteractiveField/components/Field'; | ||
| import { withRedux } from '@/utils/withRedux'; | ||
| import { Action } from 'redux'; | ||
| import { NextMove } from 'components/NextMove'; | ||
| import { TicTacToeGameState } from '@/rdx/reducer'; | ||
|
|
||
| function getReduxScreenState(state: TicTacToeGameState) { | ||
| return { | ||
| gameField: state.gameField, | ||
| nextMove: state.nextMove, | ||
| }; | ||
| } | ||
| import React from "react"; | ||
| import { Field } from "@/components/InteractiveField/components/Field"; | ||
| import { NextMove } from "components/NextMove"; | ||
| import { TicTacToeGameState } from "@/rdx/reducer"; | ||
| import { Coordinates, xMove, oMove } from "@/rdx/actions"; | ||
| import { connect } from "react-redux"; | ||
|
|
||
| interface RawReduxScreenProps { | ||
| nextMove: string; | ||
| gameField: string[][]; | ||
| dispatch: (action: Action & { payload?: any }) => void; | ||
| xMove: (coords: Coordinates) => void; | ||
| oMove: (coords: Coordinates) => void; | ||
| } | ||
|
|
||
| class RawReduxScreen extends React.Component<RawReduxScreenProps, {}>{ | ||
| class RawReduxScreen extends React.Component<RawReduxScreenProps, {}> { | ||
| onCellClick = (x: number, y: number) => { | ||
| this.props.dispatch({ | ||
| type: this.props.nextMove === 'x' ? actionTypes.X_MOVE : actionTypes.O_MOVE, | ||
| payload: { x, y }, | ||
| }) | ||
| } | ||
| this.props[this.props.nextMove === "x" ? "xMove" : "oMove"]({ x, y }); | ||
| }; | ||
|
|
||
| render() { | ||
| return <div> | ||
| <h1>Open console to observe</h1> | ||
| <NextMove /> | ||
| <Field field={this.props.gameField} onClick={this.onCellClick} /> | ||
| <pre>{JSON.stringify(this.props, null, 2)}</pre> | ||
| </div> | ||
| return ( | ||
| <div> | ||
| <h1>Open console to observe</h1> | ||
| <NextMove /> | ||
| <Field field={this.props.gameField} onClick={this.onCellClick} /> | ||
| <pre>{JSON.stringify(this.props, null, 2)}</pre> | ||
| </div> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export const ReduxScreen = withRedux(RawReduxScreen, getReduxScreenState); | ||
| function mapStateToProps(state: TicTacToeGameState) { | ||
| return { | ||
| gameField: state.gameField, | ||
| nextMove: state.nextMove, | ||
| }; | ||
| } | ||
|
|
||
| const mapDispatchToProps = { xMove, oMove }; | ||
|
|
||
| export const ReduxScreen = connect( | ||
| mapStateToProps, | ||
| mapDispatchToProps | ||
| )(RawReduxScreen); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.