2021-09-04 01:27:46 +05:30
|
|
|
import {
|
2021-12-11 22:18:48 +05:30
|
|
|
searchValidator,
|
2022-03-02 08:16:31 +05:30
|
|
|
updateOutdatedUrl,
|
2021-09-04 01:27:46 +05:30
|
|
|
fromUrlQueryToSearch,
|
|
|
|
fromSearchToUrl,
|
|
|
|
fromSearchToVariables,
|
2022-07-23 23:45:48 +05:30
|
|
|
isSearchFiltered,
|
2023-01-13 00:05:48 +05:30
|
|
|
} from 'ee_else_ce/ci/runner/runner_search_utils';
|
2023-03-04 22:38:38 +05:30
|
|
|
import { FILTERED_SEARCH_TERM } from '~/vue_shared/components/filtered_search_bar/constants';
|
2023-06-20 00:43:36 +05:30
|
|
|
import { DEFAULT_SORT } from '~/ci/runner/constants';
|
2022-08-13 15:12:31 +05:30
|
|
|
import { mockSearchExamples } from './mock_data';
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
describe('search_params.js', () => {
|
2021-12-11 22:18:48 +05:30
|
|
|
describe('searchValidator', () => {
|
2022-08-13 15:12:31 +05:30
|
|
|
mockSearchExamples.forEach(({ name, search }) => {
|
2021-12-11 22:18:48 +05:30
|
|
|
it(`Validates ${name} as a search object`, () => {
|
|
|
|
expect(searchValidator(search)).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
describe('updateOutdatedUrl', () => {
|
|
|
|
it('returns null for urls that do not need updating', () => {
|
|
|
|
expect(updateOutdatedUrl('http://test.host/')).toBe(null);
|
|
|
|
expect(updateOutdatedUrl('http://test.host/?a=b')).toBe(null);
|
|
|
|
});
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
it.each`
|
2022-08-27 11:52:29 +05:30
|
|
|
query | updatedQuery
|
|
|
|
${'status[]=ACTIVE'} | ${'paused[]=false'}
|
|
|
|
${'status[]=ACTIVE&a=b'} | ${'a=b&paused[]=false'}
|
|
|
|
${'status[]=ACTIVE'} | ${'paused[]=false'}
|
|
|
|
${'status[]=PAUSED'} | ${'paused[]=true'}
|
|
|
|
${'page=2&after=AFTER'} | ${'after=AFTER'}
|
|
|
|
${'page=2&before=BEFORE'} | ${'before=BEFORE'}
|
|
|
|
${'status[]=PAUSED&page=2&after=AFTER'} | ${'after=AFTER&paused[]=true'}
|
2022-06-21 17:19:12 +05:30
|
|
|
`('updates "$query" to "$updatedQuery"', ({ query, updatedQuery }) => {
|
|
|
|
const mockUrl = 'http://test.host/admin/runners?';
|
2022-03-02 08:16:31 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
expect(updateOutdatedUrl(`${mockUrl}${query}`)).toBe(`${mockUrl}${updatedQuery}`);
|
2022-03-02 08:16:31 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
describe('fromUrlQueryToSearch', () => {
|
2022-08-13 15:12:31 +05:30
|
|
|
mockSearchExamples.forEach(({ name, urlQuery, search }) => {
|
2021-09-04 01:27:46 +05:30
|
|
|
it(`Converts ${name} to a search object`, () => {
|
|
|
|
expect(fromUrlQueryToSearch(urlQuery)).toEqual(search);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('When search params appear as array, they are concatenated', () => {
|
|
|
|
expect(fromUrlQueryToSearch('?search[]=my&search[]=text').filters).toEqual([
|
2023-03-04 22:38:38 +05:30
|
|
|
{ type: FILTERED_SEARCH_TERM, value: { data: 'my' } },
|
|
|
|
{ type: FILTERED_SEARCH_TERM, value: { data: 'text' } },
|
2021-09-04 01:27:46 +05:30
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('fromSearchToUrl', () => {
|
2022-08-13 15:12:31 +05:30
|
|
|
mockSearchExamples.forEach(({ name, urlQuery, search }) => {
|
2021-09-04 01:27:46 +05:30
|
|
|
it(`Converts ${name} to a url`, () => {
|
2022-07-23 23:45:48 +05:30
|
|
|
expect(fromSearchToUrl(search)).toBe(`http://test.host/${urlQuery}`);
|
2021-09-04 01:27:46 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([
|
|
|
|
'http://test.host/?status[]=ACTIVE',
|
|
|
|
'http://test.host/?runner_type[]=INSTANCE_TYPE',
|
2023-03-04 22:38:38 +05:30
|
|
|
'http://test.host/?paused[]=true',
|
2021-09-04 01:27:46 +05:30
|
|
|
'http://test.host/?search=my_text',
|
2023-03-04 22:38:38 +05:30
|
|
|
])('When a filter is removed, it is removed from the URL', (initialUrl) => {
|
2023-06-20 00:43:36 +05:30
|
|
|
const search = { filters: [], sort: DEFAULT_SORT };
|
2021-09-04 01:27:46 +05:30
|
|
|
const expectedUrl = `http://test.host/`;
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
expect(fromSearchToUrl(search, initialUrl)).toBe(expectedUrl);
|
2021-09-04 01:27:46 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('When unrelated search parameter is present, it does not get removed', () => {
|
|
|
|
const initialUrl = `http://test.host/?unrelated=UNRELATED&status[]=ACTIVE`;
|
2023-06-20 00:43:36 +05:30
|
|
|
const search = { filters: [], sort: DEFAULT_SORT };
|
2021-09-04 01:27:46 +05:30
|
|
|
const expectedUrl = `http://test.host/?unrelated=UNRELATED`;
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
expect(fromSearchToUrl(search, initialUrl)).toBe(expectedUrl);
|
2021-09-04 01:27:46 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('fromSearchToVariables', () => {
|
2022-08-13 15:12:31 +05:30
|
|
|
mockSearchExamples.forEach(({ name, graphqlVariables, search }) => {
|
2021-09-04 01:27:46 +05:30
|
|
|
it(`Converts ${name} to a GraphQL query variables object`, () => {
|
|
|
|
expect(fromSearchToVariables(search)).toEqual(graphqlVariables);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('When a search param is empty, it gets removed', () => {
|
|
|
|
expect(
|
|
|
|
fromSearchToVariables({
|
|
|
|
filters: [
|
|
|
|
{
|
2023-03-04 22:38:38 +05:30
|
|
|
type: FILTERED_SEARCH_TERM,
|
2021-09-04 01:27:46 +05:30
|
|
|
value: { data: '' },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
).toMatchObject({
|
|
|
|
search: '',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
fromSearchToVariables({
|
|
|
|
filters: [
|
|
|
|
{
|
2023-03-04 22:38:38 +05:30
|
|
|
type: FILTERED_SEARCH_TERM,
|
2021-09-04 01:27:46 +05:30
|
|
|
value: { data: 'something' },
|
|
|
|
},
|
|
|
|
{
|
2023-03-04 22:38:38 +05:30
|
|
|
type: FILTERED_SEARCH_TERM,
|
2021-09-04 01:27:46 +05:30
|
|
|
value: { data: '' },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
).toMatchObject({
|
|
|
|
search: 'something',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-07-23 23:45:48 +05:30
|
|
|
|
|
|
|
describe('isSearchFiltered', () => {
|
2022-08-13 15:12:31 +05:30
|
|
|
mockSearchExamples.forEach(({ name, search, isDefault }) => {
|
2022-07-23 23:45:48 +05:30
|
|
|
it(`Given ${name}, evaluates to ${isDefault ? 'not ' : ''}filtered`, () => {
|
|
|
|
expect(isSearchFiltered(search)).toBe(!isDefault);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
it.each([null, undefined, {}])(
|
|
|
|
'given a missing pagination, evaluates as not filtered',
|
|
|
|
(mockPagination) => {
|
|
|
|
expect(isSearchFiltered({ pagination: mockPagination })).toBe(false);
|
|
|
|
},
|
|
|
|
);
|
2022-07-23 23:45:48 +05:30
|
|
|
});
|
2021-09-04 01:27:46 +05:30
|
|
|
});
|