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,
|
2022-08-13 15:12:31 +05:30
|
|
|
} from 'ee_else_ce/runner/runner_search_utils';
|
|
|
|
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([
|
|
|
|
{ type: 'filtered-search-term', value: { data: 'my' } },
|
|
|
|
{ type: 'filtered-search-term', value: { data: 'text' } },
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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',
|
|
|
|
'http://test.host/?search=my_text',
|
|
|
|
])('When a filter is removed, it is removed from the URL', (initalUrl) => {
|
|
|
|
const search = { filters: [], sort: 'CREATED_DESC' };
|
|
|
|
const expectedUrl = `http://test.host/`;
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
expect(fromSearchToUrl(search, initalUrl)).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`;
|
|
|
|
const search = { filters: [], sort: 'CREATED_DESC' };
|
|
|
|
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: [
|
|
|
|
{
|
|
|
|
type: 'filtered-search-term',
|
|
|
|
value: { data: '' },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
).toMatchObject({
|
|
|
|
search: '',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
fromSearchToVariables({
|
|
|
|
filters: [
|
|
|
|
{
|
|
|
|
type: 'filtered-search-term',
|
|
|
|
value: { data: 'something' },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'filtered-search-term',
|
|
|
|
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
|
|
|
});
|