From 78e834a0730b0590dbddc89d86236d55b27dee2a Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Mon, 31 Aug 2020 20:02:29 -0400 Subject: [PATCH 1/2] Replace React Redux recipe with Redux's recipe --- docs/example-react-redux.md | 129 ------------------------------------ netlify.toml | 7 ++ 2 files changed, 7 insertions(+), 129 deletions(-) delete mode 100644 docs/example-react-redux.md diff --git a/docs/example-react-redux.md b/docs/example-react-redux.md deleted file mode 100644 index bd93e6caf..000000000 --- a/docs/example-react-redux.md +++ /dev/null @@ -1,129 +0,0 @@ ---- -id: example-react-redux -title: React Redux ---- - -```jsx -// counter.js -import React from 'react' -import { connect } from 'react-redux' - -const Counter = ({ dispatch, count }) => { - const increment = () => { - dispatch({ type: 'INCREMENT' }) - } - - const decrement = () => { - dispatch({ type: 'DECREMENT' }) - } - - return ( -
-

Counter

-
- - {count} - -
-
- ) -} - -export default connect(state => ({ count: state.count }))(Counter) -``` - -For this example, we'll have a simple reducer that tracks and updates `count`: - -```js -// reducer.js -export const initialState = { - count: 0, -} - -export function reducer(state = initialState, action) { - switch (action.type) { - case 'INCREMENT': - return { - count: state.count + 1, - } - case 'DECREMENT': - return { - count: state.count - 1, - } - default: - return state - } -} -``` - -To test our connected component we can create a custom `render` function using -the `wrapper` option as explained in the -[setup](./react-testing-library/setup.md) page. -Our custom `render` function can look like this: - -```js -// test-utils.js -import React from 'react' -import { render as rtlRender } from '@testing-library/react' -import { createStore } from 'redux' -import { Provider } from 'react-redux' -import { initialState as reducerInitialState, reducer } from './reducer' - -function render( - ui, - { - initialState = reducerInitialState, - store = createStore(reducer, initialState), - ...renderOptions - } = {} -) { - function Wrapper({ children }) { - return {children} - } - return rtlRender(ui, { wrapper: Wrapper, ...renderOptions }) -} - -// re-export everything -export * from '@testing-library/react' - -// override render method -export { render } -``` - -```jsx -// counter.test.js -import React from 'react' -import { createStore } from 'redux' -// We're using our own custom render function and not RTL's render -// our custom utils also re-export everything from RTL -// so we can import fireEvent and screen here as well -import { render, fireEvent, screen } from './test-utils' -import '@testing-library/jest-dom/extend-expect' -import Counter from './counter' - -test('can render with redux with defaults', () => { - render() - fireEvent.click(screen.getByText('+')) - expect(screen.getByTestId('count-value')).toHaveTextContent('1') -}) - -test('can render with redux with custom initial state', () => { - render(, { - initialState: { count: 3 }, - }) - fireEvent.click(screen.getByText('-')) - expect(screen.getByTestId('count-value')).toHaveTextContent('2') -}) - -test('can render with redux with custom store', () => { - // this is a silly store that can never be changed - const store = createStore(() => ({ count: 1000 })) - render(, { - store, - }) - fireEvent.click(screen.getByText('+')) - expect(screen.getByTestId('count-value')).toHaveTextContent('1000') - fireEvent.click(screen.getByText('-')) - expect(screen.getByTestId('count-value')).toHaveTextContent('1000') -}) -``` diff --git a/netlify.toml b/netlify.toml index 1db9c9f94..9fdeaa0ff 100644 --- a/netlify.toml +++ b/netlify.toml @@ -138,3 +138,10 @@ to = "https://testing-library.com/docs/nightwatch-testing-library/intro" status = 301 force = true + +# React Redux recipe to Redux's recipe +[[redirects]] + from = "https://testing-library.com/docs/example-react-redux" + to = "https://redux.js.org/recipes/writing-tests#connected-components" + status = 301 + force = true From 856e4acf9fcc36b5ca62b43341a9961ae9b2ab69 Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Mon, 31 Aug 2020 20:19:55 -0400 Subject: [PATCH 2/2] Fix missing sidebar link for React Redux --- docs/example-react-redux.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 docs/example-react-redux.md diff --git a/docs/example-react-redux.md b/docs/example-react-redux.md new file mode 100644 index 000000000..a0a01ca5f --- /dev/null +++ b/docs/example-react-redux.md @@ -0,0 +1,7 @@ +--- +id: example-react-redux +title: React Redux +--- + +Moved to +[Writing Tests | Redux](https://redux.js.org/recipes/writing-tests#connected-components)