Commit 88396383 authored by pburdette's avatar pburdette

Apply code review suggestions

Apply recomended code review
suggestions to clean up code.
parent e483d560
import { extractEnvironmentScopes } from './utils';
/* eslint-disable import/prefer-default-export */
// Disabling import/prefer-default-export can be
// removed once a second getter is added to this file
import { uniq } from 'lodash';
export const joinedEnvironments = state => {
if (state.variables) {
const joined = state.environments.concat(extractEnvironmentScopes(state.variables));
return [...new Set(joined)].sort();
}
return null;
const scopesFromVariables = (state.variables || []).map(variable => variable.environment_scope);
return uniq(state.environments.concat(scopesFromVariables)).sort();
};
// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};
......@@ -41,14 +41,3 @@ export const prepareDataForApi = (variable, destroy = false) => {
};
export const prepareEnvironments = environments => environments.map(e => e.name);
/**
* Extract environment_scope from any variables that were created using
* wildcard environment scope
* @param {Array} variables - Array of variable objects
* @returns {Array} - Array of variable enviroment scopes
*/
export const extractEnvironmentScopes = variables => {
return [...new Set(variables.map(variable => variable.environment_scope))];
};
......@@ -30,12 +30,9 @@ describe('Ci environments dropdown', () => {
const findDropdownItemByIndex = index => wrapper.findAll(GlDropdownItem).at(index);
const findActiveIconByIndex = index => wrapper.findAll(GlIcon).at(index);
beforeEach(() => {
createComponent('prod');
});
afterEach(() => {
wrapper.destroy();
wrapper = null;
});
describe('No enviroments found', () => {
......@@ -53,14 +50,23 @@ describe('Ci environments dropdown', () => {
});
});
describe('Enviroments found', () => {
it('renders all enviroments when search term is empty', () => {
describe('Search term is empty', () => {
beforeEach(() => {
createComponent('');
});
it('renders all enviroments when search term is empty', () => {
expect(findAllDropdownItems()).toHaveLength(3);
expect(findDropdownItemByIndex(0).text()).toBe('dev');
expect(findDropdownItemByIndex(1).text()).toBe('prod');
expect(findDropdownItemByIndex(2).text()).toBe('staging');
});
});
describe('Enviroments found', () => {
beforeEach(() => {
createComponent('prod');
});
it('renders only the enviroment searched for', () => {
expect(findAllDropdownItems()).toHaveLength(1);
......@@ -78,21 +84,20 @@ describe('Ci environments dropdown', () => {
});
it('should display active checkmark if active', () => {
createComponent('dev');
expect(findActiveIconByIndex(0).classes('invisible')).toBe(false);
});
});
describe('Custom events', () => {
it('should emit selectEnvironment if an environment is clicked', () => {
findDropdownItemByIndex(0).vm.$emit('click');
expect(wrapper.emitted('selectEnvironment')).toEqual([['prod']]);
});
it('should emit createClicked if an environment is clicked', () => {
createComponent('newscope');
findDropdownItemByIndex(1).vm.$emit('click');
expect(wrapper.emitted('createClicked')).toEqual([['newscope']]);
describe('Custom events', () => {
it('should emit selectEnvironment if an environment is clicked', () => {
findDropdownItemByIndex(0).vm.$emit('click');
expect(wrapper.emitted('selectEnvironment')).toEqual([['prod']]);
});
it('should emit createClicked if an environment is clicked', () => {
createComponent('newscope');
findDropdownItemByIndex(1).vm.$emit('click');
expect(wrapper.emitted('createClicked')).toEqual([['newscope']]);
});
});
});
});
......@@ -3,7 +3,7 @@ import mockData from '../services/mock_data';
describe('Ci variable getters', () => {
describe('joinedEnvironments', () => {
it('should return "on" when isShowingLabels is true', () => {
it('should join fetched enviroments with variable environment scopes', () => {
const state = {
environments: ['All (default)', 'staging', 'deployment', 'prod'],
variables: mockData.mockVariableScopes,
......
......@@ -2,7 +2,6 @@ import {
prepareDataForDisplay,
prepareEnvironments,
prepareDataForApi,
extractEnvironmentScopes,
} from '~/ci_variable_list/store/utils';
import mockData from '../services/mock_data';
......@@ -47,12 +46,4 @@ describe('CI variables store utils', () => {
it('prepares environments for display', () => {
expect(prepareEnvironments(mockData.mockEnvironments)).toStrictEqual(['staging', 'production']);
});
it('returns variable environment scopes with no duplicates', () => {
expect(extractEnvironmentScopes(mockData.mockVariableScopes)).toStrictEqual([
'All (default)',
'staging',
'production',
]);
});
});
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