Commit 4aa04fca authored by Kushal Pandya's avatar Kushal Pandya

Merge branch 'cngo-refactor-filtered-tokens' into 'master'

Do minor refactor of filtered tokens

See merge request gitlab-org/gitlab!68161
parents c1b0422c 76945154
...@@ -28,11 +28,6 @@ export default { ...@@ -28,11 +28,6 @@ export default {
}; };
}, },
methods: { methods: {
fnCurrentTokenValue(data) {
// By default, values are transformed with `toLowerCase`
// however, runner tags are case sensitive.
return data;
},
getTagsOptions(search) { getTagsOptions(search) {
// TODO This should be implemented via a GraphQL API // TODO This should be implemented via a GraphQL API
// The API should // The API should
...@@ -72,7 +67,6 @@ export default { ...@@ -72,7 +67,6 @@ export default {
:config="config" :config="config"
:suggestions-loading="loading" :suggestions-loading="loading"
:suggestions="tags" :suggestions="tags"
:fn-current-token-value="fnCurrentTokenValue"
:recent-suggestions-storage-key="config.recentTokenValuesStorageKey" :recent-suggestions-storage-key="config.recentTokenValuesStorageKey"
@fetch-suggestions="fetchTags" @fetch-suggestions="fetchTags"
v-on="$listeners" v-on="$listeners"
......
...@@ -31,19 +31,25 @@ export default { ...@@ -31,19 +31,25 @@ export default {
data() { data() {
return { return {
authors: this.config.initialAuthors || [], authors: this.config.initialAuthors || [],
defaultAuthors: this.config.defaultAuthors || [DEFAULT_LABEL_ANY],
preloadedAuthors: this.config.preloadedAuthors || [],
loading: false, loading: false,
}; };
}, },
computed: {
defaultAuthors() {
return this.config.defaultAuthors || [DEFAULT_LABEL_ANY];
},
preloadedAuthors() {
return this.config.preloadedAuthors || [];
},
},
methods: { methods: {
getActiveAuthor(authors, currentValue) { getActiveAuthor(authors, data) {
return authors.find((author) => author.username.toLowerCase() === currentValue); return authors.find((author) => author.username.toLowerCase() === data.toLowerCase());
}, },
getAvatarUrl(author) { getAvatarUrl(author) {
return author.avatarUrl || author.avatar_url; return author.avatarUrl || author.avatar_url;
}, },
fetchAuthorBySearchTerm(searchTerm) { fetchAuthors(searchTerm) {
this.loading = true; this.loading = true;
const fetchPromise = this.config.fetchPath const fetchPromise = this.config.fetchPath
? this.config.fetchAuthors(this.config.fetchPath, searchTerm) ? this.config.fetchAuthors(this.config.fetchPath, searchTerm)
...@@ -76,11 +82,11 @@ export default { ...@@ -76,11 +82,11 @@ export default {
:active="active" :active="active"
:suggestions-loading="loading" :suggestions-loading="loading"
:suggestions="authors" :suggestions="authors"
:fn-active-token-value="getActiveAuthor" :get-active-token-value="getActiveAuthor"
:default-suggestions="defaultAuthors" :default-suggestions="defaultAuthors"
:preloaded-suggestions="preloadedAuthors" :preloaded-suggestions="preloadedAuthors"
:recent-suggestions-storage-key="config.recentSuggestionsStorageKey" :recent-suggestions-storage-key="config.recentSuggestionsStorageKey"
@fetch-suggestions="fetchAuthorBySearchTerm" @fetch-suggestions="fetchAuthors"
v-on="$listeners" v-on="$listeners"
> >
<template #view="{ viewTokenProps: { inputValue, activeTokenValue } }"> <template #view="{ viewTokenProps: { inputValue, activeTokenValue } }">
...@@ -91,7 +97,7 @@ export default { ...@@ -91,7 +97,7 @@ export default {
shape="circle" shape="circle"
class="gl-mr-2" class="gl-mr-2"
/> />
<span>{{ activeTokenValue ? activeTokenValue.name : inputValue }}</span> {{ activeTokenValue ? activeTokenValue.name : inputValue }}
</template> </template>
<template #suggestions-list="{ suggestions }"> <template #suggestions-list="{ suggestions }">
<gl-filtered-search-suggestion <gl-filtered-search-suggestion
......
...@@ -42,12 +42,10 @@ export default { ...@@ -42,12 +42,10 @@ export default {
required: false, required: false,
default: () => [], default: () => [],
}, },
fnActiveTokenValue: { getActiveTokenValue: {
type: Function, type: Function,
required: false, required: false,
default: (suggestions, currentTokenValue) => { default: (suggestions, data) => suggestions.find(({ value }) => value === data),
return suggestions.find(({ value }) => value === currentTokenValue);
},
}, },
defaultSuggestions: { defaultSuggestions: {
type: Array, type: Array,
...@@ -69,11 +67,6 @@ export default { ...@@ -69,11 +67,6 @@ export default {
required: false, required: false,
default: 'id', default: 'id',
}, },
fnCurrentTokenValue: {
type: Function,
required: false,
default: null,
},
}, },
data() { data() {
return { return {
...@@ -81,7 +74,6 @@ export default { ...@@ -81,7 +74,6 @@ export default {
recentSuggestions: this.recentSuggestionsStorageKey recentSuggestions: this.recentSuggestionsStorageKey
? getRecentlyUsedSuggestions(this.recentSuggestionsStorageKey) ? getRecentlyUsedSuggestions(this.recentSuggestionsStorageKey)
: [], : [],
loading: false,
}; };
}, },
computed: { computed: {
...@@ -94,14 +86,8 @@ export default { ...@@ -94,14 +86,8 @@ export default {
preloadedTokenIds() { preloadedTokenIds() {
return this.preloadedSuggestions.map((tokenValue) => tokenValue[this.valueIdentifier]); return this.preloadedSuggestions.map((tokenValue) => tokenValue[this.valueIdentifier]);
}, },
currentTokenValue() {
if (this.fnCurrentTokenValue) {
return this.fnCurrentTokenValue(this.value.data);
}
return this.value.data.toLowerCase();
},
activeTokenValue() { activeTokenValue() {
return this.fnActiveTokenValue(this.suggestions, this.currentTokenValue); return this.getActiveTokenValue(this.suggestions, this.value.data);
}, },
/** /**
* Return all the suggestions when searchKey is present * Return all the suggestions when searchKey is present
......
...@@ -33,14 +33,18 @@ export default { ...@@ -33,14 +33,18 @@ export default {
data() { data() {
return { return {
labels: this.config.initialLabels || [], labels: this.config.initialLabels || [],
defaultLabels: this.config.defaultLabels || DEFAULT_LABELS,
loading: false, loading: false,
}; };
}, },
computed: {
defaultLabels() {
return this.config.defaultLabels || DEFAULT_LABELS;
},
},
methods: { methods: {
getActiveLabel(labels, currentValue) { getActiveLabel(labels, data) {
return labels.find( return labels.find(
(label) => this.getLabelName(label).toLowerCase() === stripQuotes(currentValue), (label) => this.getLabelName(label).toLowerCase() === stripQuotes(data).toLowerCase(),
); );
}, },
/** /**
...@@ -68,7 +72,7 @@ export default { ...@@ -68,7 +72,7 @@ export default {
} }
return {}; return {};
}, },
fetchLabelBySearchTerm(searchTerm) { fetchLabels(searchTerm) {
this.loading = true; this.loading = true;
this.config this.config
.fetchLabels(searchTerm) .fetchLabels(searchTerm)
...@@ -98,10 +102,10 @@ export default { ...@@ -98,10 +102,10 @@ export default {
:active="active" :active="active"
:suggestions-loading="loading" :suggestions-loading="loading"
:suggestions="labels" :suggestions="labels"
:fn-active-token-value="getActiveLabel" :get-active-token-value="getActiveLabel"
:default-suggestions="defaultLabels" :default-suggestions="defaultLabels"
:recent-suggestions-storage-key="config.recentSuggestionsStorageKey" :recent-suggestions-storage-key="config.recentSuggestionsStorageKey"
@fetch-suggestions="fetchLabelBySearchTerm" @fetch-suggestions="fetchLabels"
v-on="$listeners" v-on="$listeners"
> >
<template <template
......
...@@ -86,7 +86,7 @@ describe('AuthorToken', () => { ...@@ -86,7 +86,7 @@ describe('AuthorToken', () => {
}); });
describe('methods', () => { describe('methods', () => {
describe('fetchAuthorBySearchTerm', () => { describe('fetchAuthors', () => {
beforeEach(() => { beforeEach(() => {
wrapper = createComponent(); wrapper = createComponent();
}); });
...@@ -155,7 +155,7 @@ describe('AuthorToken', () => { ...@@ -155,7 +155,7 @@ describe('AuthorToken', () => {
expect(baseTokenEl.exists()).toBe(true); expect(baseTokenEl.exists()).toBe(true);
expect(baseTokenEl.props()).toMatchObject({ expect(baseTokenEl.props()).toMatchObject({
suggestions: mockAuthors, suggestions: mockAuthors,
fnActiveTokenValue: wrapper.vm.getActiveAuthor, getActiveTokenValue: wrapper.vm.getActiveAuthor,
}); });
}); });
......
...@@ -53,7 +53,6 @@ const mockProps = { ...@@ -53,7 +53,6 @@ const mockProps = {
suggestionsLoading: false, suggestionsLoading: false,
defaultSuggestions: DEFAULT_LABELS, defaultSuggestions: DEFAULT_LABELS,
recentSuggestionsStorageKey: mockStorageKey, recentSuggestionsStorageKey: mockStorageKey,
fnCurrentTokenValue: jest.fn(),
}; };
function createComponent({ function createComponent({
...@@ -99,31 +98,20 @@ describe('BaseToken', () => { ...@@ -99,31 +98,20 @@ describe('BaseToken', () => {
}); });
describe('computed', () => { describe('computed', () => {
describe('currentTokenValue', () => {
it('calls `fnCurrentTokenValue` when it is provided', () => {
// We're disabling lint to trigger computed prop execution for this test.
// eslint-disable-next-line no-unused-vars
const { currentTokenValue } = wrapper.vm;
expect(wrapper.vm.fnCurrentTokenValue).toHaveBeenCalledWith(`"${mockRegularLabel.title}"`);
});
});
describe('activeTokenValue', () => { describe('activeTokenValue', () => {
it('calls `fnActiveTokenValue` when it is provided', async () => { it('calls `getActiveTokenValue` when it is provided', async () => {
const mockFnActiveTokenValue = jest.fn(); const mockGetActiveTokenValue = jest.fn();
wrapper.setProps({ wrapper.setProps({
fnActiveTokenValue: mockFnActiveTokenValue, getActiveTokenValue: mockGetActiveTokenValue,
fnCurrentTokenValue: undefined,
}); });
await wrapper.vm.$nextTick(); await wrapper.vm.$nextTick();
expect(mockFnActiveTokenValue).toHaveBeenCalledTimes(1); expect(mockGetActiveTokenValue).toHaveBeenCalledTimes(1);
expect(mockFnActiveTokenValue).toHaveBeenCalledWith( expect(mockGetActiveTokenValue).toHaveBeenCalledWith(
mockLabels, mockLabels,
`"${mockRegularLabel.title.toLowerCase()}"`, `"${mockRegularLabel.title}"`,
); );
}); });
}); });
......
...@@ -98,11 +98,11 @@ describe('LabelToken', () => { ...@@ -98,11 +98,11 @@ describe('LabelToken', () => {
}); });
}); });
describe('fetchLabelBySearchTerm', () => { describe('fetchLabels', () => {
it('calls `config.fetchLabels` with provided searchTerm param', () => { it('calls `config.fetchLabels` with provided searchTerm param', () => {
jest.spyOn(wrapper.vm.config, 'fetchLabels'); jest.spyOn(wrapper.vm.config, 'fetchLabels');
wrapper.vm.fetchLabelBySearchTerm('foo'); wrapper.vm.fetchLabels('foo');
expect(wrapper.vm.config.fetchLabels).toHaveBeenCalledWith('foo'); expect(wrapper.vm.config.fetchLabels).toHaveBeenCalledWith('foo');
}); });
...@@ -110,7 +110,7 @@ describe('LabelToken', () => { ...@@ -110,7 +110,7 @@ describe('LabelToken', () => {
it('sets response to `labels` when request is succesful', () => { it('sets response to `labels` when request is succesful', () => {
jest.spyOn(wrapper.vm.config, 'fetchLabels').mockResolvedValue(mockLabels); jest.spyOn(wrapper.vm.config, 'fetchLabels').mockResolvedValue(mockLabels);
wrapper.vm.fetchLabelBySearchTerm('foo'); wrapper.vm.fetchLabels('foo');
return waitForPromises().then(() => { return waitForPromises().then(() => {
expect(wrapper.vm.labels).toEqual(mockLabels); expect(wrapper.vm.labels).toEqual(mockLabels);
...@@ -120,7 +120,7 @@ describe('LabelToken', () => { ...@@ -120,7 +120,7 @@ describe('LabelToken', () => {
it('calls `createFlash` with flash error message when request fails', () => { it('calls `createFlash` with flash error message when request fails', () => {
jest.spyOn(wrapper.vm.config, 'fetchLabels').mockRejectedValue({}); jest.spyOn(wrapper.vm.config, 'fetchLabels').mockRejectedValue({});
wrapper.vm.fetchLabelBySearchTerm('foo'); wrapper.vm.fetchLabels('foo');
return waitForPromises().then(() => { return waitForPromises().then(() => {
expect(createFlash).toHaveBeenCalledWith({ expect(createFlash).toHaveBeenCalledWith({
...@@ -132,7 +132,7 @@ describe('LabelToken', () => { ...@@ -132,7 +132,7 @@ describe('LabelToken', () => {
it('sets `loading` to false when request completes', () => { it('sets `loading` to false when request completes', () => {
jest.spyOn(wrapper.vm.config, 'fetchLabels').mockRejectedValue({}); jest.spyOn(wrapper.vm.config, 'fetchLabels').mockRejectedValue({});
wrapper.vm.fetchLabelBySearchTerm('foo'); wrapper.vm.fetchLabels('foo');
return waitForPromises().then(() => { return waitForPromises().then(() => {
expect(wrapper.vm.loading).toBe(false); expect(wrapper.vm.loading).toBe(false);
...@@ -160,7 +160,7 @@ describe('LabelToken', () => { ...@@ -160,7 +160,7 @@ describe('LabelToken', () => {
expect(baseTokenEl.exists()).toBe(true); expect(baseTokenEl.exists()).toBe(true);
expect(baseTokenEl.props()).toMatchObject({ expect(baseTokenEl.props()).toMatchObject({
suggestions: mockLabels, suggestions: mockLabels,
fnActiveTokenValue: wrapper.vm.getActiveLabel, getActiveTokenValue: wrapper.vm.getActiveLabel,
}); });
}); });
......
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