Commit fc1e6f72 authored by Zack Cuddy's avatar Zack Cuddy

Geo Replication - Refactor action_spec

Currently this unit test was behaving
simlar to an integration test by
testing the axios calls.

Instead we decided it made more sense
to test the axios calls in the api file
and then just mock the function
calls in the action spec.

This MR converst the axios mock to ee/api
method mocks.
parent 35c1ce5f
import MockAdapter from 'axios-mock-adapter';
import testAction from 'helpers/vuex_action_helper'; import testAction from 'helpers/vuex_action_helper';
import flash from '~/flash'; import flash from '~/flash';
import toast from '~/vue_shared/plugins/global_toast'; import toast from '~/vue_shared/plugins/global_toast';
import axios from '~/lib/utils/axios_utils'; import Api from 'ee/api';
import * as actions from 'ee/geo_designs/store/actions'; import * as actions from 'ee/geo_designs/store/actions';
import * as types from 'ee/geo_designs/store/mutation_types'; import * as types from 'ee/geo_designs/store/mutation_types';
import createState from 'ee/geo_designs/store/state'; import createState from 'ee/geo_designs/store/state';
...@@ -19,15 +18,9 @@ jest.mock('~/vue_shared/plugins/global_toast'); ...@@ -19,15 +18,9 @@ jest.mock('~/vue_shared/plugins/global_toast');
describe('GeoDesigns Store Actions', () => { describe('GeoDesigns Store Actions', () => {
let state; let state;
let mock;
beforeEach(() => { beforeEach(() => {
state = createState(MOCK_REPLICABLE_TYPE); state = createState(MOCK_REPLICABLE_TYPE);
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.restore();
}); });
describe('requestReplicableItems', () => { describe('requestReplicableItems', () => {
...@@ -66,7 +59,6 @@ describe('GeoDesigns Store Actions', () => { ...@@ -66,7 +59,6 @@ describe('GeoDesigns Store Actions', () => {
[], [],
() => { () => {
expect(flash).toHaveBeenCalledTimes(1); expect(flash).toHaveBeenCalledTimes(1);
flash.mockClear();
}, },
); );
}); });
...@@ -75,35 +67,68 @@ describe('GeoDesigns Store Actions', () => { ...@@ -75,35 +67,68 @@ describe('GeoDesigns Store Actions', () => {
describe('fetchDesigns', () => { describe('fetchDesigns', () => {
describe('on success', () => { describe('on success', () => {
beforeEach(() => { beforeEach(() => {
mock jest.spyOn(Api, 'getGeoReplicableItems').mockResolvedValue(MOCK_BASIC_FETCH_RESPONSE);
.onGet()
.replyOnce(200, MOCK_BASIC_FETCH_RESPONSE.data, MOCK_BASIC_FETCH_RESPONSE.headers);
}); });
it('should dispatch the request with correct replicable param and success actions', () => { describe('with no params set', () => {
function fetchReplicableItemsCall() { const defaultParams = {
const callHistory = mock.history.get[0]; page: 1,
search: null,
expect(callHistory.url).toContain(`/geo_replication/${MOCK_REPLICABLE_TYPE}`); sync_status: null,
} };
it('should call getGeoReplicableItems with default queryParams', () => {
testAction(
actions.fetchDesigns,
{},
state,
[],
[
{ type: 'requestReplicableItems' },
{ type: 'receiveReplicableItemsSuccess', payload: MOCK_BASIC_FETCH_DATA_MAP },
],
() => {
expect(Api.getGeoReplicableItems).toHaveBeenCalledWith(
MOCK_REPLICABLE_TYPE,
defaultParams,
);
},
);
});
});
testAction( describe('with params set', () => {
actions.fetchDesigns, beforeEach(() => {
{}, state.currentPage = 3;
state, state.searchFilter = 'test search';
[], state.currentFilterIndex = 2;
[ });
{ type: 'requestReplicableItems' },
{ type: 'receiveReplicableItemsSuccess', payload: MOCK_BASIC_FETCH_DATA_MAP }, it('should call getGeoReplicableItems with default queryParams', () => {
], testAction(
fetchReplicableItemsCall, actions.fetchDesigns,
); {},
state,
[],
[
{ type: 'requestReplicableItems' },
{ type: 'receiveReplicableItemsSuccess', payload: MOCK_BASIC_FETCH_DATA_MAP },
],
() => {
expect(Api.getGeoReplicableItems).toHaveBeenCalledWith(MOCK_REPLICABLE_TYPE, {
page: 3,
search: 'test search',
sync_status: state.filterOptions[2],
});
},
);
});
}); });
}); });
describe('on error', () => { describe('on error', () => {
beforeEach(() => { beforeEach(() => {
mock.onGet().replyOnce(500, {}); jest.spyOn(Api, 'getGeoReplicableItems').mockRejectedValue(new Error(500));
}); });
it('should dispatch the request and error actions', done => { it('should dispatch the request and error actions', done => {
...@@ -119,73 +144,6 @@ describe('GeoDesigns Store Actions', () => { ...@@ -119,73 +144,6 @@ describe('GeoDesigns Store Actions', () => {
}); });
}); });
describe('queryParams', () => {
beforeEach(() => {
mock
.onGet()
.replyOnce(200, MOCK_BASIC_FETCH_RESPONSE.data, MOCK_BASIC_FETCH_RESPONSE.headers);
});
describe('no params set', () => {
it('should call fetchDesigns with default queryParams and correct replicable params', () => {
state.isLoading = true;
function fetchReplicableItemsCall() {
const callHistory = mock.history.get[0];
expect(callHistory.url).toContain(`/geo_replication/${MOCK_REPLICABLE_TYPE}`);
expect(callHistory.params.page).toEqual(1);
expect(callHistory.params.search).toBeNull();
expect(callHistory.params.sync_status).toBeNull();
}
testAction(
actions.fetchDesigns,
{},
state,
[],
[
{ type: 'requestReplicableItems' },
{ type: 'receiveReplicableItemsSuccess', payload: MOCK_BASIC_FETCH_DATA_MAP },
],
fetchReplicableItemsCall,
);
});
});
describe('with params set', () => {
it('should call fetchDesigns with queryParams', () => {
state.isLoading = true;
state.currentPage = 3;
state.searchFilter = 'test search';
state.currentFilterIndex = 2;
function fetchReplicableItemsCall() {
const callHistory = mock.history.get[0];
expect(callHistory.url).toContain(`/geo_replication/${MOCK_REPLICABLE_TYPE}`);
expect(callHistory.params.page).toEqual(state.currentPage);
expect(callHistory.params.search).toEqual(state.searchFilter);
expect(callHistory.params.sync_status).toEqual(
state.filterOptions[state.currentFilterIndex],
);
}
testAction(
actions.fetchDesigns,
{},
state,
[],
[
{ type: 'requestReplicableItems' },
{ type: 'receiveReplicableItemsSuccess', payload: MOCK_BASIC_FETCH_DATA_MAP },
],
fetchReplicableItemsCall,
);
});
});
});
describe('requestInitiateAllReplicableSyncs', () => { describe('requestInitiateAllReplicableSyncs', () => {
it('should commit mutation REQUEST_INITIATE_ALL_REPLICABLE_SYNCS', done => { it('should commit mutation REQUEST_INITIATE_ALL_REPLICABLE_SYNCS', done => {
testAction( testAction(
...@@ -225,7 +183,6 @@ describe('GeoDesigns Store Actions', () => { ...@@ -225,7 +183,6 @@ describe('GeoDesigns Store Actions', () => {
[], [],
() => { () => {
expect(flash).toHaveBeenCalledTimes(1); expect(flash).toHaveBeenCalledTimes(1);
flash.mockClear();
}, },
); );
}); });
...@@ -237,17 +194,12 @@ describe('GeoDesigns Store Actions', () => { ...@@ -237,17 +194,12 @@ describe('GeoDesigns Store Actions', () => {
describe('on success', () => { describe('on success', () => {
beforeEach(() => { beforeEach(() => {
action = ACTION_TYPES.RESYNC; action = ACTION_TYPES.RESYNC;
jest
mock.onPost().replyOnce(201, MOCK_BASIC_POST_RESPONSE); .spyOn(Api, 'initiateAllGeoReplicableSyncs')
.mockResolvedValue(MOCK_BASIC_POST_RESPONSE);
}); });
it('should dispatch the request with correct replicable param and success actions', () => { it('should dispatch the request with correct replicable param and success actions', () => {
function fetchReplicableItemsCall() {
const callHistory = mock.history.post[0];
expect(callHistory.url).toContain(`/geo_replication/${MOCK_REPLICABLE_TYPE}`);
}
testAction( testAction(
actions.initiateAllDesignSyncs, actions.initiateAllDesignSyncs,
action, action,
...@@ -257,7 +209,12 @@ describe('GeoDesigns Store Actions', () => { ...@@ -257,7 +209,12 @@ describe('GeoDesigns Store Actions', () => {
{ type: 'requestInitiateAllReplicableSyncs' }, { type: 'requestInitiateAllReplicableSyncs' },
{ type: 'receiveInitiateAllReplicableSyncsSuccess', payload: { action } }, { type: 'receiveInitiateAllReplicableSyncsSuccess', payload: { action } },
], ],
fetchReplicableItemsCall, () => {
expect(Api.initiateAllGeoReplicableSyncs).toHaveBeenCalledWith(
MOCK_REPLICABLE_TYPE,
action,
);
},
); );
}); });
}); });
...@@ -265,8 +222,7 @@ describe('GeoDesigns Store Actions', () => { ...@@ -265,8 +222,7 @@ describe('GeoDesigns Store Actions', () => {
describe('on error', () => { describe('on error', () => {
beforeEach(() => { beforeEach(() => {
action = ACTION_TYPES.RESYNC; action = ACTION_TYPES.RESYNC;
jest.spyOn(Api, 'initiateAllGeoReplicableSyncs').mockRejectedValue(new Error(500));
mock.onPost().replyOnce(500);
}); });
it('should dispatch the request and error actions', done => { it('should dispatch the request and error actions', done => {
...@@ -324,7 +280,6 @@ describe('GeoDesigns Store Actions', () => { ...@@ -324,7 +280,6 @@ describe('GeoDesigns Store Actions', () => {
[], [],
() => { () => {
expect(flash).toHaveBeenCalledTimes(1); expect(flash).toHaveBeenCalledTimes(1);
flash.mockClear();
}, },
); );
}); });
...@@ -340,17 +295,10 @@ describe('GeoDesigns Store Actions', () => { ...@@ -340,17 +295,10 @@ describe('GeoDesigns Store Actions', () => {
action = ACTION_TYPES.RESYNC; action = ACTION_TYPES.RESYNC;
projectId = 1; projectId = 1;
name = 'test'; name = 'test';
jest.spyOn(Api, 'initiateGeoReplicableSync').mockResolvedValue(MOCK_BASIC_POST_RESPONSE);
mock.onPut().replyOnce(201, MOCK_BASIC_POST_RESPONSE);
}); });
it('should dispatch the request with correct replicable param and success actions', () => { it('should dispatch the request with correct replicable param and success actions', () => {
function fetchReplicableItemsCall() {
const callHistory = mock.history.put[0];
expect(callHistory.url).toContain(`/geo_replication/${MOCK_REPLICABLE_TYPE}`);
}
testAction( testAction(
actions.initiateDesignSync, actions.initiateDesignSync,
{ projectId, name, action }, { projectId, name, action },
...@@ -360,7 +308,12 @@ describe('GeoDesigns Store Actions', () => { ...@@ -360,7 +308,12 @@ describe('GeoDesigns Store Actions', () => {
{ type: 'requestInitiateReplicableSync' }, { type: 'requestInitiateReplicableSync' },
{ type: 'receiveInitiateReplicableSyncSuccess', payload: { name, action } }, { type: 'receiveInitiateReplicableSyncSuccess', payload: { name, action } },
], ],
fetchReplicableItemsCall, () => {
expect(Api.initiateGeoReplicableSync).toHaveBeenCalledWith(MOCK_REPLICABLE_TYPE, {
projectId,
action,
});
},
); );
}); });
}); });
...@@ -370,8 +323,7 @@ describe('GeoDesigns Store Actions', () => { ...@@ -370,8 +323,7 @@ describe('GeoDesigns Store Actions', () => {
action = ACTION_TYPES.RESYNC; action = ACTION_TYPES.RESYNC;
projectId = 1; projectId = 1;
name = 'test'; name = 'test';
jest.spyOn(Api, 'initiateGeoReplicableSync').mockRejectedValue(new Error(500));
mock.onPut().replyOnce(500);
}); });
it('should dispatch the request and error actions', done => { it('should dispatch the request and error actions', done => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment