Skip to content

Commit

Permalink
Merge pull request #610 from MetaPhase-Consulting/tests/mock-request-…
Browse files Browse the repository at this point in the history
…testing-2

Check that mocks were called
  • Loading branch information
mjoyce91 committed Dec 30, 2019
2 parents e9b7aaa + 38c649c commit 94e8574
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 339 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@
"!src/actions/showStaticContent.js",
"!src/reducers/showStaticContent.js",
"!src/Components/CSV/**/*.{js,jsx}",
"!src/polyfills/**/*.{js,jsx}"
"!src/polyfills/**/*.{js,jsx}",
"!src/testUtilities/**/*"
],
"coverageReporters": [
"text-summary",
Expand Down
78 changes: 32 additions & 46 deletions src/actions/aboutContent.test.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,57 @@
import { setupAsyncMocks } from '../testUtilities/testUtilities';
import { setupAsyncMocks, spyMockAdapter, expectMockWasCalled } from '../testUtilities/testUtilities';
import * as actions from './aboutContent';

const { mockStore, mockAdapter } = setupAsyncMocks();
const { mockStore } = setupAsyncMocks();

describe('async actions', () => {
beforeEach(() => {
mockAdapter.onGet('http://localhost:8000/api/v1/aboutpage/').reply(200,
{ text: 'text', is_visible: true },
);
mockAdapter.onPatch('http://localhost:8000/api/v1/aboutpage/').reply(200,
null,
);
});
let mock;
let spy;

it('fetches the about content', (done) => {
const store = mockStore({});

const f = () => {
setTimeout(() => {
store.dispatch(actions.aboutContentFetchData());
done();
}, 0);
};
f();
({ mock, spy } = spyMockAdapter({
url: 'http://localhost:8000/api/v1/aboutpage/', response: [200, { text: 'text', is_visible: true }],
})); mock();

store.dispatch(actions.aboutContentFetchData());

expectMockWasCalled({ spy, cb: done });
});

it('handles errors when fetching the about content', (done) => {
const store = mockStore({});

mockAdapter.onGet('http://localhost:8000/api/v1/aboutpage/').reply(404,
null,
);

const f = () => {
setTimeout(() => {
store.dispatch(actions.aboutContentFetchData());
done();
}, 0);
};
f();
({ mock, spy } = spyMockAdapter({
url: 'http://localhost:8000/api/v1/aboutpage/', response: [404, null],
})); mock();

store.dispatch(actions.aboutContentFetchData());

expectMockWasCalled({ spy, cb: done });
});

it('patches the home banner content', (done) => {
const store = mockStore({});

const f = () => {
setTimeout(() => {
store.dispatch(actions.aboutContentPatchData({ content: 'text' }));
done();
}, 0);
};
f();
({ mock, spy } = spyMockAdapter({
url: 'http://localhost:8000/api/v1/aboutpage/', response: [200, null], type: 'onPatch',
})); mock();

store.dispatch(actions.aboutContentPatchData({ content: 'text' }));

expectMockWasCalled({ spy, cb: done });
});

it('handles errors when patching the home banner content', (done) => {
const store = mockStore({});

mockAdapter.onPatch('http://localhost:8000/api/v1/aboutpage/').reply(404,
null,
);

const f = () => {
setTimeout(() => {
store.dispatch(actions.aboutContentPatchData({ content: 'text' }));
done();
}, 0);
};
f();
({ mock, spy } = spyMockAdapter({
url: 'http://localhost:8000/api/v1/aboutpage/', response: [404, null], type: 'onPatch',
})); mock();

store.dispatch(actions.aboutContentPatchData({ content: 'text' }));

expectMockWasCalled({ spy, cb: done });
});
});
69 changes: 25 additions & 44 deletions src/actions/assignment.test.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,43 @@
import { setupAsyncMocks } from '../testUtilities/testUtilities';
import { setupAsyncMocks, spyMockAdapter, expectMockWasCalled } from '../testUtilities/testUtilities';
import * as actions from './assignment';
import assignmentObject from '../__mocks__/assignmentObject';

const { mockStore, mockAdapter } = setupAsyncMocks();
const { mockStore } = setupAsyncMocks();

describe('async actions', () => {
let [mock, spy, store] = Array(3);

beforeEach(() => {
mockAdapter.onGet('http://localhost:8000/api/v1/profile/assignments/?status=active').reply(200,
{ results: [assignmentObject] },
);
store = mockStore({ assignment: {} });

mockAdapter.onGet('http://localhost:8000/api/v1/profile/assignments/?status=closed').reply(200,
{ results: [assignmentObject] },
);
({ mock, spy } = spyMockAdapter({
url: '/profile/assignments/?status=active', response: [200, { results: [assignmentObject] }],
})); mock();
});

it('can fetch assignments', (done) => {
const store = mockStore({ assignment: {} });

const f = () => {
setTimeout(() => {
store.dispatch(actions.assignmentFetchData());
store.dispatch(actions.assignmentIsLoading());
done();
}, 0);
};
f();
store.dispatch(actions.assignmentFetchData());
store.dispatch(actions.assignmentIsLoading());
expectMockWasCalled({ spy, cb: done });
});

it('can fetch assignments where status = closed', (done) => {
const store = mockStore({ assignment: {} });

const f = () => {
setTimeout(() => {
store.dispatch(actions.assignmentFetchData('closed'));
store.dispatch(actions.assignmentIsLoading());
done();
}, 0);
};
f();
({ mock, spy } = spyMockAdapter({
url: '/profile/assignments/?status=closed', response: [200, { results: [assignmentObject] }],
})); mock();

store.dispatch(actions.assignmentFetchData('closed'));
store.dispatch(actions.assignmentIsLoading());
expectMockWasCalled({ spy, cb: done });
});

it('can handle errors when fetching assignments', (done) => {
const store = mockStore({ assignment: {} });

mockAdapter.reset();

mockAdapter.onGet('http://localhost:8000/api/v1/profile/assignments/?status=active').reply(404,
null,
);

const f = () => {
setTimeout(() => {
store.dispatch(actions.assignmentFetchData());
store.dispatch(actions.assignmentIsLoading());
done();
}, 0);
};
f();
({ mock, spy } = spyMockAdapter({
url: '/profile/assignments/?status=active', response: [404],
})); mock();

store.dispatch(actions.assignmentFetchData());
store.dispatch(actions.assignmentIsLoading());
expectMockWasCalled({ spy, cb: done });
});
});
30 changes: 15 additions & 15 deletions src/actions/bidList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('async actions', () => {
});

it('can fetch a bid list', (done) => {
mockAdapter.onGet('http://localhost:8000/api/v1/fsbid/bidlist/?ordering=draft_date').reply(200,
mockAdapter.onGet('/fsbid/bidlist/?ordering=draft_date').reply(200,
bidList,
);

Expand All @@ -62,7 +62,7 @@ describe('async actions', () => {
});

it('can handle errors when fetching a bid list', (done) => {
mockAdapter.onGet('http://localhost:8000/api/v1/fsbid/bidlist/?ordering=draft_date').reply(404,
mockAdapter.onGet('/fsbid/bidlist/?ordering=draft_date').reply(404,
null,
);

Expand All @@ -76,7 +76,7 @@ describe('async actions', () => {
});

it('can remove a position from the bid list', (done) => {
mockAdapter.onDelete('http://localhost:8000/api/v1/fsbid/bidlist/position/1/').reply(204,
mockAdapter.onDelete('/fsbid/bidlist/position/1/').reply(204,
null,
);

Expand All @@ -90,7 +90,7 @@ describe('async actions', () => {
});

it('can add a position to the bid list', (done) => {
mockAdapter.onPut('http://localhost:8000/api/v1/fsbid/bidlist/position/1/').reply(204,
mockAdapter.onPut('/fsbid/bidlist/position/1/').reply(204,
null,
);

Expand All @@ -104,7 +104,7 @@ describe('async actions', () => {
});

it('can submit a bid', (done) => {
mockAdapter.onPut('http://localhost:8000/api/v1/fsbid/bidlist/position/1/submit/').reply(204,
mockAdapter.onPut('/fsbid/bidlist/position/1/submit/').reply(204,
null,
);

Expand All @@ -118,7 +118,7 @@ describe('async actions', () => {
});

it('can handle errors when submitting a bid', (done) => {
mockAdapter.onPut('http://localhost:8000/api/v1/fsbid/bidlist/position/1/submit/').reply(404,
mockAdapter.onPut('/fsbid/bidlist/position/1/submit/').reply(404,
null,
);

Expand All @@ -132,7 +132,7 @@ describe('async actions', () => {
});

it('can handle errors when adding a position to the bid list', (done) => {
mockAdapter.onPut('http://localhost:8000/api/v1/fsbid/bidlist/position/1/').reply(404,
mockAdapter.onPut('/fsbid/bidlist/position/1/').reply(404,
null,
);

Expand All @@ -146,7 +146,7 @@ describe('async actions', () => {
});

it('can accept a bid', (done) => {
mockAdapter.onGet('http://localhost:8000/api/v1/bid/1/accept_handshake/').reply(204,
mockAdapter.onGet('/bid/1/accept_handshake/').reply(204,
null,
);

Expand All @@ -160,7 +160,7 @@ describe('async actions', () => {
});

it('can handle errors when accepting a bid', (done) => {
mockAdapter.onGet('http://localhost:8000/api/v1/bid/1/accept_handshake/').reply(404,
mockAdapter.onGet('/bid/1/accept_handshake/').reply(404,
null,
);

Expand All @@ -174,7 +174,7 @@ describe('async actions', () => {
});

it('can decline a bid', (done) => {
mockAdapter.onGet('http://localhost:8000/api/v1/bid/1/decline_handshake/').reply(204,
mockAdapter.onGet('/bid/1/decline_handshake/').reply(204,
null,
);

Expand All @@ -188,7 +188,7 @@ describe('async actions', () => {
});

it('can handle errors when declining a bid', (done) => {
mockAdapter.onGet('http://localhost:8000/api/v1/bid/1/decline_handshake/').reply(404,
mockAdapter.onGet('/bid/1/decline_handshake/').reply(404,
null,
);

Expand All @@ -202,9 +202,9 @@ describe('async actions', () => {
});

it('can fetch a client bid list', (done) => {
const store = mockStore({ clientView: { client: { perdet_seq_number: 1 } } });
store = mockStore({ clientView: { client: { perdet_seq_number: 1 } } });

mockAdapter.onGet('http://localhost:8000/api/v1/fsbid/cdo/client/1/?ordering=draft_date').reply(200,
mockAdapter.onGet('/fsbid/cdo/client/1/?ordering=draft_date').reply(200,
{ results: [] },
);

Expand All @@ -218,9 +218,9 @@ describe('async actions', () => {
});

it('can handle errors when fetching a client bid list', (done) => {
const store = mockStore({});
store = mockStore({});

mockAdapter.onGet('http://localhost:8000/api/v1/fsbid/cdo/client/1/?ordering=draft_date').reply(404,
mockAdapter.onGet('/fsbid/cdo/client/1/?ordering=draft_date').reply(404,
null,
);

Expand Down
Loading

0 comments on commit 94e8574

Please sign in to comment.