Skip to content

Commit 4a162b9

Browse files
committed
test(sp): мигрировать infrastructure на Vitest
1 parent 60e958e commit 4a162b9

47 files changed

Lines changed: 1275 additions & 1234 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

projects/social_platform/src/app/infrastructure/adapters/auth/auth-http.adapter.spec.ts

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,12 @@ import { LoginResponse, RegisterResponse } from "@core/lib/models/auth/http.mode
99

1010
describe("AuthHttpAdapter", () => {
1111
let adapter: AuthHttpAdapter;
12-
let api: jasmine.SpyObj<ApiService>;
13-
let tokens: jasmine.SpyObj<TokenService>;
12+
let api: any;
13+
let tokens: any;
1414

1515
function setup(): void {
16-
api = jasmine.createSpyObj<ApiService>("ApiService", [
17-
"get",
18-
"post",
19-
"patch",
20-
"put",
21-
"getFile",
22-
]);
23-
tokens = jasmine.createSpyObj<TokenService>("TokenService", ["getTokens"]);
16+
api = { get: vi.fn(), post: vi.fn(), patch: vi.fn(), put: vi.fn(), getFile: vi.fn() };
17+
tokens = { getTokens: vi.fn() };
2418
TestBed.configureTestingModule({
2519
providers: [
2620
AuthHttpAdapter,
@@ -33,147 +27,149 @@ describe("AuthHttpAdapter", () => {
3327

3428
it("login идёт в POST /api/token/ с email/password", () => {
3529
setup();
36-
api.post.and.returnValue(of({} as LoginResponse));
30+
api.post.mockReturnValue(of({} as LoginResponse));
3731

3832
adapter.login({ email: "u@e.com", password: "p" }).subscribe();
3933

40-
expect(api.post).toHaveBeenCalledOnceWith("/api/token/", {
34+
expect(api.post).toHaveBeenCalledExactlyOnceWith("/api/token/", {
4135
email: "u@e.com",
4236
password: "p",
4337
});
4438
});
4539

4640
it("logout идёт в POST /auth/logout/ с refreshToken из TokenService", () => {
4741
setup();
48-
tokens.getTokens.and.returnValue({ access: "a", refresh: "r" });
49-
api.post.and.returnValue(of(undefined));
42+
tokens.getTokens.mockReturnValue({ access: "a", refresh: "r" });
43+
api.post.mockReturnValue(of(undefined));
5044

5145
adapter.logout().subscribe();
5246

53-
expect(api.post).toHaveBeenCalledOnceWith("/auth/logout/", { refreshToken: "r" });
47+
expect(api.post).toHaveBeenCalledExactlyOnceWith("/auth/logout/", { refreshToken: "r" });
5448
});
5549

5650
it("register идёт в POST /auth/users/", () => {
5751
setup();
58-
api.post.and.returnValue(of({} as RegisterResponse));
52+
api.post.mockReturnValue(of({} as RegisterResponse));
5953
const data = { email: "e", password: "p" } as unknown as Parameters<typeof adapter.register>[0];
6054

6155
adapter.register(data).subscribe();
6256

63-
expect(api.post).toHaveBeenCalledOnceWith("/auth/users/", data);
57+
expect(api.post).toHaveBeenCalledExactlyOnceWith("/auth/users/", data);
6458
});
6559

6660
it("downloadCV идёт в getFile /auth/users/download_cv/", () => {
6761
setup();
68-
api.getFile.and.returnValue(of(new Blob()));
62+
api.getFile.mockReturnValue(of(new Blob()));
6963

7064
adapter.downloadCV().subscribe();
7165

72-
expect(api.getFile).toHaveBeenCalledOnceWith("/auth/users/download_cv/");
66+
expect(api.getFile).toHaveBeenCalledExactlyOnceWith("/auth/users/download_cv/");
7367
});
7468

7569
it("getProfile идёт в GET /auth/users/current/", () => {
7670
setup();
77-
api.get.and.returnValue(of({} as User));
71+
api.get.mockReturnValue(of({} as User));
7872

7973
adapter.getProfile().subscribe();
8074

81-
expect(api.get).toHaveBeenCalledOnceWith("/auth/users/current/");
75+
expect(api.get).toHaveBeenCalledExactlyOnceWith("/auth/users/current/");
8276
});
8377

8478
it("getUserRoles идёт в GET /auth/users/types/", () => {
8579
setup();
86-
api.get.and.returnValue(of([[1, "r"]] as [[number, string]]));
80+
api.get.mockReturnValue(of([[1, "r"]] as [[number, string]]));
8781

8882
adapter.getUserRoles().subscribe();
8983

90-
expect(api.get).toHaveBeenCalledOnceWith("/auth/users/types/");
84+
expect(api.get).toHaveBeenCalledExactlyOnceWith("/auth/users/types/");
9185
});
9286

9387
it("getLeaderProjects идёт в GET /auth/users/projects/leader/", () => {
9488
setup();
95-
api.get.and.returnValue(of({ count: 0, results: [], next: "", previous: "" }));
89+
api.get.mockReturnValue(of({ count: 0, results: [], next: "", previous: "" }));
9690

9791
adapter.getLeaderProjects().subscribe();
9892

99-
expect(api.get).toHaveBeenCalledOnceWith("/auth/users/projects/leader/");
93+
expect(api.get).toHaveBeenCalledExactlyOnceWith("/auth/users/projects/leader/");
10094
});
10195

10296
it("getChangeableRoles идёт в GET /auth/users/roles/", () => {
10397
setup();
104-
api.get.and.returnValue(of([[1, "r"]] as [[number, string]]));
98+
api.get.mockReturnValue(of([[1, "r"]] as [[number, string]]));
10599

106100
adapter.getChangeableRoles().subscribe();
107101

108-
expect(api.get).toHaveBeenCalledOnceWith("/auth/users/roles/");
102+
expect(api.get).toHaveBeenCalledExactlyOnceWith("/auth/users/roles/");
109103
});
110104

111105
it("getUser идёт в GET /auth/users/:id/", () => {
112106
setup();
113-
api.get.and.returnValue(of({} as User));
107+
api.get.mockReturnValue(of({} as User));
114108

115109
adapter.getUser(7).subscribe();
116110

117-
expect(api.get).toHaveBeenCalledOnceWith("/auth/users/7/");
111+
expect(api.get).toHaveBeenCalledExactlyOnceWith("/auth/users/7/");
118112
});
119113

120114
it("saveAvatar идёт в PATCH /auth/users/:pid с avatar", () => {
121115
setup();
122-
api.patch.and.returnValue(of({} as User));
116+
api.patch.mockReturnValue(of({} as User));
123117

124118
adapter.saveAvatar("https://x/a.png", 7).subscribe();
125119

126-
expect(api.patch).toHaveBeenCalledOnceWith("/auth/users/7/", { avatar: "https://x/a.png" });
120+
expect(api.patch).toHaveBeenCalledExactlyOnceWith("/auth/users/7/", {
121+
avatar: "https://x/a.png",
122+
});
127123
});
128124

129125
it("saveProfile идёт в PATCH /auth/users/:id/ с частичными данными", () => {
130126
setup();
131-
api.patch.and.returnValue(of({} as User));
127+
api.patch.mockReturnValue(of({} as User));
132128
const profile: Partial<User> = { id: 7, firstName: "A" };
133129

134130
adapter.saveProfile(profile).subscribe();
135131

136-
expect(api.patch).toHaveBeenCalledOnceWith("/auth/users/7/", profile);
132+
expect(api.patch).toHaveBeenCalledExactlyOnceWith("/auth/users/7/", profile);
137133
});
138134

139135
it("setOnboardingStage идёт в PUT /auth/users/:pid/set_onboarding_stage/", () => {
140136
setup();
141-
api.put.and.returnValue(of({} as User));
137+
api.put.mockReturnValue(of({} as User));
142138

143139
adapter.setOnboardingStage(2, 7).subscribe();
144140

145-
expect(api.put).toHaveBeenCalledOnceWith("/auth/users/7/set_onboarding_stage/", {
141+
expect(api.put).toHaveBeenCalledExactlyOnceWith("/auth/users/7/set_onboarding_stage/", {
146142
onboardingStage: 2,
147143
});
148144
});
149145

150146
it("resetPassword идёт в POST /auth/reset_password/ c email", () => {
151147
setup();
152-
api.post.and.returnValue(of(undefined));
148+
api.post.mockReturnValue(of(undefined));
153149

154150
adapter.resetPassword("u@e.com").subscribe();
155151

156-
expect(api.post).toHaveBeenCalledOnceWith("/auth/reset_password/", { email: "u@e.com" });
152+
expect(api.post).toHaveBeenCalledExactlyOnceWith("/auth/reset_password/", { email: "u@e.com" });
157153
});
158154

159155
it("setPassword идёт в POST /auth/reset_password/confirm/ c password/token", () => {
160156
setup();
161-
api.post.and.returnValue(of(undefined));
157+
api.post.mockReturnValue(of(undefined));
162158

163159
adapter.setPassword("newpass", "tok").subscribe();
164160

165-
expect(api.post).toHaveBeenCalledOnceWith("/auth/reset_password/confirm/", {
161+
expect(api.post).toHaveBeenCalledExactlyOnceWith("/auth/reset_password/confirm/", {
166162
password: "newpass",
167163
token: "tok",
168164
});
169165
});
170166

171167
it("resendEmail идёт в POST /auth/resend_email/ c email", () => {
172168
setup();
173-
api.post.and.returnValue(of({} as User));
169+
api.post.mockReturnValue(of({} as User));
174170

175171
adapter.resendEmail("u@e.com").subscribe();
176172

177-
expect(api.post).toHaveBeenCalledOnceWith("/auth/resend_email/", { email: "u@e.com" });
173+
expect(api.post).toHaveBeenCalledExactlyOnceWith("/auth/resend_email/", { email: "u@e.com" });
178174
});
179175
});

projects/social_platform/src/app/infrastructure/adapters/chat/chat-http.adapter.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import { ChatHttpAdapter } from "./chat-http.adapter";
77

88
describe("ChatHttpAdapter", () => {
99
let adapter: ChatHttpAdapter;
10-
let api: jasmine.SpyObj<ApiService>;
10+
let api: any;
1111

1212
function setup(): void {
13-
api = jasmine.createSpyObj<ApiService>("ApiService", ["get"]);
13+
api = { get: vi.fn() };
1414
TestBed.configureTestingModule({
1515
providers: [ChatHttpAdapter, { provide: ApiService, useValue: api }],
1616
});
@@ -19,42 +19,42 @@ describe("ChatHttpAdapter", () => {
1919

2020
it("loadMessages идёт в GET /chats/projects/:id/messages/ c offset/limit", () => {
2121
setup();
22-
api.get.and.returnValue(of({ count: 0, results: [], next: "", previous: "" }));
22+
api.get.mockReturnValue(of({ count: 0, results: [], next: "", previous: "" }));
2323

2424
adapter.loadMessages(42, "projects", 20, 10).subscribe();
2525

26-
const [url, params] = api.get.calls.mostRecent().args;
26+
const [url, params] = api.get.mock.lastCall;
2727
expect(url).toBe("/chats/projects/42/messages/");
2828
expect(params?.get("offset")).toBe("20");
2929
expect(params?.get("limit")).toBe("10");
3030
});
3131

3232
it("loadMessages не добавляет параметры если offset/limit не переданы", () => {
3333
setup();
34-
api.get.and.returnValue(of({ count: 0, results: [], next: "", previous: "" }));
34+
api.get.mockReturnValue(of({ count: 0, results: [], next: "", previous: "" }));
3535

3636
adapter.loadMessages(42, "projects").subscribe();
3737

38-
const params = api.get.calls.mostRecent().args[1];
39-
expect(params?.has("offset")).toBeFalse();
40-
expect(params?.has("limit")).toBeFalse();
38+
const params = api.get.mock.lastCall[1];
39+
expect(params?.has("offset")).toBe(false);
40+
expect(params?.has("limit")).toBe(false);
4141
});
4242

4343
it("loadProjectFiles идёт в GET /chats/projects/:id/files", () => {
4444
setup();
45-
api.get.and.returnValue(of([]));
45+
api.get.mockReturnValue(of([]));
4646

4747
adapter.loadProjectFiles(42).subscribe();
4848

49-
expect(api.get).toHaveBeenCalledOnceWith("/chats/projects/42/files/");
49+
expect(api.get).toHaveBeenCalledExactlyOnceWith("/chats/projects/42/files/");
5050
});
5151

5252
it("hasUnreads идёт в GET /chats/has-unreads/", () => {
5353
setup();
54-
api.get.and.returnValue(of({ hasUnreads: true }));
54+
api.get.mockReturnValue(of({ hasUnreads: true }));
5555

5656
adapter.hasUnreads().subscribe();
5757

58-
expect(api.get).toHaveBeenCalledOnceWith("/chats/has-unreads/");
58+
expect(api.get).toHaveBeenCalledExactlyOnceWith("/chats/has-unreads/");
5959
});
6060
});

0 commit comments

Comments
 (0)