debian-mirror-gitlab/spec/javascripts/environments/folder/environments_folder_view_spec.js

248 lines
7.2 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
import _ from 'underscore';
2017-08-17 22:00:37 +05:30
import Vue from 'vue';
import environmentsFolderViewComponent from '~/environments/folder/environments_folder_view.vue';
import { environmentsList } from '../mock_data';
2017-09-10 17:25:29 +05:30
import { headersInterceptor } from '../../helpers/vue_resource_helper';
2018-03-17 18:26:18 +05:30
import mountComponent from '../../helpers/vue_mount_component_helper';
2017-08-17 22:00:37 +05:30
describe('Environments Folder View', () => {
2018-03-17 18:26:18 +05:30
let Component;
let component;
const mockData = {
endpoint: 'environments.json',
folderName: 'review',
canCreateDeployment: true,
canReadEnvironment: true,
cssContainerClass: 'container',
};
2017-08-17 22:00:37 +05:30
beforeEach(() => {
2018-03-17 18:26:18 +05:30
Component = Vue.extend(environmentsFolderViewComponent);
2017-08-17 22:00:37 +05:30
});
2018-03-17 18:26:18 +05:30
afterEach(() => {
component.$destroy();
});
2017-08-17 22:00:37 +05:30
describe('successfull request', () => {
const environmentsResponseInterceptor = (request, next) => {
next(request.respondWith(JSON.stringify({
environments: environmentsList,
stopped_count: 1,
available_count: 0,
}), {
status: 200,
headers: {
'X-nExt-pAge': '2',
'x-page': '1',
2018-03-17 18:26:18 +05:30
'X-Per-Page': '2',
2017-08-17 22:00:37 +05:30
'X-Prev-Page': '',
2018-03-17 18:26:18 +05:30
'X-TOTAL': '20',
'X-Total-Pages': '10',
2017-08-17 22:00:37 +05:30
},
}));
};
beforeEach(() => {
Vue.http.interceptors.push(environmentsResponseInterceptor);
2017-09-10 17:25:29 +05:30
Vue.http.interceptors.push(headersInterceptor);
2018-03-17 18:26:18 +05:30
component = mountComponent(Component, mockData);
2017-08-17 22:00:37 +05:30
});
afterEach(() => {
Vue.http.interceptors = _.without(
Vue.http.interceptors, environmentsResponseInterceptor,
);
2017-09-10 17:25:29 +05:30
Vue.http.interceptors = _.without(Vue.http.interceptors, headersInterceptor);
2017-08-17 22:00:37 +05:30
});
it('should render a table with environments', (done) => {
setTimeout(() => {
2018-03-17 18:26:18 +05:30
expect(component.$el.querySelectorAll('table')).not.toBeNull();
2017-08-17 22:00:37 +05:30
expect(
component.$el.querySelector('.environment-name').textContent.trim(),
).toEqual(environmentsList[0].name);
done();
}, 0);
});
it('should render available tab with count', (done) => {
setTimeout(() => {
expect(
2018-03-17 18:26:18 +05:30
component.$el.querySelector('.js-environments-tab-available').textContent,
2017-08-17 22:00:37 +05:30
).toContain('Available');
expect(
2018-03-17 18:26:18 +05:30
component.$el.querySelector('.js-environments-tab-available .badge').textContent,
2017-08-17 22:00:37 +05:30
).toContain('0');
done();
}, 0);
});
it('should render stopped tab with count', (done) => {
setTimeout(() => {
expect(
2018-03-17 18:26:18 +05:30
component.$el.querySelector('.js-environments-tab-stopped').textContent,
2017-08-17 22:00:37 +05:30
).toContain('Stopped');
expect(
2018-03-17 18:26:18 +05:30
component.$el.querySelector('.js-environments-tab-stopped .badge').textContent,
2017-08-17 22:00:37 +05:30
).toContain('1');
done();
}, 0);
});
it('should render parent folder name', (done) => {
setTimeout(() => {
expect(
2018-03-17 18:26:18 +05:30
component.$el.querySelector('.js-folder-name').textContent.trim(),
).toContain('Environments / review');
2017-08-17 22:00:37 +05:30
done();
}, 0);
});
describe('pagination', () => {
it('should render pagination', (done) => {
setTimeout(() => {
expect(
2018-03-17 18:26:18 +05:30
component.$el.querySelectorAll('.gl-pagination'),
).not.toBeNull();
2017-08-17 22:00:37 +05:30
done();
}, 0);
});
2018-03-17 18:26:18 +05:30
it('should make an API request when changing page', (done) => {
spyOn(component, 'updateContent');
2017-08-17 22:00:37 +05:30
setTimeout(() => {
2018-03-17 18:26:18 +05:30
component.$el.querySelector('.gl-pagination .js-last-button a').click();
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
expect(component.updateContent).toHaveBeenCalledWith({ scope: component.scope, page: '10' });
2017-08-17 22:00:37 +05:30
done();
}, 0);
});
2018-03-17 18:26:18 +05:30
it('should make an API request when using tabs', (done) => {
2017-08-17 22:00:37 +05:30
setTimeout(() => {
2018-03-17 18:26:18 +05:30
spyOn(component, 'updateContent');
component.$el.querySelector('.js-environments-tab-stopped').click();
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
expect(component.updateContent).toHaveBeenCalledWith({ scope: 'stopped', page: '1' });
2017-08-17 22:00:37 +05:30
done();
2018-03-17 18:26:18 +05:30
});
2017-08-17 22:00:37 +05:30
});
});
});
describe('unsuccessfull request', () => {
const environmentsErrorResponseInterceptor = (request, next) => {
next(request.respondWith(JSON.stringify([]), {
status: 500,
}));
};
beforeEach(() => {
Vue.http.interceptors.push(environmentsErrorResponseInterceptor);
});
afterEach(() => {
Vue.http.interceptors = _.without(
Vue.http.interceptors, environmentsErrorResponseInterceptor,
);
});
it('should not render a table', (done) => {
2018-03-17 18:26:18 +05:30
component = mountComponent(Component, mockData);
2017-08-17 22:00:37 +05:30
setTimeout(() => {
expect(
component.$el.querySelector('table'),
).toBe(null);
done();
}, 0);
});
it('should render available tab with count 0', (done) => {
setTimeout(() => {
expect(
2018-03-17 18:26:18 +05:30
component.$el.querySelector('.js-environments-tab-available').textContent,
2017-08-17 22:00:37 +05:30
).toContain('Available');
expect(
2018-03-17 18:26:18 +05:30
component.$el.querySelector('.js-environments-tab-available .badge').textContent,
2017-08-17 22:00:37 +05:30
).toContain('0');
done();
}, 0);
});
it('should render stopped tab with count 0', (done) => {
setTimeout(() => {
expect(
2018-03-17 18:26:18 +05:30
component.$el.querySelector('.js-environments-tab-stopped').textContent,
2017-08-17 22:00:37 +05:30
).toContain('Stopped');
expect(
2018-03-17 18:26:18 +05:30
component.$el.querySelector('.js-environments-tab-stopped .badge').textContent,
2017-08-17 22:00:37 +05:30
).toContain('0');
done();
}, 0);
});
});
2018-03-17 18:26:18 +05:30
describe('methods', () => {
const environmentsEmptyResponseInterceptor = (request, next) => {
next(request.respondWith(JSON.stringify([]), {
status: 200,
}));
};
beforeEach(() => {
Vue.http.interceptors.push(environmentsEmptyResponseInterceptor);
Vue.http.interceptors.push(headersInterceptor);
component = mountComponent(Component, mockData);
spyOn(history, 'pushState').and.stub();
});
afterEach(() => {
Vue.http.interceptors = _.without(
Vue.http.interceptors, environmentsEmptyResponseInterceptor,
);
Vue.http.interceptors = _.without(Vue.http.interceptors, headersInterceptor);
});
describe('updateContent', () => {
it('should set given parameters', (done) => {
component.updateContent({ scope: 'stopped', page: '4' })
.then(() => {
expect(component.page).toEqual('4');
expect(component.scope).toEqual('stopped');
expect(component.requestData.scope).toEqual('stopped');
expect(component.requestData.page).toEqual('4');
done();
})
.catch(done.fail);
});
});
describe('onChangeTab', () => {
it('should set page to 1', () => {
spyOn(component, 'updateContent');
component.onChangeTab('stopped');
expect(component.updateContent).toHaveBeenCalledWith({ scope: 'stopped', page: '1' });
});
});
describe('onChangePage', () => {
it('should update page and keep scope', () => {
spyOn(component, 'updateContent');
component.onChangePage(4);
expect(component.updateContent).toHaveBeenCalledWith({ scope: component.scope, page: '4' });
});
});
});
2017-08-17 22:00:37 +05:30
});