Skip to content

Commit 16fa4da

Browse files
committed
fix: make more tests pass
1 parent a4736fa commit 16fa4da

38 files changed

Lines changed: 505 additions & 454 deletions

src/services/auth/plugins/magicLink/magicLink.controller.register.test.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ import waitForExpect from 'wait-for-expect';
66

77
import type { FastifyInstance } from 'fastify';
88

9-
import { HttpMethod, MAX_USERNAME_LENGTH, MemberFactory, RecaptchaAction } from '@graasp/sdk';
9+
import {
10+
AccountType,
11+
DEFAULT_LANG,
12+
HttpMethod,
13+
MAX_USERNAME_LENGTH,
14+
MemberFactory,
15+
RecaptchaAction,
16+
} from '@graasp/sdk';
1017

1118
import build, { MOCK_CAPTCHA, clearDatabase, unmockAuthenticate } from '../../../../../test/app';
1219
import { seedFromJson } from '../../../../../test/mocks/seed';
@@ -17,7 +24,6 @@ import { accountsTable, invitationsTable, itemMembershipsTable } from '../../../
1724
import type { MemberRaw } from '../../../../drizzle/types';
1825
import { MailerService } from '../../../../plugins/mailer/mailer.service';
1926
import { assertIsDefined } from '../../../../utils/assertions';
20-
import { expectMember } from '../../../member/test/fixtures/members';
2127

2228
jest.mock('node-fetch');
2329
(fetch as jest.MockedFunction<typeof fetch>).mockImplementation(async () => {
@@ -31,6 +37,19 @@ const getMemberByEmail = async (lowercaseEmail: string) => {
3137
})) as MemberRaw | undefined;
3238
};
3339

