Skip to content

Commit

Permalink
Fixes #22698 - clear the search filter
Browse files Browse the repository at this point in the history
clearing the search filter does not take place when submitting

Signed-off-by: Boaz Shuster <boaz.shuster.github@gmail.com>
  • Loading branch information
boaz0 authored and tbrisker committed May 22, 2018
1 parent 993f5ab commit 2ce888d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 41 deletions.
51 changes: 15 additions & 36 deletions webpack/assets/javascripts/foreman_tools.js
Expand Up @@ -103,43 +103,22 @@ export function initTypeAheadSelect(input) {
// handle table updates via turoblinks
export function updateTable(element) {
const uri = new URI(window.location.href);
let searchTerm;
let perPage;
let isSearchForm;
let pageNum;

if (element !== undefined) {
isSearchForm = element.id === 'search-form';

pageNum = $('#cur_page_num').val();
if (pageNum !== undefined) {
uri.setSearch('page', pageNum);
}

if (isSearchForm || element.id === 'per_page') {
uri.setSearch('page', 1);
}

if (isSearchForm) {
searchTerm = $(element)
.find('.autocomplete-input')
.val();
if (searchTerm) {
uri.setSearch('search', searchTerm.trim());
}
}

perPage = $('#per_page').val();
if (
perPage !== undefined &&
$('#search-form')
.find('.autocomplete-input')
.val() === undefined
) {
uri.removeSearch('search');
}
uri.setSearch('per_page', perPage);

const values = { };


if (['per_page', 'search-form'].includes(element.id)) {
values.page = '1';
} else {
values.page = $('#cur_page_num').val();
}

const searchTerm = $(element).find('.autocomplete-input').val();
if (searchTerm !== undefined) {
values.search = searchTerm.trim();
}
values.per_page = $('#per_page').val();
uri.setSearch(values);

/* eslint-disable no-undef */
Turbolinks.visit(uri.toString());
Expand Down
21 changes: 16 additions & 5 deletions webpack/assets/javascripts/foreman_tools.test.js
Expand Up @@ -143,11 +143,6 @@ describe('updateTableTest', () => {
`;
});

it('should use turoblinks', () => {
tools.updateTable();
expect(global.Turbolinks.visit).toBeCalled();
});

it('should use selected per page value and add it to the url considering search term and pagination', () => {
const PerPage = $('#per_page').val();

Expand Down Expand Up @@ -178,4 +173,20 @@ describe('updateTableTest', () => {
$('#search-form').submit();
expect(global.Turbolinks.visit).toHaveBeenLastCalledWith(`http://localhost/?page=1&search=test&per_page=${PerPage}`);
});

it('should not reset search when set the per_page param', () => {
window.location.href = 'http://localhost/?search=blue';
$('#per_page').val('20');
$('#pagination').submit();
expect(global.Turbolinks.visit).toHaveBeenLastCalledWith('http://localhost/?search=blue&page=1&per_page=20');
});

it('should remove search param if search is empty', () => {
['', ' '].map((searchValue) => {
const PerPage = $('#per_page').val();
$('.autocomplete-input').val(searchValue);
$('#search-form').submit();
return expect(global.Turbolinks.visit).toHaveBeenLastCalledWith(`http://localhost/?page=1&search=&per_page=${PerPage}`);
});
});
});

0 comments on commit 2ce888d

Please sign in to comment.