[CHORE] Example of reducer & selectors specs

This commit is contained in:
Oleg Shuralev 2021-02-19 02:26:21 +03:00
parent fcea2952c5
commit 202ecf3cab
No known key found for this signature in database
GPG key ID: 99C6BDC0A1C2E647
5 changed files with 102 additions and 2 deletions

View file

@ -0,0 +1,27 @@
import { Cluster, ServerStatus } from 'generated-sources';
export const onlineClusterPayload: Cluster = {
name: 'secondLocal',
defaultCluster: true,
status: ServerStatus.Online,
brokerCount: 1,
onlinePartitionCount: 6,
topicCount: 3,
bytesInPerSec: 1.55,
bytesOutPerSec: 9.314,
};
export const offlineClusterPayload: Cluster = {
name: 'local',
defaultCluster: false,
status: ServerStatus.Offline,
brokerCount: 1,
onlinePartitionCount: 2,
topicCount: 2,
bytesInPerSec: 3.42,
bytesOutPerSec: 4.14,
};
export const clustersPayload: Cluster[] = [
onlineClusterPayload,
offlineClusterPayload,
];

View file

@ -0,0 +1,15 @@
import { fetchClusterListAction } from 'redux/actions';
import reducer from 'redux/reducers/clusters/reducer';
import { clustersPayload } from './fixtures';
describe('Clusters reducer', () => {
it('returns the initial state', () => {
expect(reducer(undefined, fetchClusterListAction.request())).toEqual([]);
});
it('reacts on GET_CLUSTERS__SUCCESS and returns payload', () => {
expect(
reducer(undefined, fetchClusterListAction.success(clustersPayload))
).toEqual(clustersPayload);
});
});

View file

@ -0,0 +1,58 @@
import { fetchClusterListAction } from 'redux/actions';
import configureStore from 'redux/store/configureStore';
import * as selectors from '../selectors';
import {
clustersPayload,
offlineClusterPayload,
onlineClusterPayload,
} from './fixtures';
const store = configureStore();
describe('Clusters selectors', () => {
describe('Initial State', () => {
it('returns fetch status', () => {
expect(selectors.getIsClusterListFetched(store.getState())).toBeFalsy();
});
it('returns cluster list', () => {
expect(selectors.getClusterList(store.getState())).toEqual([]);
});
it('returns online cluster list', () => {
expect(selectors.getOnlineClusters(store.getState())).toEqual([]);
});
it('returns offline cluster list', () => {
expect(selectors.getOfflineClusters(store.getState())).toEqual([]);
});
});
describe('state', () => {
beforeAll(() => {
store.dispatch(fetchClusterListAction.success(clustersPayload));
});
it('returns fetch status', () => {
expect(selectors.getIsClusterListFetched(store.getState())).toBeTruthy();
});
it('returns cluster list', () => {
expect(selectors.getClusterList(store.getState())).toEqual(
clustersPayload
);
});
it('returns online cluster list', () => {
expect(selectors.getOnlineClusters(store.getState())).toEqual([
onlineClusterPayload,
]);
});
it('returns offline cluster list', () => {
expect(selectors.getOfflineClusters(store.getState())).toEqual([
offlineClusterPayload,
]);
});
});
});

View file

@ -2,7 +2,7 @@ import { createStore, applyMiddleware, compose } from 'redux';
import thunk, { ThunkMiddleware } from 'redux-thunk';
import { RootState } from 'redux/interfaces';
import { Action } from 'typesafe-actions';
import rootReducer from '../../reducers';
import rootReducer from 'redux/reducers';
declare global {
interface Window {

View file

@ -1,6 +1,6 @@
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from '../../reducers';
import rootReducer from 'redux/reducers';
export default () => {
const middlewares = [thunk];