Skip to content

Commit

Permalink
i dont feel sorry for you at all. dont unit test middleware unless yo…
Browse files Browse the repository at this point in the history
…u want to use redux saga
  • Loading branch information
oatkiller authored and dplumlee committed Feb 29, 2020
1 parent e993991 commit 9508391
Showing 1 changed file with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,40 @@ import { AlertListState } from '../../types';
import { alertMiddlewareFactory } from './middleware';
import { AppAction } from '../action';
import { coreMock } from 'src/core/public/mocks';
import { AlertData, AlertResultList } from '../../../../../common/types';
import { createBrowserHistory } from 'history';
import { mockAlertResultList } from './mock_alert_result_list';
import { HttpHandler, HttpResponse } from 'src/core/public/http';

describe('alert details tests', () => {
let store: Store<AlertListState, AppAction>;
let coreStart: ReturnType<typeof coreMock.createStart>;
let history: History<never>;
/**
* A function that waits until a selector returns true.
*/
let selectorIsTrue: (selector: (state: AlertListState) => boolean) => Promise<void>;
beforeEach(() => {
coreStart = coreMock.createStart();
history = createBrowserHistory();
const middleware = alertMiddlewareFactory(coreStart);
store = createStore(alertListReducer, applyMiddleware(middleware));

selectorIsTrue = async selector => {
// If the selector returns true, we're done
while (selector(store.getState()) !== true) {
// otherwise, wait til the next state change occurs
await new Promise(resolve => {
const unsubscribe = store.subscribe(() => {
unsubscribe();
resolve();
});
});
}
};
});
describe('when the user is on the alert list page with a selected alert in the url', () => {
beforeEach(() => {
const implementation: HttpHandler = async path => {
// if (path.contains('q9ncfh4q9ctrmc90umcq4')) {
// const response: AlertData = mockAlertResultList().alerts[0];
// return response;
// }
// const response: AlertResultList = mockAlertResultList();
// return response;
};
coreStart.http.get.mockImplementation(implementation);
const firstResponse: Promise<unknown> = Promise.resolve(1);
const secondResponse: Promise<unknown> = Promise.resolve(2);
coreStart.http.get.mockReturnValueOnce(firstResponse).mockReturnValueOnce(secondResponse);

// Simulates user navigating to the /alerts page
store.dispatch({
Expand All @@ -49,9 +57,9 @@ describe('alert details tests', () => {
});
});

it('should return alert details data', () => {
const actualResponse = store.getState().alertDetails;
expect(actualResponse).not.toBeUndefined();
it('should return alert details data', async () => {
// wait for alertDetails to be defined
await selectorIsTrue(state => state.alertDetails !== undefined);
});
});
});

0 comments on commit 9508391

Please sign in to comment.