Commit e4f60986 authored by Ezekiel Kigbo's avatar Ezekiel Kigbo

Adds specs for prepareTokens

Minor cleanup the prepareTokens method
and adds additional specs
parent a8f27410
...@@ -6,27 +6,22 @@ import MilestoneToken from '../../shared/components/tokens/milestone_token.vue'; ...@@ -6,27 +6,22 @@ import MilestoneToken from '../../shared/components/tokens/milestone_token.vue';
import LabelToken from '../../shared/components/tokens/label_token.vue'; import LabelToken from '../../shared/components/tokens/label_token.vue';
import UserToken from '../../shared/components/tokens/user_token.vue'; import UserToken from '../../shared/components/tokens/user_token.vue';
const prepareTokens = ({ export const prepareTokens = ({
selectedMilestone = null, milestone = null,
selectedAuthor = null, author = null,
selectedAssignees = [], assignees = [],
selectedLabels, labels = [],
}) => { } = {}) => {
const authorToken = selectedAuthor ? { type: 'author', value: { data: selectedAuthor } } : null; const authorToken = author ? [{ type: 'author', value: { data: author } }] : [];
const milestoneToken = selectedMilestone const milestoneToken = milestone ? [{ type: 'milestone', value: { data: milestone } }] : [];
? { type: 'milestone', value: { data: selectedMilestone } } const assigneeTokens = assignees?.length
: null; ? assignees.map(data => ({ type: 'assignees', value: { data } }))
const assigneeTokens = selectedAssignees.length
? selectedAssignees.map(data => ({ type: 'assignees', value: { data } }))
: []; : [];
const labelTokens = selectedLabels.length const labelTokens = labels?.length
? selectedLabels.map(data => ({ type: 'labels', value: { data } })) ? labels.map(data => ({ type: 'labels', value: { data } }))
: []; : [];
let initTokens = [...assigneeTokens, ...labelTokens]; return [...authorToken, ...milestoneToken, ...assigneeTokens, ...labelTokens];
if (authorToken) initTokens = [...initTokens, authorToken];
if (milestoneToken) initTokens = [...initTokens, milestoneToken];
return initTokens;
}; };
export default { export default {
...@@ -111,7 +106,13 @@ export default { ...@@ -111,7 +106,13 @@ export default {
methods: { methods: {
...mapActions('filters', ['setFilters']), ...mapActions('filters', ['setFilters']),
initializeTokens() { initializeTokens() {
const preparedTokens = prepareTokens(this.initialTokens); const {
selectedMilestone: milestone = null,
selectedAuthor: author = null,
selectedAssignees: assignees = [],
selectedLabels: labels = [],
} = this.initialTokens;
const preparedTokens = prepareTokens({ milestone, author, assignees, labels });
this.value = preparedTokens; this.value = preparedTokens;
}, },
processFilters(filters) { processFilters(filters) {
......
import { createLocalVue, shallowMount } from '@vue/test-utils'; import { createLocalVue, shallowMount } from '@vue/test-utils';
import Vuex from 'vuex'; import Vuex from 'vuex';
import { GlFilteredSearch } from '@gitlab/ui'; import { GlFilteredSearch } from '@gitlab/ui';
import FilterBar from 'ee/analytics/cycle_analytics/components/filter_bar.vue'; import FilterBar, { prepareTokens } from 'ee/analytics/cycle_analytics/components/filter_bar.vue';
import initialFiltersState from 'ee/analytics/cycle_analytics/store/modules/filters/state'; import initialFiltersState from 'ee/analytics/cycle_analytics/store/modules/filters/state';
import { filterMilestones, filterLabels } from '../mock_data'; import { filterMilestones, filterLabels } from '../mock_data';
...@@ -176,4 +176,27 @@ describe('Filter bar', () => { ...@@ -176,4 +176,27 @@ describe('Filter bar', () => {
); );
}); });
}); });
describe('prepareTokens', () => {
describe('with empty data', () => {
it('returns an empty array', () => {
expect(prepareTokens()).toEqual([]);
expect(prepareTokens({})).toEqual([]);
expect(prepareTokens({ milestone: null, author: null, assignees: [], labels: [] })).toEqual(
[],
);
});
});
it.each`
token | value | result
${'milestone'} | ${'v1.0'} | ${[{ type: 'milestone', value: { data: 'v1.0' } }]}
${'author'} | ${'mr.popo'} | ${[{ type: 'author', value: { data: 'mr.popo' } }]}
${'labels'} | ${['z-fighters']} | ${[{ type: 'labels', value: { data: 'z-fighters' } }]}
${'assignees'} | ${['krillin', 'piccolo']} | ${[{ type: 'assignees', value: { data: 'krillin' } }, { type: 'assignees', value: { data: 'piccolo' } }]}
`('with $token=$value sets the $token key', ({ token, value, result }) => {
const res = prepareTokens({ [token]: value });
expect(res).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