Skip to content

Commit

Permalink
refactor: fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
gao-sun committed Mar 29, 2024
1 parent df54dcb commit e7c503d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
30 changes: 26 additions & 4 deletions packages/integration-tests/src/api/factory.ts
@@ -1,5 +1,23 @@
import { authedAdminApi } from './api.js';

/**
* Transform the data to a new object or array without modifying the original data.
* This is useful since `.json()` returns an object that contains something which makes
* it impossible to use `.toStrictEqual()` directly.
*/
const transform = <T>(data: T): T => {
if (Array.isArray(data)) {
// eslint-disable-next-line no-restricted-syntax
return [...data] as T;
}

if (typeof data === 'object') {
return { ...data };
}

return data;
};

export class ApiFactory<
Schema extends Record<string, unknown>,
PostData extends Record<string, unknown>,
Expand All @@ -8,19 +26,23 @@ export class ApiFactory<
constructor(public readonly path: string) {}

async create(data: PostData): Promise<Schema> {
return authedAdminApi.post(this.path, { json: data }).json<Schema>();
return transform(await authedAdminApi.post(this.path, { json: data }).json<Schema>());
}

async getList(params?: URLSearchParams): Promise<Schema[]> {
return authedAdminApi.get(this.path + '?' + (params?.toString() ?? '')).json<Schema[]>();
return transform(
await authedAdminApi.get(this.path + '?' + (params?.toString() ?? '')).json<Schema[]>()
);
}

async get(id: string): Promise<Schema> {
return authedAdminApi.get(this.path + '/' + id).json<Schema>();
return transform(await authedAdminApi.get(this.path + '/' + id).json<Schema>());
}

async update(id: string, data: PatchData): Promise<Schema> {
return authedAdminApi.patch(this.path + '/' + id, { json: data }).json<Schema>();
return transform(
await authedAdminApi.patch(this.path + '/' + id, { json: data }).json<Schema>()
);
}

async delete(id: string): Promise<void> {
Expand Down
Expand Up @@ -120,7 +120,7 @@ describe('admin console application', () => {

expect(updatedApplication.description).toBe(newApplicationDescription);
expect(updatedApplication.oidcClientMetadata.redirectUris).toEqual(newRedirectUris);
expect(updatedApplication.customClientMetadata).toStrictEqual({
expect({ ...updatedApplication.customClientMetadata }).toStrictEqual({
rotateRefreshToken: true,
refreshTokenTtlInDays: 10,
});
Expand Down
Expand Up @@ -25,9 +25,8 @@ describe('organization role APIs', () => {

assert(response instanceof HTTPError);

const { status, body: raw } = response.response;
const body: unknown = JSON.parse(String(raw));
expect(status).toBe(422);
const body: unknown = await response.response.json();
expect(response.response.status).toBe(422);
expect(isKeyInObject(body, 'code') && body.code).toBe('entity.unique_integrity_violation');
});

Expand Down Expand Up @@ -127,7 +126,7 @@ describe('organization role APIs', () => {

assert(response instanceof HTTPError);
expect(response.response.status).toBe(422);
expect(JSON.parse(String(response.response.body))).toMatchObject(
expect(await response.response.json()).toMatchObject(
expect.objectContaining({
code: 'entity.unique_integrity_violation',
})
Expand Down Expand Up @@ -213,7 +212,7 @@ describe('organization role APIs', () => {

assert(response instanceof HTTPError);
expect(response.response.status).toBe(422);
expect(JSON.parse(String(response.response.body))).toMatchObject(
expect(await response.response.json()).toMatchObject(
expect.objectContaining({
code: 'entity.relation_foreign_key_not_found',
})
Expand Down
Expand Up @@ -22,9 +22,8 @@ describe('organization scope APIs', () => {

assert(response instanceof HTTPError);

const { status, body: raw } = response.response;
const body: unknown = JSON.parse(String(raw));
expect(status).toBe(422);
const body: unknown = await response.response.json();
expect(response.response.status).toBe(422);
expect(isKeyInObject(body, 'code') && body.code).toBe('entity.unique_integrity_violation');
});

Expand Down Expand Up @@ -100,7 +99,7 @@ describe('organization scope APIs', () => {

assert(response instanceof HTTPError);
expect(response.response.status).toBe(422);
expect(JSON.parse(String(response.response.body))).toMatchObject(
expect(await response.response.json()).toMatchObject(
expect.objectContaining({
code: 'entity.unique_integrity_violation',
})
Expand Down
Expand Up @@ -102,7 +102,7 @@ describe('organization user APIs', () => {
const response = await organizationApi.addUsers('0', ['0']).catch((error: unknown) => error);
assert(response instanceof HTTPError);
expect(response.response.status).toBe(422);
expect(JSON.parse(String(response.response.body))).toMatchObject(
expect(await response.response.json()).toMatchObject(
expect.objectContaining({ code: 'entity.relation_foreign_key_not_found' })
);
});
Expand Down Expand Up @@ -147,7 +147,7 @@ describe('organization user APIs', () => {

assert(response instanceof HTTPError);
expect(response.response.status).toBe(422);
expect(JSON.parse(String(response.response.body))).toMatchObject(
expect(await response.response.json()).toMatchObject(
expect.objectContaining({ code: 'organization.require_membership' })
);

Expand Down

0 comments on commit e7c503d

Please sign in to comment.