-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.spec.ts
More file actions
161 lines (143 loc) · 5.71 KB
/
utils.spec.ts
File metadata and controls
161 lines (143 loc) · 5.71 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { TestBed } from "@angular/core/testing";
import { FormGroup, FormControl, Validators } from "@angular/forms";
import { User, UserResponse, UserRole } from "./models/user";
import {
parseUserData,
encodeUserData,
setErrors,
controlErrorMessages$,
formErrorMessages$,
updateFormValidity,
} from "./utils";
import { toSignal } from "@angular/core/rxjs-interop";
describe("User utils", () => {
let form: FormGroup;
beforeEach(() => {
form = new FormGroup({
username: new FormControl<string>("", {
validators: [Validators.required],
}),
email: new FormControl<string>("", {
validators: [Validators.required, Validators.email],
}),
});
});
describe("parseUserData", () => {
it("should return null if result is null", () => {
const result: UserResponse | null = null;
const user = parseUserData(result);
expect(user).toBeNull();
});
it("should return a User object if result is not null", () => {
const result: UserResponse = {
id: 1,
username: "testuser",
email: "test@example.com",
firstName: "Test",
lastName: "User",
isStaff: true,
role: "visitor",
canCreateProblem: false,
canEditProblem: false,
canEditKb: false,
canAddLabelAnnotations: false,
canCopyProblem: false,
};
const user = parseUserData(result);
expect(user).toBeInstanceOf(User);
expect(user?.id).toBe(1);
expect(user?.username).toBe("testuser");
expect(user?.email).toBe("test@example.com");
expect(user?.firstName).toBe("Test");
expect(user?.lastName).toBe("User");
expect(user?.isStaff).toBe(true);
expect(user?.role).toBe(UserRole.VISITOR);
expect(user?.canEditProblem).toBe(false);
expect(user?.canCreateProblem).toBe(false);
});
});
describe("encodeUserData", () => {
it("should encode partial User data to UserResponse object", () => {
const data: Partial<User> = {
id: 1,
username: "testuser",
email: "test@example.com",
firstName: "Test",
lastName: "User",
isStaff: true,
};
const encoded = encodeUserData(data);
expect(encoded).toEqual({
id: 1,
username: "testuser",
email: "test@example.com",
firstName: "Test",
lastName: "User",
isStaff: true,
});
});
});
describe("setErrors", () => {
it("should set errors on associated controls", () => {
const errorObject = {
username: "Username is required.",
email: "Email is invalid.",
};
setErrors(errorObject, form);
expect(form.get("username")?.errors).toEqual({
invalid: "Username is required.",
});
expect(form.get("email")?.errors).toEqual({
invalid: "Email is invalid.",
});
});
it("should set errors on the form itself for non-associated controls", () => {
const errorObject = {
random: "Passwords must be identical.",
};
setErrors(errorObject, form);
expect(form.errors).toEqual({
invalid: "Passwords must be identical.",
});
});
});
describe("controlErrorMessages$", () => {
it("should return an Observable of error messages for a specific control", () => {
const usernameMessages = TestBed.runInInjectionContext(() =>
toSignal(controlErrorMessages$(form, "username")),
);
form.get("username")?.updateValueAndValidity();
expect(usernameMessages()).toEqual(["Username is required."]);
});
it("should use errors based on a provided lookup name", () => {
const usernameMessages = TestBed.runInInjectionContext(() =>
toSignal(controlErrorMessages$(form, "username", "email")),
);
form.get("username")?.updateValueAndValidity();
expect(usernameMessages()).toEqual(["Email is required."]);
});
});
describe("formErrorMessages$", () => {
it("should return an Observable of error messages for the form", () => {
const formMessages = TestBed.runInInjectionContext(() =>
toSignal(formErrorMessages$(form)),
);
form.updateValueAndValidity();
expect(formMessages()?.length).toBe(0);
form.setErrors({ random: "This is a random error." });
expect(formMessages()).toEqual(["This is a random error."]);
});
});
describe("updateFormValidity", () => {
it("should update the validity of all controls in the form", () => {
form.controls["username"].setErrors({
invalid: "Username is required.",
});
form.controls["email"].setErrors({ invalid: "Email is invalid." });
updateFormValidity(form);
expect(form.controls["username"].valid).toBe(false);
expect(form.controls["email"].valid).toBe(false);
expect(form.valid).toBe(false);
});
});
});