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