Skip to content

Commit

Permalink
fix: convert date string in websocket data (#430)
Browse files Browse the repository at this point in the history
fix: remove immutable matchers
fix: use generics for mockedMutations and mockedHook
fix: build and tsconfig for tests
fix(refactor): useless casts
  • Loading branch information
spaenleh authored Sep 22, 2023
1 parent 72953a3 commit c6517df
Show file tree
Hide file tree
Showing 52 changed files with 592 additions and 477 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
"@typescript-eslint/no-shadow": "error",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"warn",
"error",
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_",
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default {
// ],

// An array of file extensions your modules use
moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx', 'json', 'node'],
moduleFileExtensions: ['cjs', 'js', 'jsx', 'ts', 'tsx', 'json', 'node'],

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
moduleNameMapper: {
Expand Down Expand Up @@ -102,7 +102,7 @@ export default {
// resetModules: false,

// A path to a custom resolver
resolver: `${__dirname}/test/resolver.js`,
resolver: `${__dirname}/test/resolver.cjs`,

// Automatically restore mock state between every test
// restoreMocks: false,
Expand All @@ -122,7 +122,7 @@ export default {
setupFiles: ['<rootDir>/test/setupTests.ts'],

// A list of paths to modules that run some code to configure or set up the testing framework before each test
setupFilesAfterEnv: ['<rootDir>/test/setupTestsAfterEnv.ts'],
// setupFilesAfterEnv: ['<rootDir>/test/setupTestsAfterEnv.ts'],

// The number of seconds after which a test is considered as slow and reported as such in the results.
// slowTestThreshold: 5,
Expand Down Expand Up @@ -167,7 +167,7 @@ export default {
'^.+\\.(ts|tsx)?$': [
'ts-jest',
{
tsconfig: 'tsconfig.json',
tsconfig: 'tsconfig.test.json',
},
],
},
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "@graasp/query-client",
"version": "1.8.0",
"type": "module",
"workspaces": [
"example"
],
"repository": "graasp/graasp-query-client",
"main": "dist/index.js",
"module": "dist/index.modern.js",
"source": "src/index.ts",
"types": "dist/index.d.ts",
"main": "./dist/index.js",
"module": "./dist/index.modern.js",
"source": "./src/index.ts",
"types": "./dist/index.d.ts",
"author": "Graasp",
"contributors": [
"Kim Lan Phan Hoang",
Expand Down Expand Up @@ -65,7 +66,6 @@
"husky": "8.0.3",
"jest": "29.6.1",
"jest-environment-jsdom": "29.6.1",
"jest-immutable-matchers": "3.0.0",
"js-cookie": "3.0.5",
"microbundle-crl": "0.13.11",
"mock-socket": "9.2.1",
Expand Down
4 changes: 3 additions & 1 deletion src/api/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export const getAggregateActions = async (
{ API_HOST }: QueryClientConfig,
) =>
axios
.get(`${API_HOST}/${buildGetAggregateActions(args)}`)
.get<{ aggregateResult: number; createdDay: string }[]>(
`${API_HOST}/${buildGetAggregateActions(args)}`,
)
.then(({ data }) => data);

export const exportActions = async (
Expand Down
2 changes: 1 addition & 1 deletion src/api/itemTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const postItemTag = async (
);

export const deleteItemTag = async (
{ itemId, type }: { itemId: UUID; type: ItemTagType },
{ itemId, type }: { itemId: UUID; type: `${ItemTagType}` | ItemTagType },
{ API_HOST }: QueryClientConfig,
) =>
verifyAuthentication(() =>
Expand Down
30 changes: 18 additions & 12 deletions src/hooks/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
AggregateMetric,
Context,
CountGroupBy,
convertJs,
} from '@graasp/sdk';
import { ActionDataRecord, ImmutableCast } from '@graasp/sdk/frontend';

import { StatusCodes } from 'http-status-codes';
import Cookies from 'js-cookie';
Expand All @@ -22,6 +22,10 @@ import { mockHook, setUpTest } from '../../test/utils';
import { buildGetActions, buildGetAggregateActions } from '../api/routes';
import { buildActionsKey, buildAggregateActionsKey } from '../config/keys';

type AggregateActionsResponse = ImmutableCast<
{ aggregateResult: number; createdDay: string }[]
>;

const { hooks, wrapper, queryClient } = setUpTest();
const itemId = ITEMS.first()!.id;

Expand All @@ -44,12 +48,14 @@ describe('Action Hooks', () => {

it(`Receive actions for item id`, async () => {
const hook = () => hooks.useActions(args);
const endpoints = [{ route, response: response.toJS() }];
const endpoints = [{ route, response }];
const { data } = await mockHook({ endpoints, hook, wrapper });
expect(data).toEqualImmutable(response);
expect(data?.toJS()).toEqual(response);

// verify cache keys
expect(queryClient.getQueryData(key)).toEqualImmutable(response);
expect(queryClient.getQueryData<ActionDataRecord>(key)?.toJS()).toEqual(
response,
);
});

it(`Sample size = 0 does not fetch`, async () => {
Expand Down Expand Up @@ -124,23 +130,23 @@ describe('Action Hooks', () => {
const hook = () => hooks.useAggregateActions(args);
const endpoints = [{ route, response }];
const { data } = await mockHook({ endpoints, hook, wrapper });
expect(data).toEqualImmutable(convertJs(response));
expect(data?.toJS()).toEqual(response);

// verify cache keys
expect(queryClient.getQueryData(key)).toEqualImmutable(
convertJs(response),
);
expect(
queryClient.getQueryData<AggregateActionsResponse>(key)?.toJS(),
).toEqual(response);
});
it(`Receive aggregate actions for item id`, async () => {
const hook = () => hooks.useAggregateActions(args);
const endpoints = [{ route, response }];
const { data } = await mockHook({ endpoints, hook, wrapper });
expect(data).toEqualImmutable(convertJs(response));
expect(data?.toJS()).toEqual(response);

// verify cache keys
expect(queryClient.getQueryData(key)).toEqualImmutable(
convertJs(response),
);
expect(
queryClient.getQueryData<AggregateActionsResponse>(key)?.toJS(),
).toEqual(response);
});

it(`Unauthorized`, async () => {
Expand Down
10 changes: 6 additions & 4 deletions src/hooks/action.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UUID, convertJs } from '@graasp/sdk';
import { ActionDataRecord } from '@graasp/sdk/frontend';
import { ActionDataRecord, ImmutableCast } from '@graasp/sdk/frontend';

import { useQuery } from 'react-query';

Expand All @@ -26,7 +26,7 @@ export default (queryConfig: QueryClientConfig) => {
Boolean(args.requestedSampleSize);
return useQuery({
queryKey: buildActionsKey(args),
queryFn: () => {
queryFn: (): Promise<ActionDataRecord> => {
const { itemId } = args;
if (!itemId) {
throw new UndefinedArgument();
Expand All @@ -38,7 +38,7 @@ export default (queryConfig: QueryClientConfig) => {
requestedSampleSize: args.requestedSampleSize,
},
queryConfig,
).then((data) => convertJs(data) as ActionDataRecord);
).then((data) => convertJs(data));
},
...defaultQueryOptions,
enabled: enabledValue,
Expand All @@ -56,7 +56,9 @@ export default (queryConfig: QueryClientConfig) => {
Boolean(args.requestedSampleSize);
return useQuery({
queryKey: buildAggregateActionsKey(args),
queryFn: () => {
queryFn: (): Promise<
ImmutableCast<{ aggregateResult: number; createdDay: string }[]>
> => {
const { itemId } = args;
if (!itemId) {
throw new UndefinedArgument();
Expand Down
6 changes: 4 additions & 2 deletions src/hooks/apps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ describe('Apps Hooks', () => {
const endpoints = [{ route, response }];
const { data } = await mockHook({ endpoints, hook, wrapper });

expect(data as List<AppRecord>).toEqualImmutable(response);
expect(data?.toJS()).toEqual(response);

// verify cache keys
expect(queryClient.getQueryData(key)).toEqualImmutable(response);
expect(queryClient.getQueryData<List<AppRecord>>(key)?.toJS()).toEqual(
response,
);
});

it(`Unauthorized`, async () => {
Expand Down
30 changes: 16 additions & 14 deletions src/hooks/category.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* eslint-disable import/no-extraneous-dependencies */
import { CategoryRecord, ItemCategoryRecord } from '@graasp/sdk/frontend';

import { StatusCodes } from 'http-status-codes';
import { List, Record, RecordOf } from 'immutable';
import Immutable, { List, Record, RecordOf } from 'immutable';
import Cookies from 'js-cookie';
import nock from 'nock';

Expand Down Expand Up @@ -52,7 +50,7 @@ describe('Category Hooks', () => {
// const endpoints = [{ route, response }];
// const { data } = await mockHook({ endpoints, hook, wrapper });

// expect(data as List<CategoryTypeRecord>).toEqualImmutable(response);
// expect(data).toEqualImmutable(response);

// // verify cache keys
// expect(queryClient.getQueryData(key)).toEqualImmutable(response);
Expand Down Expand Up @@ -90,10 +88,12 @@ describe('Category Hooks', () => {
const endpoints = [{ route, response }];
const { data } = await mockHook({ endpoints, hook, wrapper });

expect(data as List<CategoryRecord>).toEqualImmutable(response);
expect(Immutable.is(data, response)).toBeTruthy();

// verify cache keys
expect(queryClient.getQueryData(key)).toEqualImmutable(response);
expect(
Immutable.is(queryClient.getQueryData(key), response),
).toBeTruthy();
});
it(`Unauthorized`, async () => {
const endpoints = [
Expand Down Expand Up @@ -128,10 +128,12 @@ describe('Category Hooks', () => {
const endpoints = [{ route, response }];
const { data } = await mockHook({ endpoints, hook, wrapper });

expect(data as CategoryRecord).toEqualImmutable(response);
expect(Immutable.is(data, response)).toBeTruthy();

// verify cache keys
expect(queryClient.getQueryData(key)).toEqualImmutable(response);
expect(
Immutable.is(queryClient.getQueryData(key), response),
).toBeTruthy();
});
it(`Unauthorized`, async () => {
const endpoints = [
Expand Down Expand Up @@ -166,12 +168,12 @@ describe('Category Hooks', () => {
const endpoints = [{ route, response }];
const { data } = await mockHook({ endpoints, hook, wrapper });

expect(data as List<ItemCategoryRecord>).toEqualImmutable(response);
expect(Immutable.is(data, response)).toBeTruthy();

// verify cache keys
expect(
queryClient.getQueryData(key) as List<ItemCategoryRecord>,
).toEqualImmutable(response);
Immutable.is(queryClient.getQueryData(key), response),
).toBeTruthy();
});

it(`Unauthorized`, async () => {
Expand Down Expand Up @@ -212,12 +214,12 @@ describe('Category Hooks', () => {
const endpoints = [{ route, response }];
const { data } = await mockHook({ endpoints, hook, wrapper });

expect(data as List<ItemIdRecord>).toEqualImmutable(response);
expect(Immutable.is(data, response)).toBeTruthy();

// verify cache keys
expect(
queryClient.getQueryData(key) as List<ItemIdRecord>,
).toEqualImmutable(response);
Immutable.is(queryClient.getQueryData(key), response),
).toBeTruthy();
});
it(`Unauthorized`, async () => {
const endpoints = [
Expand Down
25 changes: 17 additions & 8 deletions src/hooks/chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ChatMessage } from '@graasp/sdk';
import { ExportedItemChatRecord, ItemChatRecord } from '@graasp/sdk/frontend';

import { StatusCodes } from 'http-status-codes';
import Immutable from 'immutable';
import Cookies from 'js-cookie';
import nock from 'nock';

Expand Down Expand Up @@ -57,10 +58,12 @@ describe('Chat Hooks', () => {
wrapper,
});

expect(data as ItemChatRecord).toEqualImmutable(response);
expect(Immutable.is(data, response)).toBeTruthy();

// verify cache keys
expect(queryClient.getQueryData(key)).toEqualImmutable(response);
expect(
Immutable.is(queryClient.getQueryData(key), response),
).toBeTruthy();
});

it(`Unauthorized`, async () => {
Expand Down Expand Up @@ -113,10 +116,12 @@ describe('Chat Hooks', () => {
wrapper,
});

expect(data).toEqualImmutable(response);
expect(Immutable.is(data, response)).toBeTruthy();

// verify cache keys
expect(queryClient.getQueryData(key)).toEqualImmutable(response);
expect(
Immutable.is(queryClient.getQueryData(key), response),
).toBeTruthy();
});

it(`getUpdates = false`, async () => {
Expand All @@ -134,10 +139,12 @@ describe('Chat Hooks', () => {
wrapper,
});

expect(data).toEqualImmutable(response);
expect(Immutable.is(data, response)).toBeTruthy();

// verify cache keys
expect(queryClient.getQueryData(key)).toEqualImmutable(response);
expect(
Immutable.is(queryClient.getQueryData(key), response),
).toBeTruthy();
});
});

Expand Down Expand Up @@ -175,10 +182,12 @@ describe('Chat Hooks', () => {
wrapper,
});

expect(data as ExportedItemChatRecord).toEqualImmutable(response);
expect(Immutable.is(data, response)).toBeTruthy();

// verify cache keys
expect(queryClient.getQueryData(key)).toEqualImmutable(response);
expect(
Immutable.is(queryClient.getQueryData(key), response),
).toBeTruthy();
});

it(`Unauthorized`, async () => {
Expand Down
Loading

0 comments on commit c6517df

Please sign in to comment.