strapi/packages/strapi-utils/lib/__tests__/convertRestQueryParams.test.js

398 lines
10 KiB
JavaScript
Raw Normal View History

2019-03-13 19:27:18 +01:00
const convertRestQueryParams = require('../convertRestQueryParams');
describe('convertRestQueryParams', () => {
test('Throws on invalid input', () => {
// throws when no params provided
expect(() => convertRestQueryParams(1)).toThrow();
expect(() => convertRestQueryParams('azdazd')).toThrow();
expect(() => convertRestQueryParams(null)).toThrow();
});
test('Runs correctly on valid input', () => {
// returns empty if no params
expect(convertRestQueryParams()).toMatchObject({});
expect(convertRestQueryParams({})).toMatchObject({});
expect(
convertRestQueryParams({
_sort: 'id:desc,price',
_start: '5',
_limit: '10',
})
).toMatchObject({
sort: [
{
field: 'id',
order: 'desc',
},
{
field: 'price',
order: 'asc',
},
],
start: 5,
limit: 10,
});
});
describe('Sort param', () => {
test('Throws on invalid params', () => {
// invalid sort queries
expect(() => convertRestQueryParams({ _sort: 1 })).toThrow();
expect(() => convertRestQueryParams({ _sort: {} })).toThrow();
expect(() => convertRestQueryParams({ _sort: 'id,,test' })).toThrow();
expect(() => convertRestQueryParams({ _sort: 'id,test,' })).toThrow();
2019-05-29 16:09:19 +02:00
expect(() =>
convertRestQueryParams({ _sort: 'id:asc,test:dasc' })
).toThrow();
2019-03-13 19:27:18 +01:00
expect(() => convertRestQueryParams({ _sort: 'id:asc,:asc' })).toThrow();
});
test.each([
['id', [{ field: 'id', order: 'asc' }]],
['id:desc', [{ field: 'id', order: 'desc' }]],
['id:ASC', [{ field: 'id', order: 'asc' }]],
['id:DESC', [{ field: 'id', order: 'desc' }]],
['id:asc', [{ field: 'id', order: 'asc' }]],
2019-05-29 16:09:19 +02:00
[
'id,price',
[{ field: 'id', order: 'asc' }, { field: 'price', order: 'asc' }],
],
[
'id:desc,price',
[{ field: 'id', order: 'desc' }, { field: 'price', order: 'asc' }],
],
[
'id:desc,price:desc',
[{ field: 'id', order: 'desc' }, { field: 'price', order: 'desc' }],
],
2019-03-13 19:27:18 +01:00
[
'id:asc,price,date:desc',
[
{ field: 'id', order: 'asc' },
{ field: 'price', order: 'asc' },
{ field: 'date', order: 'desc' },
],
],
[
'published_at:asc,price:ASC,date:DESC',
[
{ field: 'published_at', order: 'asc' },
{ field: 'price', order: 'asc' },
{ field: 'date', order: 'desc' },
],
],
])('Converts sort query "%s" correctly', (input, expected) => {
2019-05-29 16:09:19 +02:00
expect(convertRestQueryParams({ _sort: input })).toMatchObject({
sort: expected,
});
2019-03-13 19:27:18 +01:00
});
});
describe('Start param', () => {
test('Throws on invalid params', () => {
// invalid sort queries
expect(() => convertRestQueryParams({ _start: 'du text' })).toThrow();
expect(() => convertRestQueryParams({ _start: '12 du text' })).toThrow();
expect(() => convertRestQueryParams({ _start: '12.1' })).toThrow();
expect(() => convertRestQueryParams({ _start: 'NaN' })).toThrow();
expect(() => convertRestQueryParams({ _start: 'Infinity' })).toThrow();
expect(() => convertRestQueryParams({ _start: Infinity })).toThrow();
expect(() => convertRestQueryParams({ _start: -Infinity })).toThrow();
2019-05-29 16:09:19 +02:00
expect(() => convertRestQueryParams({ _start: NaN })).toThrow();
2019-03-13 19:27:18 +01:00
expect(() => convertRestQueryParams({ _start: 1.2 })).toThrow();
expect(() => convertRestQueryParams({ _start: -10 })).toThrow();
expect(() => convertRestQueryParams({ _start: {} })).toThrow();
});
test.each([['1', 1], ['12', 12], ['0', 0]])(
'Converts start query "%s" correctly',
(input, expected) => {
2019-05-29 16:09:19 +02:00
expect(convertRestQueryParams({ _start: input })).toMatchObject({
start: expected,
});
2019-03-13 19:27:18 +01:00
}
);
});
describe('Limit param', () => {
test('Throws on invalid params', () => {
// invalid sort queries
expect(() => convertRestQueryParams({ _limit: 'du text' })).toThrow();
expect(() => convertRestQueryParams({ _limit: '12 du text' })).toThrow();
expect(() => convertRestQueryParams({ _limit: '12.1' })).toThrow();
expect(() => convertRestQueryParams({ _limit: 'NaN' })).toThrow();
expect(() => convertRestQueryParams({ _limit: 'Infinity' })).toThrow();
expect(() => convertRestQueryParams({ _limit: Infinity })).toThrow();
expect(() => convertRestQueryParams({ _limit: -Infinity })).toThrow();
2019-05-29 16:09:19 +02:00
expect(() => convertRestQueryParams({ _limit: NaN })).toThrow();
2019-03-13 19:27:18 +01:00
expect(() => convertRestQueryParams({ _limit: 1.2 })).toThrow();
expect(() => convertRestQueryParams({ _limit: -10 })).toThrow();
expect(() => convertRestQueryParams({ _limit: {} })).toThrow();
});
test.each([['1', 1], ['12', 12], ['0', 0]])(
'Converts start query "%s" correctly',
(input, expected) => {
2019-05-29 16:09:19 +02:00
expect(convertRestQueryParams({ _start: input })).toMatchObject({
start: expected,
});
2019-03-13 19:27:18 +01:00
}
);
});
describe('Populate', () => {
test.todo('Not Eq');
});
describe('Filters', () => {
test('Can combine filters', () => {
2019-05-29 16:09:19 +02:00
expect(
convertRestQueryParams({ id: '1', test_ne: 'text', test_: 'content' })
).toMatchObject({
2019-03-13 19:27:18 +01:00
where: [
{
field: 'id',
operator: 'eq',
value: '1',
},
{
field: 'test',
operator: 'ne',
value: 'text',
},
{
field: 'test_',
operator: 'eq',
value: 'content',
},
],
});
});
test('Ok', () => {
expect(convertRestQueryParams({ id: '1', test: 'text' })).toMatchObject({
where: [
{
field: 'id',
operator: 'eq',
value: '1',
},
{
field: 'test',
operator: 'eq',
value: 'text',
},
],
});
2019-05-29 16:09:19 +02:00
expect(
convertRestQueryParams({ id_eq: '1', test_eq: 'text' })
).toMatchObject({
2019-03-13 19:27:18 +01:00
where: [
{
field: 'id',
operator: 'eq',
value: '1',
},
{
field: 'test',
operator: 'eq',
value: 'text',
},
],
});
2019-05-29 16:09:19 +02:00
expect(
convertRestQueryParams({ published_at: '2019-01-01:00:00:00' })
).toMatchObject({
2019-03-13 19:27:18 +01:00
where: [
{
field: 'published_at',
operator: 'eq',
value: '2019-01-01:00:00:00',
},
],
});
});
test('Not Eq', () => {
expect(convertRestQueryParams({ id_ne: 1 })).toMatchObject({
where: [
{
field: 'id',
operator: 'ne',
value: 1,
},
],
});
});
test('Less than', () => {
expect(convertRestQueryParams({ id_lt: 1 })).toMatchObject({
where: [
{
field: 'id',
operator: 'lt',
value: 1,
},
],
});
});
test('Less than or equal', () => {
expect(convertRestQueryParams({ id_lte: 1 })).toMatchObject({
where: [
{
field: 'id',
operator: 'lte',
value: 1,
},
],
});
});
test('Greater than', () => {
expect(convertRestQueryParams({ id_gt: 1 })).toMatchObject({
where: [
{
field: 'id',
operator: 'gt',
value: 1,
},
],
});
});
test('Greater than or equal', () => {
expect(convertRestQueryParams({ id_gte: 1 })).toMatchObject({
where: [
{
field: 'id',
operator: 'gte',
value: 1,
},
],
});
});
test('In', () => {
expect(convertRestQueryParams({ id_in: [1, 2] })).toMatchObject({
where: [
{
field: 'id',
operator: 'in',
value: [1, 2],
},
],
});
});
test('Not in', () => {
expect(convertRestQueryParams({ id_nin: [1, 3] })).toMatchObject({
where: [
{
field: 'id',
operator: 'nin',
value: [1, 3],
},
],
});
});
test('Contains', () => {
expect(convertRestQueryParams({ id_contains: 'text' })).toMatchObject({
where: [
{
field: 'id',
operator: 'contains',
value: 'text',
},
],
});
});
test('Contains sensitive', () => {
expect(convertRestQueryParams({ id_containss: 'test' })).toMatchObject({
where: [
{
field: 'id',
operator: 'containss',
value: 'test',
},
],
});
});
test('Not Contains', () => {
2019-05-29 16:09:19 +02:00
expect(
convertRestQueryParams({ sub_title_ncontains: 'text' })
).toMatchObject({
2019-03-13 19:27:18 +01:00
where: [
{
field: 'sub_title',
operator: 'ncontains',
value: 'text',
},
],
});
});
test('Not Contains sensitive', () => {
2019-05-29 16:09:19 +02:00
expect(
convertRestQueryParams({ content_text_ncontainss: 'test' })
).toMatchObject({
2019-03-13 19:27:18 +01:00
where: [
{
field: 'content_text',
operator: 'ncontainss',
value: 'test',
},
],
});
});
test('Not Contains sensitive', () => {
2019-05-29 16:09:19 +02:00
expect(
convertRestQueryParams({ 'content.text_ncontainss': 'test' })
).toMatchObject({
2019-03-13 19:27:18 +01:00
where: [
{
field: 'content.text',
operator: 'ncontainss',
value: 'test',
},
],
});
});
2019-07-04 19:10:17 -03:00
test('Null', () => {
expect(
convertRestQueryParams({ 'content.text_null': true })
).toMatchObject({
where: [
{
field: 'content.text',
operator: 'null',
value: true,
},
],
});
});
test('Not Null', () => {
expect(
convertRestQueryParams({ 'content.text_null': false })
).toMatchObject({
where: [
{
field: 'content.text',
operator: 'null',
value: false,
},
],
});
});
2019-03-13 19:27:18 +01:00
});
});