2021-01-29 00:20:46 +05:30
|
|
|
import { GlIcon, GlFormInput, GlDropdown, GlSearchBoxByType, GlDropdownItem } from '@gitlab/ui';
|
|
|
|
import { shallowMount } from '@vue/test-utils';
|
|
|
|
import AlertMappingBuilder, { i18n } from '~/alerts_settings/components/alert_mapping_builder.vue';
|
|
|
|
import parsedMapping from '~/alerts_settings/components/mocks/parsedMapping.json';
|
2021-03-11 19:13:27 +05:30
|
|
|
import * as transformationUtils from '~/alerts_settings/utils/mapping_transformations';
|
|
|
|
import { capitalizeFirstCharacter } from '~/lib/utils/text_utility';
|
|
|
|
import alertFields from '../mocks/alertFields.json';
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
describe('AlertMappingBuilder', () => {
|
|
|
|
let wrapper;
|
|
|
|
|
|
|
|
function mountComponent() {
|
|
|
|
wrapper = shallowMount(AlertMappingBuilder, {
|
|
|
|
propsData: {
|
2021-03-11 19:13:27 +05:30
|
|
|
parsedPayload: parsedMapping.samplePayload.payloadAlerFields.nodes,
|
|
|
|
savedMapping: parsedMapping.storedMapping.nodes,
|
|
|
|
alertFields,
|
2021-01-29 00:20:46 +05:30
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
if (wrapper) {
|
|
|
|
wrapper.destroy();
|
|
|
|
wrapper = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mountComponent();
|
|
|
|
});
|
|
|
|
|
|
|
|
const findColumnInRow = (row, column) =>
|
2021-03-08 18:12:59 +05:30
|
|
|
wrapper.findAll('.gl-display-table-row').at(row).findAll('.gl-display-table-cell ').at(column);
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
it('renders column captions', () => {
|
|
|
|
expect(findColumnInRow(0, 0).text()).toContain(i18n.columns.gitlabKeyTitle);
|
|
|
|
expect(findColumnInRow(0, 2).text()).toContain(i18n.columns.payloadKeyTitle);
|
|
|
|
expect(findColumnInRow(0, 3).text()).toContain(i18n.columns.fallbackKeyTitle);
|
|
|
|
|
|
|
|
const fallbackColumnIcon = findColumnInRow(0, 3).find(GlIcon);
|
|
|
|
expect(fallbackColumnIcon.exists()).toBe(true);
|
|
|
|
expect(fallbackColumnIcon.attributes('name')).toBe('question');
|
|
|
|
expect(fallbackColumnIcon.attributes('title')).toBe(i18n.fallbackTooltip);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders disabled form input for each mapped field', () => {
|
2021-03-11 19:13:27 +05:30
|
|
|
alertFields.forEach((field, index) => {
|
2021-01-29 00:20:46 +05:30
|
|
|
const input = findColumnInRow(index + 1, 0).find(GlFormInput);
|
2021-03-11 19:13:27 +05:30
|
|
|
const types = field.types.map((t) => capitalizeFirstCharacter(t.toLowerCase())).join(' or ');
|
|
|
|
expect(input.attributes('value')).toBe(`${field.label} (${types})`);
|
2021-01-29 00:20:46 +05:30
|
|
|
expect(input.attributes('disabled')).toBe('');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders right arrow next to each input', () => {
|
2021-03-11 19:13:27 +05:30
|
|
|
alertFields.forEach((field, index) => {
|
2021-01-29 00:20:46 +05:30
|
|
|
const arrow = findColumnInRow(index + 1, 1).find('.right-arrow');
|
|
|
|
expect(arrow.exists()).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders mapping dropdown for each field', () => {
|
2021-03-11 19:13:27 +05:30
|
|
|
alertFields.forEach(({ types }, index) => {
|
2021-01-29 00:20:46 +05:30
|
|
|
const dropdown = findColumnInRow(index + 1, 2).find(GlDropdown);
|
2021-03-11 19:13:27 +05:30
|
|
|
const searchBox = dropdown.findComponent(GlSearchBoxByType);
|
|
|
|
const dropdownItems = dropdown.findAllComponents(GlDropdownItem);
|
2021-01-29 00:20:46 +05:30
|
|
|
const { nodes } = parsedMapping.samplePayload.payloadAlerFields;
|
2021-03-11 19:13:27 +05:30
|
|
|
const mappingOptions = nodes.filter(({ type }) => types.includes(type));
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
expect(dropdown.exists()).toBe(true);
|
|
|
|
expect(searchBox.exists()).toBe(true);
|
2021-03-11 19:13:27 +05:30
|
|
|
expect(dropdownItems).toHaveLength(mappingOptions.length);
|
2021-01-29 00:20:46 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders fallback dropdown only for the fields that have fallback', () => {
|
2021-03-11 19:13:27 +05:30
|
|
|
alertFields.forEach(({ types, numberOfFallbacks }, index) => {
|
2021-01-29 00:20:46 +05:30
|
|
|
const dropdown = findColumnInRow(index + 1, 3).find(GlDropdown);
|
|
|
|
expect(dropdown.exists()).toBe(Boolean(numberOfFallbacks));
|
|
|
|
|
|
|
|
if (numberOfFallbacks) {
|
2021-03-11 19:13:27 +05:30
|
|
|
const searchBox = dropdown.findComponent(GlSearchBoxByType);
|
|
|
|
const dropdownItems = dropdown.findAllComponents(GlDropdownItem);
|
2021-01-29 00:20:46 +05:30
|
|
|
const { nodes } = parsedMapping.samplePayload.payloadAlerFields;
|
2021-03-11 19:13:27 +05:30
|
|
|
const mappingOptions = nodes.filter(({ type }) => types.includes(type));
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
expect(searchBox.exists()).toBe(Boolean(numberOfFallbacks));
|
2021-03-11 19:13:27 +05:30
|
|
|
expect(dropdownItems).toHaveLength(mappingOptions.length);
|
2021-01-29 00:20:46 +05:30
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
it('emits event with selected mapping', () => {
|
|
|
|
const mappingToSave = { fieldName: 'TITLE', mapping: 'PARSED_TITLE' };
|
|
|
|
jest.spyOn(transformationUtils, 'transformForSave').mockReturnValue(mappingToSave);
|
|
|
|
const dropdown = findColumnInRow(1, 2).find(GlDropdown);
|
|
|
|
const option = dropdown.find(GlDropdownItem);
|
|
|
|
option.vm.$emit('click');
|
|
|
|
expect(wrapper.emitted('onMappingUpdate')[0]).toEqual([mappingToSave]);
|
|
|
|
});
|
2021-01-29 00:20:46 +05:30
|
|
|
});
|