Commit a1c147b8 authored by Tom Quirk's avatar Tom Quirk

Move filterGitlabCiYmls to helper function

Addresses reviewer feedback an adds unit test
parent 66fba1ef
...@@ -7,6 +7,7 @@ import { ...@@ -7,6 +7,7 @@ import {
GlSearchBoxByType, GlSearchBoxByType,
} from '@gitlab/ui'; } from '@gitlab/ui';
import { s__ } from '~/locale'; import { s__ } from '~/locale';
import { filterGitlabCiYmls } from './helpers';
export default { export default {
name: 'CiTemplateDropdown', name: 'CiTemplateDropdown',
...@@ -37,19 +38,7 @@ export default { ...@@ -37,19 +38,7 @@ export default {
return this.gitlabCiYmls; return this.gitlabCiYmls;
} }
return Object.keys(this.gitlabCiYmls).reduce((filteredYmls, category) => { return filterGitlabCiYmls(this.gitlabCiYmls, this.searchTerm);
const categoryYmls = this.gitlabCiYmls[category].filter((yml) =>
yml.name.toLowerCase().startsWith(this.searchTerm),
);
if (categoryYmls.length > 0) {
Object.assign(filteredYmls, {
[category]: categoryYmls,
});
}
return filteredYmls;
}, {});
}, },
filteredTemplateCategories() { filteredTemplateCategories() {
return Object.keys(this.filteredYmls); return Object.keys(this.filteredYmls);
......
/**
* Filters [gitlabCiYmls] based on a given [searchTerm].
* Yml catagories with no items after filtering are not included in the returned object.
* @param {Object} gitlabCiYmls - { <categoryName>: [{ name, id }] }
* @param {String} searchTerm
* @returns {Object}
*/
export function filterGitlabCiYmls(gitlabCiYmls, searchTerm) {
return Object.keys(gitlabCiYmls).reduce((filteredYmls, category) => {
const categoryYmls = gitlabCiYmls[category].filter((yml) =>
yml.name.toLowerCase().startsWith(searchTerm),
);
if (categoryYmls.length > 0) {
Object.assign(filteredYmls, {
[category]: categoryYmls,
});
}
return filteredYmls;
}, {});
}
import { filterGitlabCiYmls } from 'ee/pages/admin/application_settings/ci_cd/helpers';
describe('CI/CD helpers', () => {
const Yml = (name) => ({ name, id: name });
it.each`
gitlabCiYmls | searchTerm | result
${{ CatA: [Yml('test'), Yml('node')], CatB: [Yml('test')] }} | ${'t'} | ${{ CatA: [Yml('test')], CatB: [Yml('test')] }}
${{ CatA: [Yml('test'), Yml('tether')], CatB: [Yml('test')] }} | ${'tet'} | ${{ CatA: [Yml('tether')] }}
${{ CatA: [Yml('test'), Yml('node')], CatB: [Yml('test')] }} | ${'n'} | ${{ CatA: [Yml('node')] }}
${{ CatA: [Yml('test'), Yml('node')], CatB: [Yml('test')] }} | ${'asd'} | ${{}}
`(
'returns filtered list with correct categories when search term is $searchTerm',
({ gitlabCiYmls, searchTerm, result }) => {
expect(filterGitlabCiYmls(gitlabCiYmls, searchTerm)).toEqual(result);
},
);
});
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