2021-03-30 17:10:07 -04:00
|
|
|
import { FILTERED_SEARCH_TERM } from '~/packages_and_registries/shared/constants';
|
2021-03-29 08:09:14 -04:00
|
|
|
import {
|
|
|
|
getQueryParams,
|
|
|
|
keyValueToFilterToken,
|
|
|
|
searchArrayToFilterTokens,
|
2021-03-30 17:10:07 -04:00
|
|
|
extractFilterAndSorting,
|
2021-12-13 07:12:59 -05:00
|
|
|
beautifyPath,
|
|
|
|
getCommitLink,
|
2021-03-29 08:09:14 -04:00
|
|
|
} from '~/packages_and_registries/shared/utils';
|
|
|
|
|
2021-12-13 07:12:59 -05:00
|
|
|
import { packageList } from 'jest/packages_and_registries/infrastructure_registry/components/mock_data';
|
|
|
|
|
2021-03-29 08:09:14 -04:00
|
|
|
describe('Packages And Registries shared utils', () => {
|
|
|
|
describe('getQueryParams', () => {
|
|
|
|
it('returns an object from a query string, with arrays', () => {
|
|
|
|
const queryString = 'foo=bar&baz[]=1&baz[]=2';
|
|
|
|
|
|
|
|
expect(getQueryParams(queryString)).toStrictEqual({ foo: 'bar', baz: ['1', '2'] });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('keyValueToFilterToken', () => {
|
|
|
|
it('returns an object in the correct form', () => {
|
|
|
|
const type = 'myType';
|
|
|
|
const data = 1;
|
|
|
|
|
|
|
|
expect(keyValueToFilterToken(type, data)).toStrictEqual({ type, value: { data } });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('searchArrayToFilterTokens', () => {
|
|
|
|
it('returns an array of objects in the correct form', () => {
|
|
|
|
const search = ['one', 'two'];
|
|
|
|
|
|
|
|
expect(searchArrayToFilterTokens(search)).toStrictEqual([
|
2021-03-30 17:10:07 -04:00
|
|
|
{ type: FILTERED_SEARCH_TERM, value: { data: 'one' } },
|
|
|
|
{ type: FILTERED_SEARCH_TERM, value: { data: 'two' } },
|
2021-03-29 08:09:14 -04:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2021-03-30 17:10:07 -04:00
|
|
|
describe('extractFilterAndSorting', () => {
|
|
|
|
it.each`
|
|
|
|
search | type | sort | orderBy | result
|
|
|
|
${['one']} | ${'myType'} | ${'asc'} | ${'foo'} | ${{ sorting: { sort: 'asc', orderBy: 'foo' }, filters: [{ type: 'type', value: { data: 'myType' } }, { type: FILTERED_SEARCH_TERM, value: { data: 'one' } }] }}
|
|
|
|
${['one']} | ${null} | ${'asc'} | ${'foo'} | ${{ sorting: { sort: 'asc', orderBy: 'foo' }, filters: [{ type: FILTERED_SEARCH_TERM, value: { data: 'one' } }] }}
|
|
|
|
${[]} | ${null} | ${'asc'} | ${'foo'} | ${{ sorting: { sort: 'asc', orderBy: 'foo' }, filters: [] }}
|
|
|
|
${null} | ${null} | ${'asc'} | ${'foo'} | ${{ sorting: { sort: 'asc', orderBy: 'foo' }, filters: [] }}
|
|
|
|
${null} | ${null} | ${null} | ${'foo'} | ${{ sorting: { orderBy: 'foo' }, filters: [] }}
|
|
|
|
${null} | ${null} | ${null} | ${null} | ${{ sorting: {}, filters: [] }}
|
|
|
|
`(
|
|
|
|
'returns sorting and filters objects in the correct form',
|
|
|
|
({ search, type, sort, orderBy, result }) => {
|
|
|
|
const queryObject = {
|
|
|
|
search,
|
|
|
|
type,
|
|
|
|
sort,
|
|
|
|
orderBy,
|
|
|
|
};
|
|
|
|
expect(extractFilterAndSorting(queryObject)).toStrictEqual(result);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
2021-12-13 07:12:59 -05:00
|
|
|
|
|
|
|
describe('beautifyPath', () => {
|
|
|
|
it('returns a string with spaces around /', () => {
|
|
|
|
expect(beautifyPath('foo/bar')).toBe('foo / bar');
|
|
|
|
});
|
|
|
|
it('does not fail for empty string', () => {
|
|
|
|
expect(beautifyPath()).toBe('');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getCommitLink', () => {
|
|
|
|
it('returns a relative link when isGroup is false', () => {
|
|
|
|
const link = getCommitLink(packageList[0], false);
|
|
|
|
|
|
|
|
expect(link).toContain('../commit');
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when isGroup is true', () => {
|
|
|
|
it('returns an absolute link matching project path', () => {
|
|
|
|
const mavenPackage = packageList[0];
|
|
|
|
const link = getCommitLink(mavenPackage, true);
|
|
|
|
|
|
|
|
expect(link).toContain(`/${mavenPackage.project_path}/commit`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-03-29 08:09:14 -04:00
|
|
|
});
|