BigW Consortium Gitlab

Commit 7fa17ef6 by Phil Hughes

Merge branch '5845-extract-ee-environments-files' into 'master'

Replaces vue resource with axios for environments code See merge request gitlab-org/gitlab-ce!18950
parents 7381a33f 7f0f8594
...@@ -68,8 +68,7 @@ ...@@ -68,8 +68,7 @@
this.store.updateEnvironmentProp(folder, 'isLoadingFolderContent', showLoader); this.store.updateEnvironmentProp(folder, 'isLoadingFolderContent', showLoader);
this.service.getFolderContent(folder.folder_path) this.service.getFolderContent(folder.folder_path)
.then(resp => resp.json()) .then(response => this.store.setfolderContent(folder, response.data.environments))
.then(response => this.store.setfolderContent(folder, response.environments))
.then(() => this.store.updateEnvironmentProp(folder, 'isLoadingFolderContent', false)) .then(() => this.store.updateEnvironmentProp(folder, 'isLoadingFolderContent', false))
.catch(() => { .catch(() => {
Flash(s__('Environments|An error occurred while fetching the environments.')); Flash(s__('Environments|An error occurred while fetching the environments.'));
......
...@@ -6,7 +6,6 @@ import Visibility from 'visibilityjs'; ...@@ -6,7 +6,6 @@ import Visibility from 'visibilityjs';
import Poll from '../../lib/utils/poll'; import Poll from '../../lib/utils/poll';
import { import {
getParameterByName, getParameterByName,
parseQueryStringIntoObject,
} from '../../lib/utils/common_utils'; } from '../../lib/utils/common_utils';
import { s__ } from '../../locale'; import { s__ } from '../../locale';
import Flash from '../../flash'; import Flash from '../../flash';
...@@ -46,17 +45,14 @@ export default { ...@@ -46,17 +45,14 @@ export default {
methods: { methods: {
saveData(resp) { saveData(resp) {
const headers = resp.headers; this.isLoading = false;
return resp.json().then((response) => {
this.isLoading = false; if (_.isEqual(resp.config.params, this.requestData)) {
this.store.storeAvailableCount(resp.data.available_count);
if (_.isEqual(parseQueryStringIntoObject(resp.url.split('?')[1]), this.requestData)) { this.store.storeStoppedCount(resp.data.stopped_count);
this.store.storeAvailableCount(response.available_count); this.store.storeEnvironments(resp.data.environments);
this.store.storeStoppedCount(response.stopped_count); this.store.setPagination(resp.headers);
this.store.storeEnvironments(response.environments); }
this.store.setPagination(headers);
}
});
}, },
/** /**
...@@ -70,7 +66,7 @@ export default { ...@@ -70,7 +66,7 @@ export default {
updateContent(parameters) { updateContent(parameters) {
this.updateInternalState(parameters); this.updateInternalState(parameters);
// fetch new data // fetch new data
return this.service.get(this.requestData) return this.service.fetchEnvironments(this.requestData)
.then(response => this.successCallback(response)) .then(response => this.successCallback(response))
.then(() => { .then(() => {
// restart polling // restart polling
...@@ -105,7 +101,7 @@ export default { ...@@ -105,7 +101,7 @@ export default {
fetchEnvironments() { fetchEnvironments() {
this.isLoading = true; this.isLoading = true;
return this.service.get(this.requestData) return this.service.fetchEnvironments(this.requestData)
.then(this.successCallback) .then(this.successCallback)
.catch(this.errorCallback); .catch(this.errorCallback);
}, },
...@@ -141,7 +137,7 @@ export default { ...@@ -141,7 +137,7 @@ export default {
this.poll = new Poll({ this.poll = new Poll({
resource: this.service, resource: this.service,
method: 'get', method: 'fetchEnvironments',
data: this.requestData, data: this.requestData,
successCallback: this.successCallback, successCallback: this.successCallback,
errorCallback: this.errorCallback, errorCallback: this.errorCallback,
......
/* eslint-disable class-methods-use-this */ import axios from '~/lib/utils/axios_utils';
import Vue from 'vue';
import VueResource from 'vue-resource';
Vue.use(VueResource);
export default class EnvironmentsService { export default class EnvironmentsService {
constructor(endpoint) { constructor(endpoint) {
this.environments = Vue.resource(endpoint); this.environmentsEndpoint = endpoint;
this.folderResults = 3; this.folderResults = 3;
} }
get(options = {}) { fetchEnvironments(options = {}) {
const { scope, page } = options; const { scope, page } = options;
return this.environments.get({ scope, page }); return axios.get(this.environmentsEndpoint, { params: { scope, page } });
} }
// eslint-disable-next-line class-methods-use-this
postAction(endpoint) { postAction(endpoint) {
return Vue.http.post(endpoint, {}, { emulateJSON: true }); return axios.post(endpoint, {}, { emulateJSON: true });
} }
getFolderContent(folderUrl) { getFolderContent(folderUrl) {
return Vue.http.get(`${folderUrl}.json?per_page=${this.folderResults}`); return axios.get(`${folderUrl}.json?per_page=${this.folderResults}`);
} }
} }
import _ from 'underscore';
import Vue from 'vue'; import Vue from 'vue';
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import environmentsFolderViewComponent from '~/environments/folder/environments_folder_view.vue'; import environmentsFolderViewComponent from '~/environments/folder/environments_folder_view.vue';
import { headersInterceptor } from 'spec/helpers/vue_resource_helper';
import mountComponent from 'spec/helpers/vue_mount_component_helper'; import mountComponent from 'spec/helpers/vue_mount_component_helper';
import { environmentsList } from '../mock_data'; import { environmentsList } from '../mock_data';
describe('Environments Folder View', () => { describe('Environments Folder View', () => {
let Component; let Component;
let component; let component;
let mock;
const mockData = { const mockData = {
endpoint: 'environments.json', endpoint: 'environments.json',
folderName: 'review', folderName: 'review',
...@@ -17,46 +19,35 @@ describe('Environments Folder View', () => { ...@@ -17,46 +19,35 @@ describe('Environments Folder View', () => {
}; };
beforeEach(() => { beforeEach(() => {
mock = new MockAdapter(axios);
Component = Vue.extend(environmentsFolderViewComponent); Component = Vue.extend(environmentsFolderViewComponent);
}); });
afterEach(() => { afterEach(() => {
mock.restore();
component.$destroy(); component.$destroy();
}); });
describe('successfull request', () => { describe('successfull request', () => {
const environmentsResponseInterceptor = (request, next) => { beforeEach(() => {
next(request.respondWith(JSON.stringify({ mock.onGet(mockData.endpoint).reply(200, {
environments: environmentsList, environments: environmentsList,
stopped_count: 1, stopped_count: 1,
available_count: 0, available_count: 0,
}), { }, {
status: 200, 'X-nExt-pAge': '2',
headers: { 'x-page': '1',
'X-nExt-pAge': '2', 'X-Per-Page': '2',
'x-page': '1', 'X-Prev-Page': '',
'X-Per-Page': '2', 'X-TOTAL': '20',
'X-Prev-Page': '', 'X-Total-Pages': '10',
'X-TOTAL': '20', });
'X-Total-Pages': '10',
},
}));
};
beforeEach(() => {
Vue.http.interceptors.push(environmentsResponseInterceptor);
Vue.http.interceptors.push(headersInterceptor);
component = mountComponent(Component, mockData); component = mountComponent(Component, mockData);
}); });
afterEach(() => {
Vue.http.interceptors = _.without(
Vue.http.interceptors, environmentsResponseInterceptor,
);
Vue.http.interceptors = _.without(Vue.http.interceptors, headersInterceptor);
});
it('should render a table with environments', (done) => { it('should render a table with environments', (done) => {
setTimeout(() => { setTimeout(() => {
expect(component.$el.querySelectorAll('table')).not.toBeNull(); expect(component.$el.querySelectorAll('table')).not.toBeNull();
...@@ -135,25 +126,15 @@ describe('Environments Folder View', () => { ...@@ -135,25 +126,15 @@ describe('Environments Folder View', () => {
}); });
describe('unsuccessfull request', () => { describe('unsuccessfull request', () => {
const environmentsErrorResponseInterceptor = (request, next) => {
next(request.respondWith(JSON.stringify([]), {
status: 500,
}));
};
beforeEach(() => { beforeEach(() => {
Vue.http.interceptors.push(environmentsErrorResponseInterceptor); mock.onGet(mockData.endpoint).reply(500, {
}); environments: [],
});
afterEach(() => { component = mountComponent(Component, mockData);
Vue.http.interceptors = _.without(
Vue.http.interceptors, environmentsErrorResponseInterceptor,
);
}); });
it('should not render a table', (done) => { it('should not render a table', (done) => {
component = mountComponent(Component, mockData);
setTimeout(() => { setTimeout(() => {
expect( expect(
component.$el.querySelector('table'), component.$el.querySelector('table'),
...@@ -190,27 +171,15 @@ describe('Environments Folder View', () => { ...@@ -190,27 +171,15 @@ describe('Environments Folder View', () => {
}); });
describe('methods', () => { describe('methods', () => {
const environmentsEmptyResponseInterceptor = (request, next) => {
next(request.respondWith(JSON.stringify([]), {
status: 200,
}));
};
beforeEach(() => { beforeEach(() => {
Vue.http.interceptors.push(environmentsEmptyResponseInterceptor); mock.onGet(mockData.endpoint).reply(200, {
Vue.http.interceptors.push(headersInterceptor); environments: [],
});
component = mountComponent(Component, mockData); component = mountComponent(Component, mockData);
spyOn(history, 'pushState').and.stub(); spyOn(history, 'pushState').and.stub();
}); });
afterEach(() => {
Vue.http.interceptors = _.without(
Vue.http.interceptors, environmentsEmptyResponseInterceptor,
);
Vue.http.interceptors = _.without(Vue.http.interceptors, headersInterceptor);
});
describe('updateContent', () => { describe('updateContent', () => {
it('should set given parameters', (done) => { it('should set given parameters', (done) => {
component.updateContent({ scope: 'stopped', page: '4' }) component.updateContent({ scope: 'stopped', page: '4' })
......
...@@ -82,6 +82,7 @@ export const environment = { ...@@ -82,6 +82,7 @@ export const environment = {
stop_path: '/root/review-app/environments/7/stop', stop_path: '/root/review-app/environments/7/stop',
created_at: '2017-01-31T10:53:46.894Z', created_at: '2017-01-31T10:53:46.894Z',
updated_at: '2017-01-31T10:53:46.894Z', updated_at: '2017-01-31T10:53:46.894Z',
folder_path: '/root/review-app/environments/7',
}, },
}; };
......
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