Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React from 'react';
import { act, fireEvent, render, screen } from '@testing-library/react';
import { Provider } from 'react-redux';
import { AnyAction, applyMiddleware, combineReducers, createStore } from 'redux';
import thunk from 'redux-thunk';

import { FCCounterConnectedBindActionCreators as ConnectedCounter } from './fc-counter-connected-bind-action-creators';

const reducer = combineReducers({
counters: combineReducers({
reduxCounter: (state: number = 0, action: AnyAction) => {
switch (action.type) {
case 'counters/INCREMENT':
return state + 1;

default:
return state;
}
},
}),
});

afterEach(() => {
jest.useRealTimers();
});
Comment on lines +23 to +25

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The cleanup() call is redundant because @testing-library/react automatically performs cleanup after each test when used with Jest. This block can be simplified.

afterEach(() => {
  jest.useRealTimers();
});


test('can dispatch the delayed increment thunk', async () => {
jest.useFakeTimers();

const label = 'Counter 1';
renderWithRedux(<ConnectedCounter label={label} />);

expect(screen.getByText(`${label}: 0`)).toBeTruthy();
fireEvent.click(screen.getByText('Increment'));

await act(async () => {
jest.advanceTimersByTime(1000);
});

expect(screen.getByText(`${label}: 1`)).toBeTruthy();
});

function renderWithRedux(
jsx: JSX.Element,
options: { initialState?: object } = {}
) {
const store = createStore(
reducer,
options.initialState,
applyMiddleware(thunk)
);

return render(<Provider store={store}>{jsx}</Provider>);
}
3 changes: 2 additions & 1 deletion playground/src/store/store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RootAction, RootState, Services } from 'MyTypes';
import { applyMiddleware, createStore } from 'redux';
import { createEpicMiddleware } from 'redux-observable';
import thunk from 'redux-thunk';

import services from '../services';
import { routerMiddleware } from './redux-router';
Expand All @@ -18,7 +19,7 @@ const epicMiddleware = createEpicMiddleware<
});

// configure middlewares
const middlewares = [epicMiddleware, routerMiddleware];
const middlewares = [thunk, epicMiddleware, routerMiddleware];
// compose enhancers
const enhancer = composeEnhancers(applyMiddleware(...middlewares));

Expand Down