Commit 2ea16b66 authored by Bryce Johnson's avatar Bryce Johnson

Add JS unit tests and shush eslint.

parent 7d4e83fe
......@@ -337,7 +337,7 @@ class FilteredSearchManager {
removalValidator(token) {
const isToken = token.classList.contains('js-visual-token');
return this.customRemovalValidator ? (isToken && this.customRemovalValidator(token)) : isToken;
return this.customRemovalValidator ? (this.customRemovalValidator(token) && isToken) : isToken;
}
clearSearch() {
......@@ -426,14 +426,13 @@ class FilteredSearchManager {
}
// allows for modifying params array when a param can't be included in the URL (e.g. Service Desk)
getAllParams() {
let urlParams = gl.utils.getUrlParamsArray();
getAllParams(urlParams) {
return this.modifyUrlParams ? this.modifyUrlParams(urlParams) : urlParams;
}
loadSearchParamsFromURL() {
const params = this.getAllParams();
const urlParams = gl.utils.getUrlParamsArray();
const params = this.getAllParams(urlParams);
const usernameParams = this.getUsernameParams();
let hasFilteredSearch = false;
......
/* eslint-disable class-methods-use-this */
export default class FilteredSearchServiceDesk extends gl.FilteredSearchManager {
constructor() {
super('service_desk');
......@@ -5,7 +7,7 @@ export default class FilteredSearchServiceDesk extends gl.FilteredSearchManager
customRemovalValidator(token) {
return token.querySelector('.value-container').getAttribute('data-original-value') !== '@support-bot';
};
}
canEdit(tokenName) {
return tokenName !== 'author';
......@@ -16,9 +18,7 @@ export default class FilteredSearchServiceDesk extends gl.FilteredSearchManager
// FIXME: Need to grab the value from a data attribute
const supportBotParamPair = `${authorParamKey}=support-bot`;
const onlyValidParams = paramsArray.filter((param) => {
return param.indexOf(authorParamKey) === -1;
});
const onlyValidParams = paramsArray.filter(param => param.indexOf(authorParamKey) === -1);
// unshift ensures author param is always first token element
onlyValidParams.unshift(supportBotParamPair);
......
......@@ -411,4 +411,62 @@ describe('Filtered Search Manager', () => {
expect(document.querySelector('.filtered-search-box').classList.contains('focus')).toEqual(false);
});
});
describe('removalValidator', () => {
beforeEach(() => {
Object.assign(gl.FilteredSearchManager.prototype, {
customRemovalValidator: () => true,
});
spyOn(gl.FilteredSearchManager.prototype, 'removalValidator').and.callThrough();
spyOn(gl.FilteredSearchManager.prototype, 'customRemovalValidator').and.callThrough();
initializeManager();
});
it('is called on clearSearch', () => {
manager.clearSearch();
expect(manager.removalValidator).toHaveBeenCalled();
});
it('calls the customRemovalValidator when present', () => {
manager.clearSearch();
expect(manager.customRemovalValidator).toHaveBeenCalled();
});
});
describe('getAllParams', () => {
beforeEach(() => {
this.paramsArr = ['key=value', 'otherkey=othervalue'];
Object.assign(gl.FilteredSearchManager.prototype, {
modifyUrlParams: paramsArr => paramsArr.reverse(),
});
spyOn(gl.FilteredSearchManager.prototype, 'modifyUrlParams').and.callThrough();
initializeManager();
});
it('calls modifyUrlParams when present', () => {
manager.getAllParams(this.paramsArr);
expect(manager.modifyUrlParams).toHaveBeenCalled();
});
it('correctly modifies params when custom modifier is passed', () => {
const modifedParams = manager.getAllParams(this.paramsArr);
expect(modifedParams[0]).toBe('otherkey=othervalue');
});
it('does not modify params when no custom modifier is passed', () => {
Object.assign(gl.FilteredSearchManager.prototype, { modifyUrlParams: undefined });
const modifedParams = manager.getAllParams(this.paramsArr);
expect(modifedParams[1]).toBe('otherkey=othervalue');
});
});
});
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