debian-mirror-gitlab/spec/frontend/logs/components/environment_logs_spec.js

373 lines
11 KiB
JavaScript
Raw Normal View History

2021-01-03 14:25:43 +05:30
import { GlSprintf, GlDropdown, GlDropdownItem } from '@gitlab/ui';
2020-04-08 14:13:33 +05:30
import { shallowMount } from '@vue/test-utils';
2021-03-11 19:13:27 +05:30
import { scrollDown } from '~/lib/utils/scroll_utils';
2020-04-08 14:13:33 +05:30
import EnvironmentLogs from '~/logs/components/environment_logs.vue';
import { createStore } from '~/logs/stores';
import {
mockEnvName,
mockEnvironments,
mockPods,
mockLogsResult,
mockTrace,
mockEnvironmentsEndpoint,
mockDocumentationPath,
} from '../mock_data';
jest.mock('~/lib/utils/scroll_utils');
const module = 'environmentLogs';
jest.mock('lodash/throttle', () =>
2021-03-08 18:12:59 +05:30
jest.fn((func) => {
2020-04-08 14:13:33 +05:30
return func;
}),
);
describe('EnvironmentLogs', () => {
let store;
let dispatch;
let wrapper;
let state;
const propsData = {
environmentName: mockEnvName,
environmentsPath: mockEnvironmentsEndpoint,
clusterApplicationsDocumentationPath: mockDocumentationPath,
2021-09-04 01:27:46 +05:30
clustersPath: '/gitlab-org',
2020-04-08 14:13:33 +05:30
};
const updateControlBtnsMock = jest.fn();
2020-11-24 15:15:51 +05:30
const LogControlButtonsStub = {
template: '<div/>',
methods: {
update: updateControlBtnsMock,
},
props: {
scrollDownButtonDisabled: false,
},
};
2020-04-08 14:13:33 +05:30
const findEnvironmentsDropdown = () => wrapper.find('.js-environments-dropdown');
2020-04-22 19:07:51 +05:30
const findSimpleFilters = () => wrapper.find({ ref: 'log-simple-filters' });
const findAdvancedFilters = () => wrapper.find({ ref: 'log-advanced-filters' });
const findElasticsearchNotice = () => wrapper.find({ ref: 'elasticsearchNotice' });
2020-11-24 15:15:51 +05:30
const findLogControlButtons = () => wrapper.find(LogControlButtonsStub);
2020-04-08 14:13:33 +05:30
const findInfiniteScroll = () => wrapper.find({ ref: 'infiniteScroll' });
2020-04-22 19:07:51 +05:30
const findLogTrace = () => wrapper.find({ ref: 'logTrace' });
2020-04-08 14:13:33 +05:30
const findLogFooter = () => wrapper.find({ ref: 'logFooter' });
2021-03-08 18:12:59 +05:30
const getInfiniteScrollAttr = (attr) => parseInt(findInfiniteScroll().attributes(attr), 10);
2020-04-08 14:13:33 +05:30
const mockSetInitData = () => {
state.pods.options = mockPods;
state.environments.current = mockEnvName;
[state.pods.current] = state.pods.options;
state.logs.lines = [];
};
const mockShowPodLogs = () => {
state.pods.options = mockPods;
[state.pods.current] = mockPods;
state.logs.lines = mockLogsResult;
};
const mockFetchEnvs = () => {
state.environments.options = mockEnvironments;
};
const initWrapper = () => {
2020-04-22 19:07:51 +05:30
wrapper = shallowMount(EnvironmentLogs, {
2020-04-08 14:13:33 +05:30
propsData,
store,
stubs: {
2020-11-24 15:15:51 +05:30
LogControlButtons: LogControlButtonsStub,
2020-04-08 14:13:33 +05:30
GlInfiniteScroll: {
name: 'gl-infinite-scroll',
template: `
<div>
<slot name="header"></slot>
<slot name="items"></slot>
<slot></slot>
</div>
`,
},
GlSprintf,
},
});
};
beforeEach(() => {
store = createStore();
state = store.state.environmentLogs;
jest.spyOn(store, 'dispatch').mockResolvedValue();
dispatch = store.dispatch;
});
afterEach(() => {
store.dispatch.mockReset();
if (wrapper) {
wrapper.destroy();
}
});
it('displays UI elements', () => {
initWrapper();
2021-01-03 14:25:43 +05:30
expect(findEnvironmentsDropdown().is(GlDropdown)).toBe(true);
2020-04-22 19:07:51 +05:30
expect(findSimpleFilters().exists()).toBe(true);
2020-04-08 14:13:33 +05:30
expect(findLogControlButtons().exists()).toBe(true);
expect(findInfiniteScroll().exists()).toBe(true);
expect(findLogTrace().exists()).toBe(true);
});
it('mounted inits data', () => {
initWrapper();
expect(dispatch).toHaveBeenCalledWith(`${module}/setInitData`, {
timeRange: expect.objectContaining({
default: true,
}),
environmentName: mockEnvName,
podName: null,
});
expect(dispatch).toHaveBeenCalledWith(`${module}/fetchEnvironments`, mockEnvironmentsEndpoint);
});
describe('loading state', () => {
beforeEach(() => {
state.pods.options = [];
state.logs.lines = [];
state.logs.isLoading = true;
state.environments = {
options: [],
isLoading: true,
};
initWrapper();
});
2020-04-22 19:07:51 +05:30
it('does not display an alert to upgrade to ES', () => {
expect(findElasticsearchNotice().exists()).toBe(false);
});
2020-04-08 14:13:33 +05:30
it('displays a disabled environments dropdown', () => {
expect(findEnvironmentsDropdown().attributes('disabled')).toBe('true');
2021-01-03 14:25:43 +05:30
expect(findEnvironmentsDropdown().findAll(GlDropdownItem).length).toBe(0);
2020-04-08 14:13:33 +05:30
});
it('does not update buttons state', () => {
expect(updateControlBtnsMock).not.toHaveBeenCalled();
});
2020-04-22 19:07:51 +05:30
it('shows an infinite scroll with no content', () => {
2020-04-08 14:13:33 +05:30
expect(getInfiniteScrollAttr('fetched-items')).toBe(0);
});
2020-04-22 19:07:51 +05:30
it('shows an infinite scroll container with no set max-height ', () => {
expect(findInfiniteScroll().attributes('max-list-height')).toBeUndefined();
2020-04-08 14:13:33 +05:30
});
it('shows a logs trace', () => {
expect(findLogTrace().text()).toBe('');
2021-03-08 18:12:59 +05:30
expect(findLogTrace().find('.js-build-loader-animation').isVisible()).toBe(true);
2020-04-08 14:13:33 +05:30
});
});
describe('k8s environment', () => {
beforeEach(() => {
state.pods.options = [];
state.logs.lines = [];
state.logs.isLoading = false;
state.environments = {
options: mockEnvironments,
current: 'staging',
isLoading: false,
};
initWrapper();
});
2020-04-22 19:07:51 +05:30
it('displays an alert to upgrade to ES', () => {
expect(findElasticsearchNotice().exists()).toBe(true);
2020-04-08 14:13:33 +05:30
});
2020-04-22 19:07:51 +05:30
it('displays simple filters for kubernetes logs API', () => {
expect(findSimpleFilters().exists()).toBe(true);
expect(findAdvancedFilters().exists()).toBe(false);
2020-04-08 14:13:33 +05:30
});
});
describe('state with data', () => {
beforeEach(() => {
2021-03-08 18:12:59 +05:30
dispatch.mockImplementation((actionName) => {
2020-04-08 14:13:33 +05:30
if (actionName === `${module}/setInitData`) {
mockSetInitData();
} else if (actionName === `${module}/showPodLogs`) {
mockShowPodLogs();
} else if (actionName === `${module}/fetchEnvironments`) {
mockFetchEnvs();
mockShowPodLogs();
}
});
initWrapper();
});
afterEach(() => {
scrollDown.mockReset();
updateControlBtnsMock.mockReset();
});
it('does not display an alert to upgrade to ES', () => {
2020-04-22 19:07:51 +05:30
expect(findElasticsearchNotice().exists()).toBe(false);
2020-04-08 14:13:33 +05:30
});
it('populates environments dropdown', () => {
2021-01-03 14:25:43 +05:30
const items = findEnvironmentsDropdown().findAll(GlDropdownItem);
2020-04-08 14:13:33 +05:30
expect(findEnvironmentsDropdown().props('text')).toBe(mockEnvName);
expect(items.length).toBe(mockEnvironments.length);
mockEnvironments.forEach((env, i) => {
const item = items.at(i);
expect(item.text()).toBe(env.name);
});
});
2020-04-22 19:07:51 +05:30
it('dropdown has one environment selected', () => {
2021-01-03 14:25:43 +05:30
const items = findEnvironmentsDropdown().findAll(GlDropdownItem);
2020-04-22 19:07:51 +05:30
mockEnvironments.forEach((env, i) => {
2020-04-08 14:13:33 +05:30
const item = items.at(i);
2020-04-22 19:07:51 +05:30
if (item.text() !== mockEnvName) {
2021-01-03 14:25:43 +05:30
expect(item.find(GlDropdownItem).attributes('ischecked')).toBeFalsy();
2020-04-22 19:07:51 +05:30
} else {
2021-01-03 14:25:43 +05:30
expect(item.find(GlDropdownItem).attributes('ischecked')).toBeTruthy();
2020-04-22 19:07:51 +05:30
}
2020-04-08 14:13:33 +05:30
});
});
2020-04-22 19:07:51 +05:30
it('displays advanced filters for elasticsearch logs API', () => {
expect(findSimpleFilters().exists()).toBe(false);
expect(findAdvancedFilters().exists()).toBe(true);
});
it('shows infinite scroll with content', () => {
2020-04-08 14:13:33 +05:30
expect(getInfiniteScrollAttr('fetched-items')).toBe(mockTrace.length);
});
it('populates logs trace', () => {
const trace = findLogTrace();
expect(trace.text().split('\n').length).toBe(mockTrace.length);
expect(trace.text().split('\n')).toEqual(mockTrace);
});
it('populates footer', () => {
const footer = findLogFooter().text();
expect(footer).toContain(`${mockLogsResult.length} results`);
});
describe('when user clicks', () => {
it('environment name, trace is refreshed', () => {
2021-01-03 14:25:43 +05:30
const items = findEnvironmentsDropdown().findAll(GlDropdownItem);
2020-04-08 14:13:33 +05:30
const index = 1; // any env
expect(dispatch).not.toHaveBeenCalledWith(`${module}/showEnvironment`, expect.anything());
items.at(index).vm.$emit('click');
expect(dispatch).toHaveBeenCalledWith(
`${module}/showEnvironment`,
mockEnvironments[index].name,
);
});
it('refresh button, trace is refreshed', () => {
2020-06-23 00:09:42 +05:30
expect(dispatch).not.toHaveBeenCalledWith(`${module}/refreshPodLogs`, undefined);
2020-04-08 14:13:33 +05:30
findLogControlButtons().vm.$emit('refresh');
2020-06-23 00:09:42 +05:30
expect(dispatch).toHaveBeenCalledWith(`${module}/refreshPodLogs`, undefined);
2020-04-08 14:13:33 +05:30
});
});
});
describe('listeners', () => {
beforeEach(() => {
initWrapper();
});
it('attaches listeners in components', () => {
expect(findInfiniteScroll().vm.$listeners).toEqual({
topReached: expect.any(Function),
scroll: expect.any(Function),
});
});
it('`topReached` when not loading', () => {
expect(store.dispatch).not.toHaveBeenCalledWith(`${module}/fetchMoreLogsPrepend`, undefined);
findInfiniteScroll().vm.$emit('topReached');
expect(store.dispatch).toHaveBeenCalledWith(`${module}/fetchMoreLogsPrepend`, undefined);
});
it('`topReached` does not fetches more logs when already loading', () => {
state.logs.isLoading = true;
findInfiniteScroll().vm.$emit('topReached');
expect(store.dispatch).not.toHaveBeenCalledWith(`${module}/fetchMoreLogsPrepend`, undefined);
});
it('`topReached` fetches more logs', () => {
state.logs.isLoading = true;
findInfiniteScroll().vm.$emit('topReached');
expect(store.dispatch).not.toHaveBeenCalledWith(`${module}/fetchMoreLogsPrepend`, undefined);
});
it('`scroll` on a scrollable target results in enabled scroll buttons', () => {
const target = { scrollTop: 10, clientHeight: 10, scrollHeight: 21 };
state.logs.isLoading = true;
findInfiniteScroll().vm.$emit('scroll', { target });
return wrapper.vm.$nextTick(() => {
expect(findLogControlButtons().props('scrollDownButtonDisabled')).toEqual(false);
});
});
it('`scroll` on a non-scrollable target in disabled scroll buttons', () => {
const target = { scrollTop: 10, clientHeight: 10, scrollHeight: 20 };
state.logs.isLoading = true;
findInfiniteScroll().vm.$emit('scroll', { target });
return wrapper.vm.$nextTick(() => {
expect(findLogControlButtons().props('scrollDownButtonDisabled')).toEqual(true);
});
});
it('`scroll` on no target results in disabled scroll buttons', () => {
state.logs.isLoading = true;
findInfiniteScroll().vm.$emit('scroll', { target: undefined });
return wrapper.vm.$nextTick(() => {
expect(findLogControlButtons().props('scrollDownButtonDisabled')).toEqual(true);
});
});
});
});