-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathfc-counter-connected-own-props.spec.tsx
51 lines (44 loc) · 1.51 KB
/
fc-counter-connected-own-props.spec.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import React from 'react';
import { createStore, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import { render, fireEvent, cleanup } from '@testing-library/react';
import { FCCounterConnectedOwnProps as ConnectedCounter } from './fc-counter-connected-own-props';
const reducer = combineReducers({
counters: combineReducers({
reduxCounter: (state: number = 0, action: any) => {
switch (action.type) {
case 'counters/INCREMENT':
return state + 1; // action: { type: "INCREMENT"; }
default:
return state;
}
},
}),
});
afterEach(cleanup);
test('can render with redux with defaults', () => {
const label = 'Counter 1';
const { getByText } = renderWithRedux(<ConnectedCounter label={label} />);
fireEvent.click(getByText('Increment'));
expect(getByText(RegExp(label)).textContent).toBe(label + ': 1');
});
test('can render with redux with custom initial state', () => {
const label = 'Counter 1';
const { getByText } = renderWithRedux(<ConnectedCounter label={label} />, {
initialState: { counters: { reduxCounter: 3 } },
});
fireEvent.click(getByText('Increment'));
expect(getByText(RegExp(label)).textContent).toBe(label + ': 4');
});
// TODO: move to external utils
// Redux Provider utility
function renderWithRedux(
jsx: JSX.Element,
options: { initialState?: object } = {}
) {
const store = createStore(reducer, options.initialState);
return {
...render(<Provider store={store}>{jsx}</Provider>),
store,
};
}