Commit 7febb5cd authored by Natalia Tepluhina's avatar Natalia Tepluhina

Merge branch '194223-move-gke_zone_dropdown_spec' into 'master'

Migrate gke_zone_dropdown_spec to Jest

See merge request gitlab-org/gitlab!25813
parents 3fbd86da 807df0f6
import Vue from 'vue'; import { shallowMount } from '@vue/test-utils';
import { mountComponentWithStore } from 'spec/helpers/vue_mount_component_helper';
import GkeZoneDropdown from '~/create_cluster/gke_cluster/components/gke_zone_dropdown.vue'; import GkeZoneDropdown from '~/create_cluster/gke_cluster/components/gke_zone_dropdown.vue';
import DropdownHiddenInput from '~/vue_shared/components/dropdown/dropdown_hidden_input.vue';
import DropdownButton from '~/vue_shared/components/dropdown/dropdown_button.vue';
import { createStore } from '~/create_cluster/gke_cluster/store'; import { createStore } from '~/create_cluster/gke_cluster/store';
import { import {
SET_PROJECT, SET_PROJECT,
...@@ -9,7 +10,7 @@ import { ...@@ -9,7 +10,7 @@ import {
} from '~/create_cluster/gke_cluster/store/mutation_types'; } from '~/create_cluster/gke_cluster/store/mutation_types';
import { selectedZoneMock, selectedProjectMock, gapiZonesResponseMock } from '../mock_data'; import { selectedZoneMock, selectedProjectMock, gapiZonesResponseMock } from '../mock_data';
const componentConfig = { const propsData = {
fieldId: 'cluster_provider_gcp_attributes_gcp_zone', fieldId: 'cluster_provider_gcp_attributes_gcp_zone',
fieldName: 'cluster[provider_gcp_attributes][gcp_zone]', fieldName: 'cluster[provider_gcp_attributes][gcp_zone]',
}; };
...@@ -20,75 +21,81 @@ const LABELS = { ...@@ -20,75 +21,81 @@ const LABELS = {
DEFAULT: 'Select zone', DEFAULT: 'Select zone',
}; };
const createComponent = (store, props = componentConfig) => {
const Component = Vue.extend(GkeZoneDropdown);
return mountComponentWithStore(Component, {
el: null,
props,
store,
});
};
describe('GkeZoneDropdown', () => { describe('GkeZoneDropdown', () => {
let vm;
let store; let store;
let wrapper;
beforeEach(() => { beforeEach(() => {
store = createStore(); store = createStore();
vm = createComponent(store); wrapper = shallowMount(GkeZoneDropdown, { propsData, store });
}); });
afterEach(() => { afterEach(() => {
vm.$destroy(); wrapper.destroy();
}); });
describe('toggleText', () => { describe('toggleText', () => {
let dropdownButton;
beforeEach(() => {
dropdownButton = wrapper.find(DropdownButton);
});
it('returns disabled state toggle text', () => { it('returns disabled state toggle text', () => {
expect(vm.toggleText).toBe(LABELS.DISABLED); expect(dropdownButton.props('toggleText')).toBe(LABELS.DISABLED);
});
describe('isLoading', () => {
beforeEach(() => {
wrapper.setData({ isLoading: true });
return wrapper.vm.$nextTick();
}); });
it('returns loading toggle text', () => { it('returns loading toggle text', () => {
vm.isLoading = true; expect(dropdownButton.props('toggleText')).toBe(LABELS.LOADING);
});
});
expect(vm.toggleText).toBe(LABELS.LOADING); describe('project is set', () => {
beforeEach(() => {
wrapper.vm.$store.commit(SET_PROJECT, selectedProjectMock);
wrapper.vm.$store.commit(SET_PROJECT_BILLING_STATUS, true);
return wrapper.vm.$nextTick();
}); });
it('returns default toggle text', () => { it('returns default toggle text', () => {
expect(vm.toggleText).toBe(LABELS.DISABLED); expect(dropdownButton.props('toggleText')).toBe(LABELS.DEFAULT);
});
vm.$store.commit(SET_PROJECT, selectedProjectMock); });
vm.$store.commit(SET_PROJECT_BILLING_STATUS, true);
expect(vm.toggleText).toBe(LABELS.DEFAULT); describe('project is selected', () => {
beforeEach(() => {
wrapper.vm.setItem(selectedZoneMock);
return wrapper.vm.$nextTick();
}); });
it('returns project name if project selected', () => { it('returns project name if project selected', () => {
vm.setItem(selectedZoneMock); expect(dropdownButton.props('toggleText')).toBe(selectedZoneMock);
});
expect(vm.toggleText).toBe(selectedZoneMock);
}); });
}); });
describe('selectItem', () => { describe('selectItem', () => {
it('reflects new value when dropdown item is clicked', done => { beforeEach(() => {
expect(vm.$el.querySelector('input').value).toBe(''); wrapper.vm.$store.commit(SET_ZONES, gapiZonesResponseMock.items);
vm.$store.commit(SET_ZONES, gapiZonesResponseMock.items); return wrapper.vm.$nextTick();
});
return vm
.$nextTick() it('reflects new value when dropdown item is clicked', () => {
.then(() => { const dropdown = wrapper.find(DropdownHiddenInput);
vm.$el.querySelector('.dropdown-content button').click();
expect(dropdown.attributes('value')).toBe('');
return vm
.$nextTick() wrapper.find('.dropdown-content button').trigger('click');
.then(() => {
expect(vm.$el.querySelector('input').value).toBe(selectedZoneMock); return wrapper.vm.$nextTick().then(() => {
done(); expect(dropdown.attributes('value')).toBe(selectedZoneMock);
}) });
.catch(done.fail);
})
.catch(done.fail);
}); });
}); });
}); });
rules:
# https://gitlab.com/gitlab-org/gitlab/issues/33025
promise/no-nesting: off
export const emptyProjectMock = {
projectId: '',
name: '',
};
export const selectedProjectMock = {
projectId: 'gcp-project-123',
name: 'gcp-project',
};
export const selectedZoneMock = 'us-central1-a';
export const selectedMachineTypeMock = 'n1-standard-2';
export const gapiProjectsResponseMock = {
projects: [
{
projectNumber: '1234',
projectId: 'gcp-project-123',
lifecycleState: 'ACTIVE',
name: 'gcp-project',
createTime: '2017-12-16T01:48:29.129Z',
parent: {
type: 'organization',
id: '12345',
},
},
],
};
export const gapiZonesResponseMock = {
kind: 'compute#zoneList',
id: 'projects/gitlab-internal-153318/zones',
items: [
{
kind: 'compute#zone',
id: '2000',
creationTimestamp: '1969-12-31T16:00:00.000-08:00',
name: 'us-central1-a',
description: 'us-central1-a',
status: 'UP',
region:
'https://www.googleapis.com/compute/v1/projects/gitlab-internal-153318/regions/us-central1',
selfLink:
'https://www.googleapis.com/compute/v1/projects/gitlab-internal-153318/zones/us-central1-a',
availableCpuPlatforms: ['Intel Skylake', 'Intel Broadwell', 'Intel Sandy Bridge'],
},
],
selfLink: 'https://www.googleapis.com/compute/v1/projects/gitlab-internal-153318/zones',
};
export const gapiMachineTypesResponseMock = {
kind: 'compute#machineTypeList',
id: 'projects/gitlab-internal-153318/zones/us-central1-a/machineTypes',
items: [
{
kind: 'compute#machineType',
id: '3002',
creationTimestamp: '1969-12-31T16:00:00.000-08:00',
name: 'n1-standard-2',
description: '2 vCPUs, 7.5 GB RAM',
guestCpus: 2,
memoryMb: 7680,
imageSpaceGb: 10,
maximumPersistentDisks: 64,
maximumPersistentDisksSizeGb: '65536',
zone: 'us-central1-a',
selfLink:
'https://www.googleapis.com/compute/v1/projects/gitlab-internal-153318/zones/us-central1-a/machineTypes/n1-standard-2',
isSharedCpu: false,
},
],
selfLink:
'https://www.googleapis.com/compute/v1/projects/gitlab-internal-153318/zones/us-central1-a/machineTypes',
};
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