Commit 4e0e0552 authored by Dennis Tang's avatar Dennis Tang

fix tests

each suite needs its own instance of the store, but the state has to be returned via a factory otherwise the suites all share the same reference to one state object
parent c960fea1
...@@ -3,13 +3,16 @@ import Vuex from 'vuex'; ...@@ -3,13 +3,16 @@ import Vuex from 'vuex';
import * as actions from './actions'; import * as actions from './actions';
import * as getters from './getters'; import * as getters from './getters';
import mutations from './mutations'; import mutations from './mutations';
import state from './state'; import { state } from './state';
Vue.use(Vuex); Vue.use(Vuex);
export default new Vuex.Store({ export const createStore = () =>
new Vuex.Store({
actions, actions,
getters, getters,
mutations, mutations,
state, state: state(),
}); });
export default createStore();
export default { export const state = () => ({
selectedProject: { selectedProject: {
projectId: '', projectId: '',
name: '', name: '',
...@@ -9,4 +9,6 @@ export default { ...@@ -9,4 +9,6 @@ export default {
projects: [], projects: [],
zones: [], zones: [],
machineTypes: [], machineTypes: [],
}; });
export default state();
import Vue from 'vue'; import Vue from 'vue';
import GkeMachineTypeDropdown from '~/projects/gke_cluster_dropdowns/components/gke_machine_type_dropdown.vue'; import GkeMachineTypeDropdown from '~/projects/gke_cluster_dropdowns/components/gke_machine_type_dropdown.vue';
import { createStore } from '~/projects/gke_cluster_dropdowns/store';
import { import {
SET_PROJECT, SET_PROJECT,
SET_PROJECT_BILLING_STATUS,
SET_ZONE, SET_ZONE,
SET_MACHINE_TYPES, SET_MACHINE_TYPES,
} from '~/projects/gke_cluster_dropdowns/store/mutation_types'; } from '~/projects/gke_cluster_dropdowns/store/mutation_types';
import mountComponent from 'spec/helpers/vue_mount_component_helper'; import { mountComponentWithStore } from 'spec/helpers/vue_mount_component_helper';
import { resetStore } from '../helpers';
import { import {
selectedZoneMock, selectedZoneMock,
selectedProjectMock, selectedProjectMock,
...@@ -27,23 +27,27 @@ const LABELS = { ...@@ -27,23 +27,27 @@ const LABELS = {
DEFAULT: 'Select machine type', DEFAULT: 'Select machine type',
}; };
const createComponent = (config = componentConfig) => { const createComponent = (store, props = componentConfig) => {
const Component = Vue.extend(GkeMachineTypeDropdown); const Component = Vue.extend(GkeMachineTypeDropdown);
return mountComponent(Component, config); return mountComponentWithStore(Component, {
el: null,
props,
store,
});
}; };
describe('GkeMachineTypeDropdown', () => { describe('GkeMachineTypeDropdown', () => {
let vm; let vm;
let store;
beforeEach(() => { beforeEach(() => {
vm = createComponent(); store = createStore();
vm = createComponent(store);
}); });
afterEach(() => { afterEach(() => {
vm.$destroy(); vm.$destroy();
resetStore(vm.$store);
}); });
describe('shows various toggle text depending on state', () => { describe('shows various toggle text depending on state', () => {
...@@ -53,6 +57,7 @@ describe('GkeMachineTypeDropdown', () => { ...@@ -53,6 +57,7 @@ describe('GkeMachineTypeDropdown', () => {
it('returns disabled state toggle text when no zone is selected', () => { it('returns disabled state toggle text when no zone is selected', () => {
vm.$store.commit(SET_PROJECT, selectedProjectMock); vm.$store.commit(SET_PROJECT, selectedProjectMock);
vm.$store.commit(SET_PROJECT_BILLING_STATUS, true);
expect(vm.toggleText).toBe(LABELS.DISABLED_NO_ZONE); expect(vm.toggleText).toBe(LABELS.DISABLED_NO_ZONE);
}); });
...@@ -67,6 +72,7 @@ describe('GkeMachineTypeDropdown', () => { ...@@ -67,6 +72,7 @@ describe('GkeMachineTypeDropdown', () => {
expect(vm.toggleText).toBe(LABELS.DISABLED_NO_PROJECT); expect(vm.toggleText).toBe(LABELS.DISABLED_NO_PROJECT);
vm.$store.commit(SET_PROJECT, selectedProjectMock); vm.$store.commit(SET_PROJECT, selectedProjectMock);
vm.$store.commit(SET_PROJECT_BILLING_STATUS, true);
vm.$store.commit(SET_ZONE, selectedZoneMock); vm.$store.commit(SET_ZONE, selectedZoneMock);
expect(vm.toggleText).toBe(LABELS.DEFAULT); expect(vm.toggleText).toBe(LABELS.DEFAULT);
......
import Vue from 'vue'; import Vue from 'vue';
import GkeProjectIdDropdown from '~/projects/gke_cluster_dropdowns/components/gke_project_id_dropdown.vue'; import GkeProjectIdDropdown from '~/projects/gke_cluster_dropdowns/components/gke_project_id_dropdown.vue';
import { createStore } from '~/projects/gke_cluster_dropdowns/store';
import { SET_PROJECTS } from '~/projects/gke_cluster_dropdowns/store/mutation_types'; import { SET_PROJECTS } from '~/projects/gke_cluster_dropdowns/store/mutation_types';
import mountComponent from 'spec/helpers/vue_mount_component_helper'; import { mountComponentWithStore } from 'spec/helpers/vue_mount_component_helper';
import { resetStore } from '../helpers';
import { emptyProjectMock, selectedProjectMock } from '../mock_data'; import { emptyProjectMock, selectedProjectMock } from '../mock_data';
const componentConfig = { const componentConfig = {
...@@ -18,23 +17,27 @@ const LABELS = { ...@@ -18,23 +17,27 @@ const LABELS = {
EMPTY: 'No projects found', EMPTY: 'No projects found',
}; };
const createComponent = (config = componentConfig) => { const createComponent = (store, props = componentConfig) => {
const Component = Vue.extend(GkeProjectIdDropdown); const Component = Vue.extend(GkeProjectIdDropdown);
return mountComponent(Component, config); return mountComponentWithStore(Component, {
el: null,
props,
store,
});
}; };
describe('GkeProjectIdDropdown', () => { describe('GkeProjectIdDropdown', () => {
let vm; let vm;
let store;
beforeEach(() => { beforeEach(() => {
vm = createComponent(); store = createStore();
vm = createComponent(store);
}); });
afterEach(() => { afterEach(() => {
vm.$destroy(); vm.$destroy();
resetStore(vm.$store);
}); });
describe('toggleText', () => { describe('toggleText', () => {
......
import Vue from 'vue'; import Vue from 'vue';
import GkeZoneDropdown from '~/projects/gke_cluster_dropdowns/components/gke_zone_dropdown.vue'; import GkeZoneDropdown from '~/projects/gke_cluster_dropdowns/components/gke_zone_dropdown.vue';
import { SET_PROJECT, SET_ZONES } from '~/projects/gke_cluster_dropdowns/store/mutation_types'; import { createStore } from '~/projects/gke_cluster_dropdowns/store';
import mountComponent from 'spec/helpers/vue_mount_component_helper'; import {
SET_PROJECT,
import { resetStore } from '../helpers'; SET_ZONES,
SET_PROJECT_BILLING_STATUS,
} from '~/projects/gke_cluster_dropdowns/store/mutation_types';
import { mountComponentWithStore } from 'spec/helpers/vue_mount_component_helper';
import { selectedZoneMock, selectedProjectMock, gapiZonesResponseMock } from '../mock_data'; import { selectedZoneMock, selectedProjectMock, gapiZonesResponseMock } from '../mock_data';
const componentConfig = { const componentConfig = {
...@@ -17,23 +20,27 @@ const LABELS = { ...@@ -17,23 +20,27 @@ const LABELS = {
DEFAULT: 'Select zone', DEFAULT: 'Select zone',
}; };
const createComponent = (config = componentConfig) => { const createComponent = (store, props = componentConfig) => {
const Component = Vue.extend(GkeZoneDropdown); const Component = Vue.extend(GkeZoneDropdown);
return mountComponent(Component, config); return mountComponentWithStore(Component, {
el: null,
props,
store,
});
}; };
describe('GkeZoneDropdown', () => { describe('GkeZoneDropdown', () => {
let vm; let vm;
let store;
beforeEach(() => { beforeEach(() => {
vm = createComponent(); store = createStore();
vm = createComponent(store);
}); });
afterEach(() => { afterEach(() => {
vm.$destroy(); vm.$destroy();
resetStore(vm.$store);
}); });
describe('toggleText', () => { describe('toggleText', () => {
...@@ -51,6 +58,8 @@ describe('GkeZoneDropdown', () => { ...@@ -51,6 +58,8 @@ describe('GkeZoneDropdown', () => {
expect(vm.toggleText).toBe(LABELS.DISABLED); expect(vm.toggleText).toBe(LABELS.DISABLED);
vm.$store.commit(SET_PROJECT, selectedProjectMock); vm.$store.commit(SET_PROJECT, selectedProjectMock);
vm.$store.commit(SET_PROJECT_BILLING_STATUS, true);
expect(vm.toggleText).toBe(LABELS.DEFAULT); expect(vm.toggleText).toBe(LABELS.DEFAULT);
}); });
......
...@@ -4,24 +4,19 @@ import { ...@@ -4,24 +4,19 @@ import {
gapiMachineTypesResponseMock, gapiMachineTypesResponseMock,
} from './mock_data'; } from './mock_data';
// eslint-disable-next-line import/prefer-default-export
export const resetStore = store => {
store.replaceState({
selectedProject: {
projectId: '',
name: '',
},
selectedZone: '',
selectedMachineType: '',
projects: [],
zones: [],
machineTypes: [],
});
};
// eslint-disable-next-line import/prefer-default-export // eslint-disable-next-line import/prefer-default-export
export const gapi = () => ({ export const gapi = () => ({
client: { client: {
cloudbilling: {
projects: {
getBillingInfo: () =>
new Promise(resolve => {
resolve({
result: { billingEnabled: true },
});
}),
},
},
cloudresourcemanager: { cloudresourcemanager: {
projects: { projects: {
list: () => list: () =>
......
import testAction from 'spec/helpers/vuex_action_helper'; import testAction from 'spec/helpers/vuex_action_helper';
import * as actions from '~/projects/gke_cluster_dropdowns/store/actions'; import * as actions from '~/projects/gke_cluster_dropdowns/store/actions';
import store from '~/projects/gke_cluster_dropdowns/store'; import { createStore } from '~/projects/gke_cluster_dropdowns/store';
import { resetStore, gapi } from '../helpers'; import { gapi } from '../helpers';
import { selectedProjectMock, selectedZoneMock, selectedMachineTypeMock } from '../mock_data'; import { selectedProjectMock, selectedZoneMock, selectedMachineTypeMock } from '../mock_data';
describe('GCP Cluster Dropdown Store Actions', () => { describe('GCP Cluster Dropdown Store Actions', () => {
afterEach(() => { let store;
resetStore(store);
beforeEach(() => {
store = createStore();
}); });
describe('setProject', () => { describe('setProject', () => {
......
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