Browse Source

added way to test action called from react component

Torkel Ödegaard 7 years ago
parent
commit
61e9148eed

+ 15 - 0
public/app/core/redux/actionCreatorFactory.ts

@@ -53,5 +53,20 @@ export const noPayloadActionCreatorFactory = (type: string): NoPayloadActionCrea
   return { create };
 };
 
+export interface NoPayloadActionCreatorMock extends NoPayloadActionCreator {
+  calls: number;
+}
+
+export const getNoPayloadActionCreatorMock = (creator: NoPayloadActionCreator): NoPayloadActionCreatorMock => {
+  const mock: NoPayloadActionCreatorMock = Object.assign(
+    (): ActionOf<undefined> => {
+      mock.calls++;
+      return { type: creator.type, payload: undefined };
+    },
+    { type: creator.type, calls: 0 }
+  );
+  return mock;
+};
+
 // Should only be used by tests
 export const resetAllActionCreatorTypes = () => (allActionCreators.length = 0);

+ 2 - 2
public/app/core/redux/index.ts

@@ -1,2 +1,2 @@
-export { actionCreatorFactory, noPayloadActionCreatorFactory, ActionOf, ActionCreator } from './actionCreatorFactory';
-export { reducerFactory } from './reducerFactory';
+export * from './actionCreatorFactory';
+export * from './reducerFactory';

+ 19 - 1
public/app/features/dashboard/containers/DashboardPage.test.tsx

@@ -3,6 +3,7 @@ import { shallow, ShallowWrapper } from 'enzyme';
 import { DashboardPage, Props, State } from './DashboardPage';
 import { DashboardModel } from '../state';
 import { cleanUpDashboard } from '../state/actions';
+import { getNoPayloadActionCreatorMock, NoPayloadActionCreatorMock  } from 'app/core/redux';
 import { DashboardRouteInfo, DashboardInitPhase } from 'app/types';
 
 jest.mock('sass/_variables.scss', () => ({
@@ -13,6 +14,7 @@ jest.mock('sass/_variables.scss', () => ({
 jest.mock('app/features/dashboard/components/DashboardSettings/SettingsCtrl', () => ({}));
 
 interface ScenarioContext {
+  cleanUpDashboardMock: NoPayloadActionCreatorMock;
   dashboard?: DashboardModel;
   setDashboardProp: (overrides?: any, metaOverrides?: any) => void;
   wrapper?: ShallowWrapper<Props, State, DashboardPage>;
@@ -42,6 +44,7 @@ function dashboardPageScenario(description, scenarioFn: (ctx: ScenarioContext) =
     let setupFn: () => void;
 
     const ctx: ScenarioContext = {
+      cleanUpDashboardMock: getNoPayloadActionCreatorMock(cleanUpDashboard),
       setup: fn => {
         setupFn = fn;
       },
@@ -63,7 +66,7 @@ function dashboardPageScenario(description, scenarioFn: (ctx: ScenarioContext) =
           initDashboard: jest.fn(),
           updateLocation: jest.fn(),
           notifyApp: jest.fn(),
-          cleanUpDashboard: cleanUpDashboard,
+          cleanUpDashboard: ctx.cleanUpDashboardMock,
           dashboard: null,
         };
 
@@ -230,4 +233,19 @@ describe('DashboardPage', () => {
       expect(ctx.wrapper.state().fullscreenPanel.id).toBe(0);
     });
   });
+
+  dashboardPageScenario("When dashboard unmounts", (ctx) => {
+    ctx.setup(() => {
+      ctx.mount();
+      ctx.setDashboardProp({
+        panels: [{ id: 0, type: 'graph'}],
+        schemaVersion: 17,
+      });
+      ctx.wrapper.unmount();
+    });
+
+    it('Should call clean up action' , () => {
+      expect(ctx.cleanUpDashboardMock.calls).toBe(1);
+    });
+  });
 });