Skip to content

Commit

Permalink
refactor: remove success messages on async op, fix ws msg
Browse files Browse the repository at this point in the history
  • Loading branch information
pyphilia committed Nov 1, 2023
1 parent d779bf9 commit f3108a7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 444 deletions.
320 changes: 1 addition & 319 deletions src/mutations/item.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable import/no-extraneous-dependencies */
import {
DiscriminatedItem,
GraaspError,
HttpMethod,
ItemType,
MAX_TARGETS_FOR_MODIFY_REQUEST,
Expand Down Expand Up @@ -45,11 +44,7 @@ import {
buildItemThumbnailKey,
getKeyForParentId,
} from '../config/keys';
import {
deleteItemsRoutine,
uploadFileRoutine,
uploadItemThumbnailRoutine,
} from '../routines';
import { uploadFileRoutine, uploadItemThumbnailRoutine } from '../routines';
import {
buildPath,
getDirectParentId,
Expand Down Expand Up @@ -378,54 +373,6 @@ describe('Items Mutations', () => {
// Check new parent is not invalidated (because copy is async)
expect(queryClient.getQueryState(key)?.isInvalidated).toBeFalsy();
});

it('Unauthorized to copy multiple items', async () => {
const nb = 2;
const copied = ITEMS.slice(0, nb);
const copiedIds = copied.map(({ id }) => id);
const route = `/${buildCopyItemsRoute(copiedIds)}`;
// set data in cache
copied.forEach((item) => {
const itemKey = buildItemKey(item.id);
queryClient.setQueryData(itemKey, item);
});
queryClient.setQueryData(key, [copied[1]]);

const response = UNAUTHORIZED_RESPONSE;

const endpoints = [
{
response,
statusCode: StatusCodes.UNAUTHORIZED,
method: HttpMethod.POST,
route,
},
];

const mockedMutation = await mockMutation({
endpoints,
mutation,
wrapper,
});

await act(async () => {
await mockedMutation.mutate({
to,
ids: copiedIds,
});
await waitForMutation();
});

// original copied items path have not changed
copied.forEach((item) => {
const itemKey = buildItemKey(item.id);
const path = queryClient.getQueryData<DiscriminatedItem>(itemKey)?.path;
expect(path).toEqual(item.path);
});

// check new parent is not invalidated because copy is async
expect(queryClient.getQueryState(key)?.isInvalidated).toBeFalsy();
});
});

describe('useMoveItems', () => {
Expand Down Expand Up @@ -538,67 +485,6 @@ describe('Items Mutations', () => {
const fromItemKey = getKeyForParentId(null);
expect(queryClient.getQueryState(fromItemKey)?.isInvalidated).toBeFalsy();
});

describe('Error handling', () => {
const moved = ITEMS.slice(0, 2);
const movedIds = moved.map((x) => x.id);
const route = `/${buildMoveItemsRoute(movedIds)}`;

it('Unauthorized to move multiple items', async () => {
// set data in cache
moved.forEach((item) => {
const itemKey = buildItemKey(item.id);
queryClient.setQueryData(itemKey, item);
});
queryClient.setQueryData(getKeyForParentId(null), moved);
const toItemKey = getKeyForParentId(toId);
queryClient.setQueryData(toItemKey, ITEMS);

const response = UNAUTHORIZED_RESPONSE;

const endpoints = [
{
response,
statusCode: StatusCodes.UNAUTHORIZED,
method: HttpMethod.POST,
route,
},
];

const mockedMutation = await mockMutation({
endpoints,
mutation,
wrapper,
});

await act(async () => {
await mockedMutation.mutate({
to: toId,
ids: movedIds,
});
await waitForMutation();
});

// items path have not changed
moved.forEach((item) => {
const itemKey = buildItemKey(item.id);
const path =
queryClient.getQueryData<DiscriminatedItem>(itemKey)?.path;
expect(path).toEqual(item.path);
});

// Check new parent is correctly invalidated
expect(
queryClient.getQueryState(toItemKey)?.isInvalidated,
).toBeTruthy();

// Check old parent is correctly invalidated
const fromItemKey = getKeyForParentId(null);
expect(
queryClient.getQueryState(fromItemKey)?.isInvalidated,
).toBeTruthy();
});
});
});

