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';
import LabelToken from '../../shared/components/tokens/label_token.vue';
import UserToken from '../../shared/components/tokens/user_token.vue';
const prepareTokens = ({
selectedMilestone = null,
selectedAuthor = null,
selectedAssignees = [],
selectedLabels,
}) => {
const authorToken = selectedAuthor ? { type: 'author', value: { data: selectedAuthor } } : null;
const milestoneToken = selectedMilestone
? { type: 'milestone', value: { data: selectedMilestone } }
: null;
const assigneeTokens = selectedAssignees.length
? selectedAssignees.map(data => ({ type: 'assignees', value: { data } }))
export const prepareTokens = ({
milestone = null,
author = null,
assignees = [],
labels = [],
} = {}) => {
const authorToken = author ? [{ type: 'author', value: { data: author } }] : [];
const milestoneToken = milestone ? [{ type: 'milestone', value: { data: milestone } }] : [];
const assigneeTokens = assignees?.length
? assignees.map(data => ({ type: 'assignees', value: { data } }))
: [];
const labelTokens = selectedLabels.length
? selectedLabels.map(data => ({ type: 'labels', value: { data } }))
const labelTokens = labels?.length
? labels.map(data => ({ type: 'labels', value: { data } }))
: [];
let initTokens = [...assigneeTokens, ...labelTokens];
if (authorToken) initTokens = [...initTokens, authorToken];
if (milestoneToken) initTokens = [...initTokens, milestoneToken];
return initTokens;
return [...authorToken, ...milestoneToken, ...assigneeTokens, ...labelTokens];
};
export default {
......@@ -111,7 +106,13 @@ export default {
methods: {
...mapActions('filters', ['setFilters']),
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;
},
processFilters(filters) {
......
import { createLocalVue, shallowMount } from '@vue/test-utils';
import Vuex from 'vuex';
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 { filterMilestones, filterLabels } from '../mock_data';
......@@ -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