mirror of
https://github.com/strapi/strapi.git
synced 2026-01-06 12:13:52 +00:00
useAssets/ useFolders: Add query merge tests
This commit is contained in:
parent
b34cc3cb5d
commit
8dc5c8a90c
@ -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 })}`
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@ -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 })}`
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@ -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({
|
||||
|
||||
@ -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({
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user