40+
const expectMember = (
41+
m: MemberRaw | undefined | null,
42+
expectation: Partial<Pick<MemberRaw, 'type' | 'extra'>> & Pick<MemberRaw, 'name' | 'email'>,
43+
) => {
44+
if (!m) {
45+
throw 'member does not exist';
46+
}
47+
expect(m.name).toEqual(expectation.name);
48+
expect(m.email).toEqual(expectation.email);
49+
expect(m.type).toEqual(expectation.type ?? AccountType.Individual);
50+
expect(m.extra).toEqual(expectation.extra ?? { lang: DEFAULT_LANG });
51+
};
52+
3453
describe('POST /register', () => {
3554
let app: FastifyInstance;
3655
let mailerService: MailerService;
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe } from 'node:test';
2+
import { expect, it, vi } from 'vitest';
23

34
import { ItemVisibilityType } from '@graasp/sdk';
45

@@ -63,9 +64,9 @@ describe('ItemWrapperService', () => {
6364
assertIsMemberForTest(actor);
6465

6566
const itemVisibilityRepository = {
66-
getForManyItems: jest.fn(),
67+
getForManyItems: vi.fn(),
6768
} as unknown as ItemVisibilityRepository;
68-
jest.spyOn(itemVisibilityRepository, 'getForManyItems').mockImplementation(async () => ({
69+
vi.spyOn(itemVisibilityRepository, 'getForManyItems').mockImplementation(async () => ({
6970
data: {
7071
[items[0].id]: [itemVisibilities[0]],
7172
[items[1].id]: [itemVisibilities[1]],
@@ -78,14 +79,14 @@ describe('ItemWrapperService', () => {
7879
errors: [],
7980
} as any;
8081
const itemMembershipRepository = {
81-
getForManyItems: jest.fn(),
82+
getForManyItems: vi.fn(),
8283
} as unknown as ItemMembershipRepository;
83-
jest
84-
.spyOn(itemMembershipRepository, 'getForManyItems')
85-
.mockImplementation(async () => resultOfMemberships);
84+
vi.spyOn(itemMembershipRepository, 'getForManyItems').mockImplementation(
85+
async () => resultOfMemberships,
86+
);
8687

87-
const itemThumbnailService = { getUrlsByItems: jest.fn() } as unknown as ItemThumbnailService;
88-
jest.spyOn(itemThumbnailService, 'getUrlsByItems').mockImplementation(async () => ({}));
88+
const itemThumbnailService = { getUrlsByItems: vi.fn() } as unknown as ItemThumbnailService;
89+
vi.spyOn(itemThumbnailService, 'getUrlsByItems').mockImplementation(async () => ({}));
8990

9091
const packedItems = await new ItemWrapperService(
9192
itemVisibilityRepository,

src/services/item/item.controller.s3.regression.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe('Item routes tests', () => {
8080

8181
const response = await app.inject({
8282
method: HttpMethod.Get,
83-
url: `/items/${childOfChild.id}/parents`,
83+
url: `/api/items/${childOfChild.id}/parents`,
8484
});
8585

8686
expect(response.json()[1]).not.toHaveProperty('thumbnails');

src/services/item/item.controller.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('Item controller', () => {
4040
mockAuthenticate(actor);
4141
const response = await app.inject({
4242
method: 'GET',
43-
url: `/items/${rootUUID}/descendants`,
43+
url: `/api/items/${rootUUID}/descendants`,
4444
});
4545
expect(response.statusCode).toBe(StatusCodes.OK);
4646
const json = response.json();
@@ -71,7 +71,7 @@ describe('Item controller', () => {
7171
mockAuthenticate(actor);
7272
const response = await app.inject({
7373
method: 'GET',
74-
url: `/items/${rootUUID}/descendants?types=${ItemType.FOLDER}`,
74+
url: `/api/items/${rootUUID}/descendants?types=${ItemType.FOLDER}`,
7575
});
7676
expect(response.statusCode).toBe(StatusCodes.OK);
7777
const json = response.json();
@@ -100,7 +100,7 @@ describe('Item controller', () => {
100100
mockAuthenticate(actor);
101101
const response = await app.inject({
102102
method: 'GET',
103-
url: `/items/${rootUUID}/descendants?types=${ItemType.APP}`,
103+
url: `/api/items/${rootUUID}/descendants?types=${ItemType.APP}`,
104104
});
105105
expect(response.statusCode).toBe(StatusCodes.OK);
106106
const json = response.json();
@@ -126,7 +126,7 @@ describe('Item controller', () => {
126126
mockAuthenticate(actor);
127127
const response = await app.inject({
128128
method: 'GET',
129-
url: `/items/${rootUUID}/descendants?showHidden=false`,
129+
url: `/api/items/${rootUUID}/descendants?showHidden=false`,
130130
});
131131
expect(response.statusCode).toBe(StatusCodes.OK);
132132
const json = response.json();
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { faker } from '@faker-js/faker/locale/en';
44
import { eq, inArray } from 'drizzle-orm/sql';
55
import { v4 } from 'uuid';
6+
import { describe, expect, it, vi } from 'vitest';
67

78
import {
89
ItemType,
@@ -31,7 +32,7 @@ import { MemberDTO } from '../member/types';
3132
import { DEFAULT_ORDER } from './constants';
3233
import type { FolderItem } from './discrimination';
3334
import { ItemRepository } from './item.repository';
34-
import { expectItem, expectManyItems } from './test/fixtures/items';
35+
import { expectItem, expectManyItems } from './test/fixtures/items.vitest';
3536

3637
const alphabeticalOrder = (a: string, b: string) => a.localeCompare(b);
3738

@@ -1303,7 +1304,7 @@ describe('Item Repository', () => {
13031304
// } = await seedFromJson({
13041305
// items: [{}],
13051306
// });
1306-
// // const updateFn = jest.spyOn(itemRawRepository, 'update');
1307+
// // const updateFn = vi.spyOn(itemRawRepository, 'update');
13071308
// await itemRepository.rescaleOrder(db, actor, parentItem);
13081309
// // expect(updateFn).not.toHaveBeenCalled();
13091310
// });
@@ -1398,7 +1399,7 @@ describe('Item Repository', () => {
13981399
let updateMock;
13991400
try {
14001401
await db.transaction(async (tx) => {
1401-
updateMock = jest.spyOn(tx, 'update').mockImplementationOnce(() => {
1402+
updateMock = vi.spyOn(tx, 'update').mockImplementationOnce(() => {
14021403
throw new Error('mock update error');
14031404
});
14041405

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// TODO
22
// import { Readable } from 'node:stream';
3+
import { expect, it } from 'vitest';
34

45
// import { ItemGeolocation, ItemType, ItemVisibilityType } from '@graasp/sdk';
56

src/services/item/plugins/app/appItem.controller.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ describe('App Item tests', () => {
141141

142142
const response = await app.inject({
143143
method: HttpMethod.Patch,
144-
url: `/items/apps/${item.id}`,
144+
url: `/api/items/apps/${item.id}`,
145145
payload: { name: 'new name' },
146146
});
147147

@@ -174,7 +174,7 @@ describe('App Item tests', () => {
174174

175175
const response = await app.inject({
176176
method: HttpMethod.Patch,
177-
url: `/items/apps/${item.id}`,
177+
url: `/api/items/apps/${item.id}`,
178178
payload,
179179
});
180180

src/services/item/plugins/recycled/recycled.controller.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ describe('Recycle Bin Tests', () => {
240240

241241
const response = await app.inject({
242242
method: HttpMethod.Post,
243-
url: `/items/recycle?id=${item.id}`,
243+
url: `/api/items/recycle?id=${item.id}`,
244244
});
245245

246246
expect(response.statusCode).toBe(StatusCodes.UNAUTHORIZED);
@@ -551,7 +551,7 @@ describe('Recycle Bin Tests', () => {
551551

552552
const recycle = await app.inject({
553553
method: HttpMethod.Post,
554-
url: `/items/recycle?id=${parentItem.id}`,
554+
url: `/api/items/recycle?id=${parentItem.id}`,
555555
});
556556
expect(recycle.statusCode).toBe(StatusCodes.ACCEPTED);
557557

@@ -570,7 +570,7 @@ describe('Recycle Bin Tests', () => {
570570

571571
const restore = await app.inject({
572572
method: HttpMethod.Post,
573-
url: `/items/restore?id=${parentItem.id}`,
573+
url: `/api/items/restore?id=${parentItem.id}`,
574574
});
575575
expect(restore.statusCode).toBe(StatusCodes.ACCEPTED);
576576

src/services/item/plugins/recycled/recycled.repository.test.ts renamed to src/services/item/plugins/recycled/recycled.repository.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { describe, expect, it } from 'vitest';
2+
13
import { PermissionLevel } from '@graasp/sdk';
24

35
import { seedFromJson } from '../../../../../test/mocks/seed';

src/services/item/plugins/recycled/recycled.ws.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ describe('Recycle websocket hooks', () => {
3232

3333
afterAll(async () => {
3434
await clearDatabase(db);
35-
app.close();
3635
ws.close();
36+
await app.close();
3737
});
3838

3939
afterEach(() => {

0 commit comments

Comments
 (0)