Commit e59611de authored by Mike Greiling's avatar Mike Greiling

Merge branch 'winh-issue-boards-loadList-ee' into 'master'

Move loadList() from BoardListSelector to BoardsStoreEE

See merge request gitlab-org/gitlab-ee!13203
parents 8b390ac9 58ef3a97
import Vue from 'vue';
import { __, sprintf } from '~/locale';
import Flash from '~/flash';
import axios from '~/lib/utils/axios_utils';
import boardsStore from '~/boards/stores/boards_store';
import ListContainer from './list_container.vue';
......@@ -30,24 +27,9 @@ export default Vue.extend({
},
methods: {
loadList() {
if (this.store.state[this.listType].length) {
return Promise.resolve();
}
return axios
.get(this.listPath)
.then(({ data }) => {
this.loading = false;
this.store.state[this.listType] = data;
})
.catch(() => {
this.loading = false;
Flash(
sprintf(__('Something went wrong while fetching %{listType} list'), {
listType: this.listType,
}),
);
});
return this.store.loadList(this.listPath, this.listType).then(() => {
this.loading = false;
});
},
filterItems(term, items) {
const query = term.toLowerCase();
......
/* eslint-disable class-methods-use-this */
import _ from 'underscore';
import Cookies from 'js-cookie';
import { __ } from '~/locale';
import { __, sprintf } from '~/locale';
import BoardService from 'ee/boards/services/board_service';
import sidebarEventHub from '~/sidebar/event_hub';
import createFlash from '~/flash';
import { parseBoolean } from '~/lib/utils/common_utils';
import axios from '~/lib/utils/axios_utils';
class BoardsStoreEE {
initEESpecific(boardsStore) {
......@@ -14,6 +15,7 @@ class BoardsStoreEE {
this.store.addPromotionState = () => {
this.addPromotion();
};
this.store.loadList = (listPath, listType) => this.loadList(listPath, listType);
this.store.removePromotionState = () => {
this.removePromotion();
};
......@@ -179,6 +181,25 @@ class BoardsStoreEE {
});
}
}
loadList(listPath, listType) {
if (this.store.state[listType].length) {
return Promise.resolve();
}
return axios
.get(listPath)
.then(({ data }) => {
this.store.state[listType] = data;
})
.catch(() => {
createFlash(
sprintf(__('Something went wrong while fetching %{listType} list'), {
listType,
}),
);
});
}
}
export default new BoardsStoreEE();
import AxiosMockAdapter from 'axios-mock-adapter';
import BoardsStoreEE from 'ee_else_ce/boards/stores/boards_store_ee';
import axios from '~/lib/utils/axios_utils';
import createFlash from '~/flash';
import { TEST_HOST } from 'helpers/test_constants';
jest.mock('~/flash');
describe('BoardsStoreEE', () => {
let axiosMock;
beforeEach(() => {
axiosMock = new AxiosMockAdapter(axios);
// mock CE store
const storeMock = {
state: {},
create() {},
};
BoardsStoreEE.initEESpecific(storeMock);
});
describe('loadList', () => {
const listPath = `${TEST_HOST}/list/path`;
const listType = 'D-negative';
it('fetches from listPath and stores the result', () => {
const dummyResponse = { uni: 'corn' };
axiosMock.onGet(listPath).replyOnce(200, dummyResponse);
const { state } = BoardsStoreEE.store;
state[listType] = [];
return BoardsStoreEE.loadList(listPath, listType).then(() => {
expect(state[listType]).toEqual(dummyResponse);
});
});
it('displays error if fetching fails', () => {
axiosMock.onGet(listPath).replyOnce(500);
const { state } = BoardsStoreEE.store;
state[listType] = [];
return BoardsStoreEE.loadList(listPath, listType).then(() => {
expect(state[listType]).toEqual([]);
expect(createFlash).toHaveBeenCalled();
});
});
it('does not make a request if response is cached', () => {
const { state } = BoardsStoreEE.store;
state[listType] = ['something'];
return BoardsStoreEE.loadList(listPath, listType).then(() => {
expect(axiosMock.history.get.length).toBe(0);
});
});
});
});
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