2021-03-08 18:12:59 +05:30
|
|
|
import { GlLoadingIcon, GlButton, GlSearchBoxByType } from '@gitlab/ui';
|
2021-03-11 19:13:27 +05:30
|
|
|
import { shallowMount } from '@vue/test-utils';
|
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2022-04-04 11:22:00 +05:30
|
|
|
import { nextTick } from 'vue';
|
2021-01-03 14:25:43 +05:30
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2021-03-11 19:13:27 +05:30
|
|
|
import { TEST_HOST } from 'spec/test_constants';
|
2021-01-03 14:25:43 +05:30
|
|
|
import EnvironmentsDropdown from '~/feature_flags/components/environments_dropdown.vue';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import httpStatusCodes from '~/lib/utils/http_status';
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
describe('Feature flags > Environments dropdown', () => {
|
2021-01-03 14:25:43 +05:30
|
|
|
let wrapper;
|
|
|
|
let mock;
|
|
|
|
const results = ['production', 'staging'];
|
2021-03-08 18:12:59 +05:30
|
|
|
const factory = (props) => {
|
2021-01-03 14:25:43 +05:30
|
|
|
wrapper = shallowMount(EnvironmentsDropdown, {
|
|
|
|
propsData: {
|
|
|
|
...props,
|
|
|
|
},
|
|
|
|
provide: {
|
|
|
|
environmentsEndpoint: `${TEST_HOST}/environments.json'`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
const findEnvironmentSearchInput = () => wrapper.findComponent(GlSearchBoxByType);
|
2021-01-03 14:25:43 +05:30
|
|
|
const findDropdownMenu = () => wrapper.find('.dropdown-menu');
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
|
|
|
mock.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('without value', () => {
|
|
|
|
it('renders the placeholder', () => {
|
|
|
|
factory();
|
|
|
|
expect(findEnvironmentSearchInput().vm.$attrs.placeholder).toBe('Search an environment spec');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with value', () => {
|
|
|
|
it('sets filter to equal the value', () => {
|
|
|
|
factory({ value: 'production' });
|
|
|
|
expect(findEnvironmentSearchInput().props('value')).toBe('production');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('on focus', () => {
|
|
|
|
it('sets results with the received data', async () => {
|
|
|
|
mock.onGet(`${TEST_HOST}/environments.json'`).replyOnce(httpStatusCodes.OK, results);
|
|
|
|
factory();
|
|
|
|
findEnvironmentSearchInput().vm.$emit('focus');
|
|
|
|
await waitForPromises();
|
2022-04-04 11:22:00 +05:30
|
|
|
await nextTick();
|
2021-01-03 14:25:43 +05:30
|
|
|
expect(wrapper.find('.dropdown-content > ul').exists()).toBe(true);
|
|
|
|
expect(wrapper.findAll('.dropdown-content > ul > li').exists()).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('on keyup', () => {
|
|
|
|
it('sets results with the received data', async () => {
|
|
|
|
mock.onGet(`${TEST_HOST}/environments.json'`).replyOnce(httpStatusCodes.OK, results);
|
|
|
|
factory();
|
|
|
|
findEnvironmentSearchInput().vm.$emit('keyup');
|
|
|
|
await waitForPromises();
|
2022-04-04 11:22:00 +05:30
|
|
|
await nextTick();
|
2021-01-03 14:25:43 +05:30
|
|
|
expect(wrapper.find('.dropdown-content > ul').exists()).toBe(true);
|
|
|
|
expect(wrapper.findAll('.dropdown-content > ul > li').exists()).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('on input change', () => {
|
|
|
|
describe('on success', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
mock.onGet(`${TEST_HOST}/environments.json'`).replyOnce(httpStatusCodes.OK, results);
|
|
|
|
factory();
|
|
|
|
findEnvironmentSearchInput().vm.$emit('focus');
|
|
|
|
findEnvironmentSearchInput().vm.$emit('input', 'production');
|
|
|
|
await waitForPromises();
|
2022-04-04 11:22:00 +05:30
|
|
|
await nextTick();
|
2021-01-03 14:25:43 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('sets filter value', () => {
|
|
|
|
expect(findEnvironmentSearchInput().props('value')).toBe('production');
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('with received data', () => {
|
|
|
|
it('sets is loading to false', () => {
|
|
|
|
expect(wrapper.vm.isLoading).toBe(false);
|
2022-08-27 11:52:29 +05:30
|
|
|
expect(wrapper.findComponent(GlLoadingIcon).exists()).toBe(false);
|
2021-01-03 14:25:43 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('shows the suggestions', () => {
|
|
|
|
expect(findDropdownMenu().exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('emits event when a suggestion is clicked', async () => {
|
|
|
|
const button = wrapper
|
2022-08-27 11:52:29 +05:30
|
|
|
.findAllComponents(GlButton)
|
2021-03-08 18:12:59 +05:30
|
|
|
.filter((b) => b.text() === 'production')
|
2021-01-03 14:25:43 +05:30
|
|
|
.at(0);
|
|
|
|
button.vm.$emit('click');
|
2022-04-04 11:22:00 +05:30
|
|
|
await nextTick();
|
2021-01-03 14:25:43 +05:30
|
|
|
expect(wrapper.emitted('selectEnvironment')).toEqual([['production']]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('on click clear button', () => {
|
|
|
|
beforeEach(async () => {
|
2022-08-27 11:52:29 +05:30
|
|
|
wrapper.findComponent(GlButton).vm.$emit('click');
|
2022-04-04 11:22:00 +05:30
|
|
|
await nextTick();
|
2021-01-03 14:25:43 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('resets filter value', () => {
|
|
|
|
expect(findEnvironmentSearchInput().props('value')).toBe('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('closes list of suggestions', () => {
|
|
|
|
expect(wrapper.vm.showSuggestions).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('on click create button', () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
mock.onGet(`${TEST_HOST}/environments.json'`).replyOnce(httpStatusCodes.OK, []);
|
|
|
|
factory();
|
|
|
|
findEnvironmentSearchInput().vm.$emit('focus');
|
|
|
|
findEnvironmentSearchInput().vm.$emit('input', 'production');
|
|
|
|
await waitForPromises();
|
2022-04-04 11:22:00 +05:30
|
|
|
await nextTick();
|
2021-01-03 14:25:43 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('emits create event', async () => {
|
2022-08-27 11:52:29 +05:30
|
|
|
wrapper.findAllComponents(GlButton).at(0).vm.$emit('click');
|
2022-04-04 11:22:00 +05:30
|
|
|
await nextTick();
|
2021-01-03 14:25:43 +05:30
|
|
|
expect(wrapper.emitted('createClicked')).toEqual([['production']]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|