useAssets/ useFolders: Add query merge tests

This commit is contained in:
Gustav Hansen 2022-06-07 15:42:29 +01:00
parent b34cc3cb5d
commit 8dc5c8a90c
4 changed files with 145 additions and 20 deletions

View File

@ -1,4 +1,5 @@
import React from 'react';
import { stringify } from 'qs';
import { IntlProvider } from 'react-intl';
import { QueryClientProvider, QueryClient } from 'react-query';
import { renderHook, act } from '@testing-library/react-hooks';
@ -80,8 +81,22 @@ describe('useAssets', () => {
await waitFor(() => result.current.isSuccess);
const expected = {
filters: {
$and: [
{
folder: {
id: {
$null: true,
},
},
},
],
},
};
expect(axiosInstance.get).toBeCalledWith(
`/upload/files?filters${encodeURIComponent('[folder][id][$null]')}=true`
`/upload/files?${stringify(expected, { encode: false })}`
);
});
@ -90,8 +105,47 @@ describe('useAssets', () => {
await waitFor(() => result.current.isSuccess);
const expected = {
filters: {
$and: [
{
folder: {
id: 1,
},
},
],
},
};
expect(axiosInstance.get).toBeCalledWith(
`/upload/files?filters${encodeURIComponent('[folder][id]')}=1`
`/upload/files?${stringify(expected, { encode: false })}`
);
});
test('allows to merge filter query params using filters.$and', async () => {
const { result, waitFor } = await setup({
query: { folder: 5, filters: { $and: [{ something: 'true' }] } },
});
await waitFor(() => result.current.isSuccess);
const expected = {
filters: {
$and: [
{
something: true,
},
{
folder: {
id: 5,
},
},
],
},
};
expect(axiosInstance.get).toBeCalledWith(
`/upload/files?${stringify(expected, { encode: false })}`
);
});

View File

@ -1,4 +1,5 @@
import React from 'react';
import { stringify } from 'qs';
import { IntlProvider } from 'react-intl';
import { QueryClientProvider, QueryClient } from 'react-query';
import { renderHook, act } from '@testing-library/react-hooks';
@ -80,10 +81,25 @@ describe('useFolders', () => {
await waitFor(() => result.current.isSuccess);
const expected = {
pagination: {
pageSize: -1,
},
filters: {
$and: [
{
parent: {
id: {
$null: true,
},
},
},
],
},
};
expect(axiosInstance.get).toBeCalledWith(
`/upload/folders?pagination${encodeURIComponent('[pageSize]')}=-1&filters${encodeURIComponent(
'[parent][id][$null]'
)}=true`
`/upload/folders?${stringify(expected, { encode: false })}`
);
});
@ -92,10 +108,53 @@ describe('useFolders', () => {
await waitFor(() => result.current.isSuccess);
const expected = {
pagination: {
pageSize: -1,
},
filters: {
$and: [
{
parent: {
id: 1,
},
},
],
},
};
expect(axiosInstance.get).toBeCalledWith(
`/upload/folders?pagination${encodeURIComponent('[pageSize]')}=-1&filters${encodeURIComponent(
'[parent][id]'
)}=1`
`/upload/folders?${stringify(expected, { encode: false })}`
);
});
test('allows to merge filter query params using filters.$and', async () => {
const { result, waitFor } = await setup({
query: { folder: 5, filters: { $and: [{ something: 'true' }] } },
});
await waitFor(() => result.current.isSuccess);
const expected = {
filters: {
$and: [
{
something: true,
},
{
parent: {
id: 5,
},
},
],
},
pagination: {
pageSize: -1,
},
};
expect(axiosInstance.get).toBeCalledWith(
`/upload/folders?${stringify(expected, { encode: false })}`
);
});

View File

@ -16,18 +16,24 @@ export const useAssets = ({ skipWhen = false, query = {} } = {}) => {
const params = {
...paramsExceptFolder,
filters: {
...query?.filters,
folder: {
id: query?.folder ?? {
$null: true,
$and: [
...(query?.filters?.$and ?? []),
{
folder: {
id: query?.folder ?? {
$null: true,
},
},
},
},
],
},
};
const getAssets = async () => {
try {
const { data } = await axiosInstance.get(`${dataRequestURL}?${stringify(params)}`);
const { data } = await axiosInstance.get(
`${dataRequestURL}?${stringify(params, { encode: false })}`
);
notifyStatus(
formatMessage({

View File

@ -19,18 +19,24 @@ export const useFolders = ({ enabled = true, query = {} }) => {
pageSize: -1,
},
filters: {
...query?.filters,
parent: {
id: query?.folder ?? {
$null: true,
$and: [
...(query?.filters?.$and ?? []),
{
parent: {
id: query?.folder ?? {
$null: true,
},
},
},
},
],
},
};
const fetchFolders = async () => {
try {
const { data } = await axiosInstance.get(`${dataRequestURL}?${stringify(params)}`);
const { data } = await axiosInstance.get(
`${dataRequestURL}?${stringify(params, { encode: false })}`
);
notifyStatus(
formatMessage({