Commit 68dbdca7 authored by Phil Hughes's avatar Phil Hughes

Merge branch '58252-web-ide-dropdown-duplicates' into 'master'

Resolve "Web IDE template dropdown showing duplicates"

Closes #58252

See merge request gitlab-org/gitlab-ce!27237
parents 712a63a7 7dc2a7aa
...@@ -23,22 +23,27 @@ export const receiveTemplateTypesError = ({ commit, dispatch }) => { ...@@ -23,22 +23,27 @@ export const receiveTemplateTypesError = ({ commit, dispatch }) => {
export const receiveTemplateTypesSuccess = ({ commit }, templates) => export const receiveTemplateTypesSuccess = ({ commit }, templates) =>
commit(types.RECEIVE_TEMPLATE_TYPES_SUCCESS, templates); commit(types.RECEIVE_TEMPLATE_TYPES_SUCCESS, templates);
export const fetchTemplateTypes = ({ dispatch, state, rootState }, page = 1) => { export const fetchTemplateTypes = ({ dispatch, state, rootState }) => {
if (!Object.keys(state.selectedTemplateType).length) return Promise.reject(); if (!Object.keys(state.selectedTemplateType).length) return Promise.reject();
dispatch('requestTemplateTypes'); dispatch('requestTemplateTypes');
return Api.projectTemplates(rootState.currentProjectId, state.selectedTemplateType.key, { page }) const fetchPages = (page = 1, prev = []) =>
Api.projectTemplates(rootState.currentProjectId, state.selectedTemplateType.key, {
page,
per_page: 100,
})
.then(({ data, headers }) => { .then(({ data, headers }) => {
const nextPage = parseInt(normalizeHeaders(headers)['X-NEXT-PAGE'], 10); const nextPage = parseInt(normalizeHeaders(headers)['X-NEXT-PAGE'], 10);
const nextData = prev.concat(data);
dispatch('receiveTemplateTypesSuccess', data); dispatch('receiveTemplateTypesSuccess', nextData);
if (nextPage) { return nextPage ? fetchPages(nextPage, nextData) : nextData;
dispatch('fetchTemplateTypes', nextPage);
}
}) })
.catch(() => dispatch('receiveTemplateTypesError')); .catch(() => dispatch('receiveTemplateTypesError'));
return fetchPages();
}; };
export const setSelectedTemplateType = ({ commit, dispatch, rootGetters }, type) => { export const setSelectedTemplateType = ({ commit, dispatch, rootGetters }, type) => {
......
...@@ -3,13 +3,14 @@ import * as types from './mutation_types'; ...@@ -3,13 +3,14 @@ import * as types from './mutation_types';
export default { export default {
[types.REQUEST_TEMPLATE_TYPES](state) { [types.REQUEST_TEMPLATE_TYPES](state) {
state.isLoading = true; state.isLoading = true;
state.templates = [];
}, },
[types.RECEIVE_TEMPLATE_TYPES_ERROR](state) { [types.RECEIVE_TEMPLATE_TYPES_ERROR](state) {
state.isLoading = false; state.isLoading = false;
}, },
[types.RECEIVE_TEMPLATE_TYPES_SUCCESS](state, templates) { [types.RECEIVE_TEMPLATE_TYPES_SUCCESS](state, templates) {
state.isLoading = false; state.isLoading = false;
state.templates = state.templates.concat(templates); state.templates = templates;
}, },
[types.SET_SELECTED_TEMPLATE_TYPE](state, type) { [types.SET_SELECTED_TEMPLATE_TYPE](state, type) {
state.selectedTemplateType = type; state.selectedTemplateType = type;
......
---
title: Resolve Web IDE template dropdown showing duplicates
merge_request: 27237
author:
type: fixed
...@@ -2,6 +2,9 @@ import createState from '~/ide/stores/modules/file_templates/state'; ...@@ -2,6 +2,9 @@ import createState from '~/ide/stores/modules/file_templates/state';
import * as types from '~/ide/stores/modules/file_templates/mutation_types'; import * as types from '~/ide/stores/modules/file_templates/mutation_types';
import mutations from '~/ide/stores/modules/file_templates/mutations'; import mutations from '~/ide/stores/modules/file_templates/mutations';
const mockFileTemplates = [['MIT'], ['CC']];
const mockTemplateType = 'test';
describe('IDE file templates mutations', () => { describe('IDE file templates mutations', () => {
let state; let state;
...@@ -10,11 +13,21 @@ describe('IDE file templates mutations', () => { ...@@ -10,11 +13,21 @@ describe('IDE file templates mutations', () => {
}); });
describe(`${types.REQUEST_TEMPLATE_TYPES}`, () => { describe(`${types.REQUEST_TEMPLATE_TYPES}`, () => {
it('sets isLoading', () => { it('sets loading to true', () => {
state.isLoading = false;
mutations[types.REQUEST_TEMPLATE_TYPES](state); mutations[types.REQUEST_TEMPLATE_TYPES](state);
expect(state.isLoading).toBe(true); expect(state.isLoading).toBe(true);
}); });
it('sets templates to an empty array', () => {
state.templates = mockFileTemplates;
mutations[types.REQUEST_TEMPLATE_TYPES](state);
expect(state.templates).toEqual([]);
});
}); });
describe(`${types.RECEIVE_TEMPLATE_TYPES_ERROR}`, () => { describe(`${types.RECEIVE_TEMPLATE_TYPES_ERROR}`, () => {
...@@ -31,29 +44,33 @@ describe('IDE file templates mutations', () => { ...@@ -31,29 +44,33 @@ describe('IDE file templates mutations', () => {
it('sets isLoading to false', () => { it('sets isLoading to false', () => {
state.isLoading = true; state.isLoading = true;
mutations[types.RECEIVE_TEMPLATE_TYPES_SUCCESS](state, []); mutations[types.RECEIVE_TEMPLATE_TYPES_SUCCESS](state, mockFileTemplates);
expect(state.isLoading).toBe(false); expect(state.isLoading).toBe(false);
}); });
it('sets templates', () => { it('sets templates to payload', () => {
mutations[types.RECEIVE_TEMPLATE_TYPES_SUCCESS](state, ['test']); state.templates = ['test'];
mutations[types.RECEIVE_TEMPLATE_TYPES_SUCCESS](state, mockFileTemplates);
expect(state.templates).toEqual(['test']); expect(state.templates).toEqual(mockFileTemplates);
}); });
}); });
describe(`${types.SET_SELECTED_TEMPLATE_TYPE}`, () => { describe(`${types.SET_SELECTED_TEMPLATE_TYPE}`, () => {
it('sets selectedTemplateType', () => { it('sets templates type to selected type', () => {
mutations[types.SET_SELECTED_TEMPLATE_TYPE](state, 'type'); state.selectedTemplateType = '';
expect(state.selectedTemplateType).toBe('type'); mutations[types.SET_SELECTED_TEMPLATE_TYPE](state, mockTemplateType);
expect(state.selectedTemplateType).toBe(mockTemplateType);
}); });
it('clears templates', () => { it('sets templates to empty array', () => {
state.templates = ['test']; state.templates = mockFileTemplates;
mutations[types.SET_SELECTED_TEMPLATE_TYPE](state, 'type'); mutations[types.SET_SELECTED_TEMPLATE_TYPE](state, mockTemplateType);
expect(state.templates).toEqual([]); expect(state.templates).toEqual([]);
}); });
...@@ -61,6 +78,8 @@ describe('IDE file templates mutations', () => { ...@@ -61,6 +78,8 @@ describe('IDE file templates mutations', () => {
describe(`${types.SET_UPDATE_SUCCESS}`, () => { describe(`${types.SET_UPDATE_SUCCESS}`, () => {
it('sets updateSuccess', () => { it('sets updateSuccess', () => {
state.updateSuccess = false;
mutations[types.SET_UPDATE_SUCCESS](state, true); mutations[types.SET_UPDATE_SUCCESS](state, true);
expect(state.updateSuccess).toBe(true); expect(state.updateSuccess).toBe(true);
......
...@@ -69,18 +69,16 @@ describe('IDE file templates actions', () => { ...@@ -69,18 +69,16 @@ describe('IDE file templates actions', () => {
describe('fetchTemplateTypes', () => { describe('fetchTemplateTypes', () => {
describe('success', () => { describe('success', () => {
let nextPage; const pages = [[{ name: 'MIT' }], [{ name: 'Apache' }], [{ name: 'CC' }]];
beforeEach(() => { beforeEach(() => {
mock.onGet(/api\/(.*)\/templates\/licenses/).replyOnce(() => [ mock.onGet(/api\/(.*)\/templates\/licenses/).reply(({ params }) => {
200, const pageNum = params.page;
[ const page = pages[pageNum - 1];
{ const hasNextPage = pageNum < pages.length;
name: 'MIT',
}, return [200, page, hasNextPage ? { 'X-NEXT-PAGE': pageNum + 1 } : {}];
], });
{ 'X-NEXT-PAGE': nextPage },
]);
}); });
it('rejects if selectedTemplateType is empty', done => { it('rejects if selectedTemplateType is empty', done => {
...@@ -112,43 +110,15 @@ describe('IDE file templates actions', () => { ...@@ -112,43 +110,15 @@ describe('IDE file templates actions', () => {
}, },
{ {
type: 'receiveTemplateTypesSuccess', type: 'receiveTemplateTypesSuccess',
payload: [ payload: pages[0],
{
name: 'MIT',
},
],
},
],
done,
);
});
it('dispatches actions for next page', done => {
nextPage = '2';
state.selectedTemplateType = {
key: 'licenses',
};
testAction(
actions.fetchTemplateTypes,
null,
state,
[],
[
{
type: 'requestTemplateTypes',
}, },
{ {
type: 'receiveTemplateTypesSuccess', type: 'receiveTemplateTypesSuccess',
payload: [ payload: pages[0].concat(pages[1]),
{
name: 'MIT',
},
],
}, },
{ {
type: 'fetchTemplateTypes', type: 'receiveTemplateTypesSuccess',
payload: 2, payload: pages[0].concat(pages[1]).concat(pages[2]),
}, },
], ],
done, 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