-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.spec.ts
More file actions
69 lines (62 loc) · 2.24 KB
/
Copy pathapp.spec.ts
File metadata and controls
69 lines (62 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { TestBed } from "@angular/core/testing";
import { OidcSecurityService, LoginResponse } from "angular-auth-oidc-client";
import { Observable, of } from "rxjs";
import { App } from "./app";
function makeLoginResponse(overrides: Partial<LoginResponse>): LoginResponse {
return {
isAuthenticated: false,
userData: null,
accessToken: "",
idToken: "",
configId: "",
...overrides,
} as LoginResponse;
}
describe("App", () => {
let checkAuthFn: () => Observable<LoginResponse>;
const mockOidcService = {
get checkAuth() {
return checkAuthFn;
},
authorize: vi.fn(),
logoff: vi.fn(() => of(null)),
};
beforeEach(async () => {
checkAuthFn = () => of(makeLoginResponse({ isAuthenticated: false, userData: null }));
await TestBed.configureTestingModule({
imports: [App],
})
.overrideProvider(OidcSecurityService, { useValue: mockOidcService })
.compileComponents();
});
it("should render sign in button when not authenticated", async () => {
checkAuthFn = () => of(makeLoginResponse({ isAuthenticated: false, userData: null }));
const fixture = TestBed.createComponent(App);
await fixture.whenStable();
const compiled = fixture.nativeElement as HTMLElement;
expect(compiled.querySelector("button")?.textContent).toContain("Sign in");
});
it("should display user info when authenticated", async () => {
checkAuthFn = () =>
of(
makeLoginResponse({
isAuthenticated: true,
userData: { given_name: "Jane", family_name: "Doe", email: "jane@example.com" },
}),
);
const fixture = TestBed.createComponent(App);
await fixture.whenStable();
fixture.detectChanges();
const compiled = fixture.nativeElement as HTMLElement;
expect(compiled.textContent).toContain("Jane");
expect(compiled.textContent).toContain("Doe");
});
it("should call authorize on sign in click", async () => {
checkAuthFn = () => of(makeLoginResponse({ isAuthenticated: false, userData: null }));
const fixture = TestBed.createComponent(App);
await fixture.whenStable();
const button = fixture.nativeElement.querySelector("button");
button.click();
expect(mockOidcService.authorize).toHaveBeenCalled();
});
});