describe('useRecycleItems', () => {
Expand Down Expand Up @@ -710,116 +596,6 @@ describe('Items Mutations', () => {
?.filter(({ id }) => itemIds.includes(id)).length,
).toBeFalsy();
});

it('Unauthorized to recycle one of the items', async () => {
const items = ITEMS.slice(2);
const itemIds = items.map(({ id }) => id);
const route = `/${buildRecycleItemsRoute(itemIds)}`;

ITEMS.forEach((item) => {
const itemKey = buildItemKey(item.id);
queryClient.setQueryData(itemKey, item);
});
const childrenKey = getKeyForParentId(null);
queryClient.setQueryData(childrenKey, ITEMS);

const response: (DiscriminatedItem | GraaspError)[] = [
UNAUTHORIZED_RESPONSE,
...items,
];

const endpoints = [
{
response,
method: HttpMethod.POST,
route,
},
];

const mockedMutation = await mockMutation({
endpoints,
mutation,
wrapper,
});

await act(async () => {
await mockedMutation.mutate(itemIds);
await waitForMutation();
});

// verify item is still available
// in real cases, the path should be different
for (const itemId of itemIds) {
const itemKey = buildItemKey(itemId);
const data = queryClient.getQueryData<DiscriminatedItem>(itemKey);
expect(data).toMatchObject(items.find(({ id }) => id === itemId)!);
}

// Check parent's children key is correctly invalidated
// and still contains the items
expect(
queryClient
.getQueryData<DiscriminatedItem[]>(childrenKey)
?.filter(({ id }) => itemIds.includes(id)).length,
).toBeTruthy();
expect(
queryClient.getQueryState(childrenKey)?.isInvalidated,
).toBeTruthy();
});

it('Unauthorized to recycle items', async () => {
const items = ITEMS.slice(2);
const itemIds = items.map(({ id }) => id);
const route = `/${buildRecycleItemsRoute(itemIds)}`;

ITEMS.forEach((item) => {
const itemKey = buildItemKey(item.id);
queryClient.setQueryData(itemKey, item);
});
const childrenKey = getKeyForParentId(null);
queryClient.setQueryData(childrenKey, ITEMS);

const response = UNAUTHORIZED_RESPONSE;

const endpoints = [
{
response,
statusCode: StatusCodes.UNAUTHORIZED,
method: HttpMethod.POST,
route,
},
];

const mockedMutation = await mockMutation({
endpoints,
mutation,
wrapper,
});

await act(async () => {
await mockedMutation.mutate(itemIds);
await waitForMutation();
});

// verify item is still available
// in real cases, the path should be different
for (const itemId of itemIds) {
const itemKey = buildItemKey(itemId);
const data = queryClient.getQueryData<DiscriminatedItem>(itemKey);
expect(data).toMatchObject(items.find(({ id }) => id === itemId)!);
}

// Check parent's children key is correctly invalidated
// and still contains the items
expect(
queryClient
.getQueryData<DiscriminatedItem[]>(childrenKey)
?.filter(({ id }) => itemIds.includes(id)).length,
).toBeTruthy();
expect(
queryClient.getQueryState(childrenKey)?.isInvalidated,
).toBeTruthy();
});
});

describe('useDeleteItems', () => {
Expand Down Expand Up @@ -913,53 +689,6 @@ describe('Items Mutations', () => {
expect(state?.isInvalidated).toBeFalsy();
}
});

it('Unauthorized to delete an item', async () => {
const items = ITEMS.slice(2);
const itemIds = items.map(({ id }) => id);
const route = `/${buildDeleteItemsRoute(itemIds)}`;

ITEMS.forEach((item) => {
const itemKey = buildItemKey(item.id);
queryClient.setQueryData(itemKey, item);
});

const response = UNAUTHORIZED_RESPONSE;

const endpoints = [
{
response,
statusCode: StatusCodes.UNAUTHORIZED,
method: HttpMethod.DELETE,
route,
},
];

const mockedMutation = await mockMutation({
endpoints,
mutation,
wrapper,
});

await act(async () => {
await mockedMutation.mutate(itemIds);
await waitForMutation();
});

// verify item is still available
// in real cases, the path should be different
for (const itemId of itemIds) {
const itemKey = buildItemKey(itemId);
const data = queryClient.getQueryData<DiscriminatedItem>(itemKey);
expect(data).toMatchObject(items.find(({ id }) => id === itemId)!);
}

// check notification trigger
expect(mockedNotifier).toHaveBeenCalledWith({
type: deleteItemsRoutine.FAILURE,
payload: expect.anything(),
});
});
});

describe('useUploadFiles', () => {
Expand Down Expand Up @@ -1145,53 +874,6 @@ describe('Items Mutations', () => {
.length,
).toEqual(0);
});

it('Unauthorized to restore items', async () => {
const items = ITEMS.slice(2);
const itemIds = items.map(({ id }) => id);
const route = `/${buildRestoreItemsRoute(itemIds)}`;

ITEMS.forEach((item) => {
const itemKey = buildItemKey(item.id);
queryClient.setQueryData(itemKey, item);
});
queryClient.setQueryData(RECYCLED_ITEMS_DATA_KEY, RECYCLED_ITEM_DATA);
expect(
queryClient.getQueryData<RecycledItemData[]>(RECYCLED_ITEMS_DATA_KEY)!
.length,
).not.toEqual(0);

const endpoints = [
{
response: UNAUTHORIZED_RESPONSE,
statusCode: StatusCodes.UNAUTHORIZED,
method: HttpMethod.POST,
route,
},
];

const mockedMutation = await mockMutation({
endpoints,
mutation,
wrapper,
});

await act(async () => {
await mockedMutation.mutate(itemIds);
await waitForMutation();
});

// verify item is still available
// in real cases, the path should be different
for (const item of items) {
const itemKey = buildItemKey(item.id);
const data = queryClient.getQueryData<DiscriminatedItem>(itemKey);
expect(data).toMatchObject(item);
}
expect(
queryClient.getQueryState(RECYCLED_ITEMS_DATA_KEY)?.isInvalidated,
).toBeTruthy();
});
});

describe('useUploadItemThumbnail', () => {
Expand Down
Loading

0 comments on commit f3108a7

Please sign in to comment.