Commit 8686325f authored by Phil Hughes's avatar Phil Hughes

Merge branch '37271-normalize-vuex-actions-mutations-ee' into 'master'

Normalize Vuex actions/mutations in License Management

See merge request gitlab-org/gitlab!24471
parents d15aa586 9e7d2ed9
......@@ -37,10 +37,10 @@ export default {
this.setAPISettings({
apiUrlManageLicenses: this.apiUrl,
});
this.loadManagedLicenses();
this.fetchManagedLicenses();
},
methods: {
...mapActions(['loadManagedLicenses', 'setAPISettings', 'setLicenseApproval']),
...mapActions(['fetchManagedLicenses', 'setAPISettings', 'setLicenseApproval']),
openAddLicenseForm() {
this.formIsOpen = true;
},
......
......@@ -95,10 +95,10 @@ export default {
licensesApiPath,
});
this.loadParsedLicenseReport();
this.fetchParsedLicenseReport();
},
methods: {
...mapActions(['setAPISettings', 'loadParsedLicenseReport']),
...mapActions(['setAPISettings', 'fetchParsedLicenseReport']),
},
};
</script>
......
......@@ -20,7 +20,7 @@ export const requestDeleteLicense = ({ commit }) => {
};
export const receiveDeleteLicense = ({ commit, dispatch }) => {
commit(types.RECEIVE_DELETE_LICENSE);
dispatch('loadManagedLicenses');
dispatch('fetchManagedLicenses');
};
export const receiveDeleteLicenseError = ({ commit }, error) => {
commit(types.RECEIVE_DELETE_LICENSE_ERROR, error);
......@@ -39,57 +39,53 @@ export const deleteLicense = ({ dispatch, state }) => {
});
};
export const requestLoadManagedLicenses = ({ commit }) => {
commit(types.REQUEST_LOAD_MANAGED_LICENSES);
export const requestManagedLicenses = ({ commit }) => {
commit(types.REQUEST_MANAGED_LICENSES);
};
export const receiveLoadManagedLicenses = ({ commit }, licenses) => {
commit(types.RECEIVE_LOAD_MANAGED_LICENSES, licenses);
export const receiveManagedLicensesSuccess = ({ commit }, licenses) => {
commit(types.RECEIVE_MANAGED_LICENSES_SUCCESS, licenses);
};
export const receiveLoadManagedLicensesError = ({ commit }, error) => {
commit(types.RECEIVE_LOAD_MANAGED_LICENSES_ERROR, error);
export const receiveManagedLicensesError = ({ commit }, error) => {
commit(types.RECEIVE_MANAGED_LICENSES_ERROR, error);
};
export const loadManagedLicenses = ({ dispatch, state }) => {
dispatch('requestLoadManagedLicenses');
export const fetchManagedLicenses = ({ dispatch, state }) => {
dispatch('requestManagedLicenses');
const { apiUrlManageLicenses } = state;
return axios
.get(apiUrlManageLicenses, { params: { per_page: 100 } })
.then(({ data }) => {
dispatch('receiveLoadManagedLicenses', data);
dispatch('receiveManagedLicensesSuccess', data);
})
.catch(error => {
dispatch('receiveLoadManagedLicensesError', error);
dispatch('receiveManagedLicensesError', error);
});
};
export const requestLoadParsedLicenseReport = ({ commit }) => {
commit(types.REQUEST_LOAD_PARSED_LICENSE_REPORT);
export const requestParsedLicenseReport = ({ commit }) => {
commit(types.REQUEST_PARSED_LICENSE_REPORT);
};
export const receiveLoadParsedLicenseReport = ({ commit }, reports) => {
commit(types.RECEIVE_LOAD_PARSED_LICENSE_REPORT, reports);
export const receiveParsedLicenseReportSuccess = ({ commit }, reports) => {
commit(types.RECEIVE_PARSED_LICENSE_REPORT_SUCCESS, reports);
};
export const receiveLoadParsedLicenseReportError = ({ commit }, error) => {
commit(types.RECEIVE_LOAD_PARSED_LICENSE_REPORT_ERROR, error);
export const receiveParsedLicenseReportError = ({ commit }, error) => {
commit(types.RECEIVE_PARSED_LICENSE_REPORT_ERROR, error);
};
export const loadParsedLicenseReport = ({ dispatch, state }) => {
dispatch('requestLoadParsedLicenseReport');
export const fetchParsedLicenseReport = ({ dispatch, state }) => {
dispatch('requestParsedLicenseReport');
pollUntilComplete(state.licensesApiPath)
.then(({ data }) => {
const newLicenses = (data.new_licenses || data).map(convertToOldReportFormat);
const existingLicenses = (data.existing_licenses || []).map(convertToOldReportFormat);
dispatch('receiveLoadParsedLicenseReport', { newLicenses, existingLicenses });
dispatch('receiveParsedLicenseReportSuccess', { newLicenses, existingLicenses });
})
.catch(error => {
dispatch('receiveLoadLicenseReportError', error);
dispatch('receiveParsedLicenseReportError', error);
});
};
export const receiveLoadLicenseReportError = ({ commit }, error) => {
commit(types.RECEIVE_LOAD_LICENSE_REPORT_ERROR, error);
};
export const requestSetLicenseApproval = ({ commit }) => {
commit(types.REQUEST_SET_LICENSE_APPROVAL);
};
......@@ -101,9 +97,9 @@ export const receiveSetLicenseApproval = ({ commit, dispatch, state }) => {
// the project settings page.
// https://gitlab.com/gitlab-org/gitlab/issues/201867
if (state.licensesApiPath) {
dispatch('loadParsedLicenseReport');
dispatch('fetchParsedLicenseReport');
} else {
dispatch('loadManagedLicenses');
dispatch('fetchManagedLicenses');
}
};
export const receiveSetLicenseApprovalError = ({ commit }, error) => {
......
export const RECEIVE_DELETE_LICENSE = 'RECEIVE_DELETE_LICENSE';
export const RECEIVE_DELETE_LICENSE_ERROR = 'RECEIVE_DELETE_LICENSE_ERROR';
export const RECEIVE_LOAD_LICENSE_REPORT_ERROR = 'RECEIVE_LOAD_LICENSE_REPORT_ERROR';
export const RECEIVE_LOAD_MANAGED_LICENSES = 'RECEIVE_LOAD_MANAGED_LICENSES';
export const RECEIVE_LOAD_MANAGED_LICENSES_ERROR = 'RECEIVE_LOAD_MANAGED_LICENSES_ERROR';
export const RECEIVE_LOAD_PARSED_LICENSE_REPORT = 'RECEIVE_LOAD_PARSED_LICENSE_REPORT';
export const RECEIVE_LOAD_PARSED_LICENSE_REPORT_ERROR = 'RECEIVE_LOAD_PARSED_LICENSE_REPORT_ERROR';
export const RECEIVE_MANAGED_LICENSES_SUCCESS = 'RECEIVE_MANAGED_LICENSES_SUCCESS';
export const RECEIVE_MANAGED_LICENSES_ERROR = 'RECEIVE_MANAGED_LICENSES_ERROR';
export const RECEIVE_PARSED_LICENSE_REPORT_SUCCESS = 'RECEIVE_PARSED_LICENSE_REPORT_SUCCESS';
export const RECEIVE_PARSED_LICENSE_REPORT_ERROR = 'RECEIVE_PARSED_LICENSE_REPORT_ERROR';
export const RECEIVE_SET_LICENSE_APPROVAL = 'RECEIVE_SET_LICENSE_APPROVAL';
export const RECEIVE_SET_LICENSE_APPROVAL_ERROR = 'RECEIVE_SET_LICENSE_APPROVAL_ERROR';
export const REQUEST_DELETE_LICENSE = 'REQUEST_DELETE_LICENSE';
export const REQUEST_LOAD_MANAGED_LICENSES = 'REQUEST_LOAD_MANAGED_LICENSES';
export const REQUEST_LOAD_PARSED_LICENSE_REPORT = 'REQUEST_LOAD_PARSED_LICENSE_REPORT';
export const REQUEST_MANAGED_LICENSES = 'REQUEST_MANAGED_LICENSES';
export const REQUEST_PARSED_LICENSE_REPORT = 'REQUEST_PARSED_LICENSE_REPORT';
export const REQUEST_SET_LICENSE_APPROVAL = 'REQUEST_SET_LICENSE_APPROVAL';
export const RESET_LICENSE_IN_MODAL = 'RESET_LICENSE_IN_MODAL';
export const SET_API_SETTINGS = 'SET_API_SETTINGS';
......
......@@ -16,7 +16,7 @@ export default {
Object.assign(state, data);
},
[types.RECEIVE_LOAD_MANAGED_LICENSES](state, licenses = []) {
[types.RECEIVE_MANAGED_LICENSES_SUCCESS](state, licenses = []) {
const managedLicenses = licenses.map(normalizeLicense).reverse();
Object.assign(state, {
......@@ -25,20 +25,20 @@ export default {
loadManagedLicensesError: false,
});
},
[types.RECEIVE_LOAD_MANAGED_LICENSES_ERROR](state, error) {
[types.RECEIVE_MANAGED_LICENSES_ERROR](state, error) {
Object.assign(state, {
managedLicenses: [],
isLoadingManagedLicenses: false,
loadManagedLicensesError: error,
});
},
[types.REQUEST_LOAD_MANAGED_LICENSES](state) {
[types.REQUEST_MANAGED_LICENSES](state) {
Object.assign(state, {
isLoadingManagedLicenses: true,
});
},
[types.RECEIVE_LOAD_PARSED_LICENSE_REPORT](state, { newLicenses, existingLicenses }) {
[types.RECEIVE_PARSED_LICENSE_REPORT_SUCCESS](state, { newLicenses, existingLicenses }) {
Object.assign(state, {
newLicenses,
existingLicenses,
......@@ -46,26 +46,18 @@ export default {
loadLicenseReportError: false,
});
},
[types.RECEIVE_LOAD_PARSED_LICENSE_REPORT_ERROR](state, error) {
[types.RECEIVE_PARSED_LICENSE_REPORT_ERROR](state, error) {
Object.assign(state, {
isLoadingLicenseReport: false,
loadLicenseReportError: error,
});
},
[types.REQUEST_LOAD_PARSED_LICENSE_REPORT](state) {
[types.REQUEST_PARSED_LICENSE_REPORT](state) {
Object.assign(state, {
isLoadingLicenseReport: true,
});
},
[types.RECEIVE_LOAD_LICENSE_REPORT_ERROR](state, error) {
Object.assign(state, {
managedLicenses: [],
isLoadingLicenseReport: false,
loadLicenseReportError: error,
});
},
[types.RECEIVE_DELETE_LICENSE](state) {
Object.assign(state, {
isDeleting: false,
......
......@@ -37,7 +37,7 @@ describe('LicenseManagement', () => {
...state,
},
actions: {
loadManagedLicenses: noop,
fetchManagedLicenses: noop,
setAPISettings: noop,
setLicenseApproval: noop,
...actionMocks,
......@@ -111,13 +111,13 @@ describe('LicenseManagement', () => {
it('should set api settings after mount and init API calls', () => {
const setAPISettingsMock = jest.fn();
const loadManagedLicensesMock = jest.fn();
const fetchManagedLicensesMock = jest.fn();
createComponent({
state: { isLoadingManagedLicenses: false },
actionMocks: {
setAPISettings: setAPISettingsMock,
loadManagedLicenses: loadManagedLicensesMock,
fetchManagedLicenses: fetchManagedLicensesMock,
},
});
......@@ -129,6 +129,6 @@ describe('LicenseManagement', () => {
undefined,
);
expect(loadManagedLicensesMock).toHaveBeenCalledWith(expect.any(Object), undefined, undefined);
expect(fetchManagedLicensesMock).toHaveBeenCalledWith(expect.any(Object), undefined, undefined);
});
});
......@@ -51,8 +51,8 @@ describe('License Report MR Widget', () => {
const defaultActions = {
setAPISettings: () => {},
loadManagedLicenses: () => {},
loadParsedLicenseReport: () => {},
fetchManagedLicenses: () => {},
fetchParsedLicenseReport: () => {},
};
const mountComponent = ({
......@@ -219,7 +219,9 @@ describe('License Report MR Widget', () => {
it('should init store after mount', () => {
const actions = {
setAPISettings: jasmine.createSpy('setAPISettings').and.callFake(() => {}),
loadParsedLicenseReport: jasmine.createSpy('loadParsedLicenseReport').and.callFake(() => {}),
fetchParsedLicenseReport: jasmine
.createSpy('fetchParsedLicenseReport')
.and.callFake(() => {}),
};
vm = mountComponent({ actions });
......@@ -233,7 +235,7 @@ describe('License Report MR Widget', () => {
undefined,
);
expect(actions.loadParsedLicenseReport).toHaveBeenCalledWith(
expect(actions.fetchParsedLicenseReport).toHaveBeenCalledWith(
jasmine.any(Object),
undefined,
undefined,
......
......@@ -125,7 +125,7 @@ describe('License store mutations', () => {
});
});
describe('RECEIVE_LOAD_MANAGED_LICENSES', () => {
describe('RECEIVE_MANAGED_LICENSES_SUCCESS', () => {
it('sets isLoadingManagedLicenses and loadManagedLicensesError to false and saves managed licenses', () => {
store.replaceState({
...store.state,
......@@ -134,7 +134,7 @@ describe('License store mutations', () => {
loadManagedLicensesError: true,
});
store.commit(types.RECEIVE_LOAD_MANAGED_LICENSES, [
store.commit(types.RECEIVE_MANAGED_LICENSES_SUCCESS, [
{ name: 'Foo', approval_status: LICENSE_APPROVAL_STATUS.approved },
]);
......@@ -147,7 +147,7 @@ describe('License store mutations', () => {
});
});
describe('RECEIVE_LOAD_MANAGED_LICENSES_ERROR', () => {
describe('RECEIVE_MANAGED_LICENSES_ERROR', () => {
it('sets isLoadingManagedLicenses to true and saves the error', () => {
const error = new Error('test');
store.replaceState({
......@@ -156,50 +156,34 @@ describe('License store mutations', () => {
loadManagedLicensesError: false,
});
store.commit(types.RECEIVE_LOAD_MANAGED_LICENSES_ERROR, error);
store.commit(types.RECEIVE_MANAGED_LICENSES_ERROR, error);
expect(store.state.isLoadingManagedLicenses).toBe(false);
expect(store.state.loadManagedLicensesError).toBe(error);
});
});
describe('REQUEST_LOAD_MANAGED_LICENSES', () => {
describe('REQUEST_MANAGED_LICENSES', () => {
it('sets isLoadingManagedLicenses to true', () => {
store.replaceState({
...store.state,
isLoadingManagedLicenses: true,
});
store.commit(types.REQUEST_LOAD_MANAGED_LICENSES);
store.commit(types.REQUEST_MANAGED_LICENSES);
expect(store.state.isLoadingManagedLicenses).toBe(true);
});
});
describe('RECEIVE_LOAD_LICENSE_REPORT_ERROR', () => {
it('sets isLoadingLicenseReport to true and saves the error', () => {
const error = new Error('test');
store.replaceState({
...store.state,
isLoadingLicenseReport: true,
loadLicenseReportError: false,
});
store.commit(types.RECEIVE_LOAD_LICENSE_REPORT_ERROR, error);
expect(store.state.isLoadingLicenseReport).toBe(false);
expect(store.state.loadLicenseReportError).toBe(error);
});
});
describe('RECEIVE_LOAD_PARSED_LICENSE_REPORT', () => {
describe('RECEIVE_PARSED_LICENSE_REPORT_SUCCESS', () => {
const newLicenses = [];
const existingLicenses = [];
beforeEach(() => {
store.state.isLoadingLicenseReport = true;
store.state.loadLicenseReportError = new Error('test');
store.commit(types.RECEIVE_LOAD_PARSED_LICENSE_REPORT, { newLicenses, existingLicenses });
store.commit(types.RECEIVE_PARSED_LICENSE_REPORT_SUCCESS, { newLicenses, existingLicenses });
});
it('should set the new and existing reports', () => {
......@@ -213,12 +197,12 @@ describe('License store mutations', () => {
});
});
describe('RECEIVE_LOAD_PARSED_LICENSE_REPORT_ERROR', () => {
describe('RECEIVE_PARSED_LICENSE_REPORT_ERROR', () => {
const error = new Error('test');
beforeEach(() => {
store.state.isLoadingLicenseReport = true;
store.state.loadLicenseReportError = false;
store.commit(types.RECEIVE_LOAD_PARSED_LICENSE_REPORT_ERROR, error);
store.commit(types.RECEIVE_PARSED_LICENSE_REPORT_ERROR, error);
});
it('should set the error on the state', () => {
......@@ -230,10 +214,10 @@ describe('License store mutations', () => {
});
});
describe('REQUEST_LOAD_PARSED_LICENSE_REPORT', () => {
describe('REQUEST_PARSED_LICENSE_REPORT', () => {
beforeEach(() => {
store.state.isLoadingLicenseReport = false;
store.commit(types.REQUEST_LOAD_PARSED_LICENSE_REPORT);
store.commit(types.REQUEST_PARSED_LICENSE_REPORT);
});
it('should initiate loading', () => {
......
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