浏览代码

[CHORE] Example of reducer & selectors specs

Oleg Shuralev 4 年之前
父节点
当前提交
202ecf3cab

+ 27 - 0
kafka-ui-react-app/src/redux/reducers/clusters/__test__/fixtures.ts

@@ -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,
+];

+ 15 - 0
kafka-ui-react-app/src/redux/reducers/clusters/__test__/reducer.spec.ts

@@ -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);
+  });
+});

+ 58 - 0
kafka-ui-react-app/src/redux/reducers/clusters/__test__/selectors.spec.ts

@@ -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,
+      ]);
+    });
+  });
+});

+ 1 - 1
kafka-ui-react-app/src/redux/store/configureStore/dev.ts

@@ -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 {

+ 1 - 1
kafka-ui-react-app/src/redux/store/configureStore/prod.ts

@@ -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];