Skip to content

Commit b915481

Browse files
committed
test(sp): мигрировать domain и app на Vitest
1 parent f77f2c4 commit b915481

2 files changed

Lines changed: 25 additions & 24 deletions

File tree

projects/social_platform/src/app/app.component.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/** @format */
22

33
import { TestBed } from "@angular/core/testing";
4-
import { RouterTestingModule } from "@angular/router/testing";
54
import { AppComponent } from "./app.component";
65
import { AuthRepositoryPort } from "@domain/auth/ports/auth.repository.port";
76
import { of } from "rxjs";
87
import { TokenService } from "@corelib";
98
import { LoadingService } from "@api/shared/loading.service";
9+
import { beforeEach, describe, expect, it, vi } from "vitest";
1010

1111
describe("AppComponent", () => {
1212
beforeEach(async () => {
@@ -17,15 +17,15 @@ describe("AppComponent", () => {
1717
fetchLeaderProjects: of({} as any),
1818
};
1919

20-
const tokenSpy = { getTokens: jasmine.createSpy("getTokens").and.returnValue(null) };
20+
const tokenSpy = { getTokens: vi.fn().mockReturnValue(null) };
2121
const loadingSpy = {
22-
show: jasmine.createSpy("show"),
23-
hide: jasmine.createSpy("hide"),
22+
show: vi.fn(),
23+
hide: vi.fn(),
2424
isLoading$: of(false),
2525
};
2626

2727
await TestBed.configureTestingModule({
28-
imports: [RouterTestingModule, AppComponent],
28+
imports: [AppComponent],
2929
providers: [
3030
{ provide: AuthRepositoryPort, useValue: authPortSpy },
3131
{ provide: TokenService, useValue: tokenSpy },

projects/social_platform/src/app/domain/shared/entity-cache.spec.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,19 @@ describe("EntityCache", () => {
1616
factoryCallCount = 0;
1717
});
1818

19-
it("возвращает один и тот же Observable при повторном getOrFetch", done => {
20-
const first = cache.getOrFetch(1, factory);
21-
const second = cache.getOrFetch(1, factory);
19+
it("возвращает один и тот же Observable при повторном getOrFetch", () =>
20+
new Promise<void>(done => {
21+
const first = cache.getOrFetch(1, factory);
22+
const second = cache.getOrFetch(1, factory);
2223

23-
expect(first).toBe(second);
24-
expect(factoryCallCount).toBe(1);
24+
expect(first).toBe(second);
25+
expect(factoryCallCount).toBe(1);
2526

26-
first.subscribe(value => {
27-
expect(value).toBe("value-1");
28-
done();
29-
});
30-
});
27+
first.subscribe(value => {
28+
expect(value).toBe("value-1");
29+
done();
30+
});
31+
}));
3132

3233
it("разные id — разные Observable", () => {
3334
const a = cache.getOrFetch(1, factory);
@@ -59,11 +60,11 @@ describe("EntityCache", () => {
5960

6061
describe("с TTL", () => {
6162
beforeEach(() => {
62-
jasmine.clock().install();
63+
vi.useFakeTimers();
6364
});
6465

6566
afterEach(() => {
66-
jasmine.clock().uninstall();
67+
vi.useRealTimers();
6768
});
6869

6970
it("возвращает кешированный Observable до истечения TTL", () => {
@@ -79,7 +80,7 @@ describe("EntityCache", () => {
7980
const ttlCache = new EntityCache<string>(5000);
8081
const first = ttlCache.getOrFetch(1, factory);
8182

82-
jasmine.clock().mockDate(new Date(Date.now() + 3000));
83+
vi.setSystemTime(new Date(Date.now() + 3000));
8384

8485
const second = ttlCache.getOrFetch(1, factory);
8586

@@ -104,20 +105,20 @@ describe("EntityCache", () => {
104105
let swrFactoryCallCount: number;
105106

106107
beforeEach(() => {
107-
jasmine.clock().install();
108+
vi.useFakeTimers();
108109
subject$ = new Subject<string>();
109110
swrCache = new EntityCache<string>(5000);
110111
swrFactoryCallCount = 0;
111112
});
112113

113114
afterEach(() => {
114-
jasmine.clock().uninstall();
115+
vi.useRealTimers();
115116
});
116117

117118
it("после TTL возвращает стухшие данные немедленно", () => {
118119
swrCache.getOrFetch(1, () => of("initial"));
119120

120-
jasmine.clock().mockDate(new Date(Date.now() + 6000));
121+
vi.setSystemTime(new Date(Date.now() + 6000));
121122

122123
let emitted = "";
123124
swrCache.getOrFetch(1, () => of("refreshed")).subscribe(v => (emitted = v));
@@ -131,7 +132,7 @@ describe("EntityCache", () => {
131132
// ("macroTask setTimeout can not transition to running") и течёт в другие спеки.
132133
swrCache.getOrFetch(1, () => of("initial"));
133134

134-
jasmine.clock().mockDate(new Date(Date.now() + 6000));
135+
vi.setSystemTime(new Date(Date.now() + 6000));
135136

136137
swrCache.getOrFetch(1, () => {
137138
swrFactoryCallCount++;
@@ -149,7 +150,7 @@ describe("EntityCache", () => {
149150
it("не запускает повторный re-fetch если предыдущий ещё летит", () => {
150151
swrCache.getOrFetch(1, () => of("initial"));
151152

152-
jasmine.clock().mockDate(new Date(Date.now() + 6000));
153+
vi.setSystemTime(new Date(Date.now() + 6000));
153154

154155
const longSubject = new Subject<string>();
155156
swrCache.getOrFetch(1, () => {
@@ -170,7 +171,7 @@ describe("EntityCache", () => {
170171
it("invalidate останавливает фоновый re-fetch", () => {
171172
swrCache.getOrFetch(1, () => of("initial"));
172173

173-
jasmine.clock().mockDate(new Date(Date.now() + 6000));
174+
vi.setSystemTime(new Date(Date.now() + 6000));
174175

175176
const longSubject = new Subject<string>();
176177
swrCache.getOrFetch(1, () => {

0 commit comments

Comments
 (0)