create-huenei-template 0.2.2 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config/templates.js +3 -3
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-component/SKILL.md +288 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-component/references/component-patterns.md +358 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-di/SKILL.md +387 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-di/references/di-patterns.md +519 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-directives/SKILL.md +437 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-directives/references/directive-patterns.md +570 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-forms/SKILL.md +434 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-forms/references/form-patterns.md +398 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-http/SKILL.md +366 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-http/references/http-patterns.md +448 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-routing/SKILL.md +395 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-routing/references/routing-patterns.md +472 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-signals/SKILL.md +302 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-signals/references/signal-patterns.md +404 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-ssr/SKILL.md +435 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-ssr/references/ssr-patterns.md +497 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-testing/SKILL.md +660 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-testing/references/testing-patterns.md +707 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-tooling/SKILL.md +352 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-tooling/references/tooling-patterns.md +448 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-accessibility/SKILL.md +780 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-components/SKILL.md +718 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-design-systems/SKILL.md +612 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-layouts/SKILL.md +594 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/SKILL.md +432 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/references/animation-patterns.md +427 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/SKILL.md +354 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/references/troubleshooting-guide.md +306 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/SKILL.md +578 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/references/react-patterns.md +353 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/SKILL.md +608 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/advanced-theme-patterns.md +215 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/custom-utilities-guide.md +281 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-mobile-first/SKILL.md +768 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/SKILL.md +374 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/references/bundle-optimization.md +244 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-plugins/SKILL.md +505 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/SKILL.md +601 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/references/advanced-responsive-patterns.md +485 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/web-design-guidelines/SKILL.md +39 -0
- package/dist/templates/frontend/angular-base/package.json +0 -1
- package/dist/templates/frontend/angular-base/pnpm-lock.yaml +8183 -0
- package/package.json +1 -1
- package/templates/frontend/angular-base/.cursor/skills/angular-component/SKILL.md +288 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-component/references/component-patterns.md +358 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-di/SKILL.md +387 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-di/references/di-patterns.md +519 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-directives/SKILL.md +437 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-directives/references/directive-patterns.md +570 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-forms/SKILL.md +434 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-forms/references/form-patterns.md +398 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-http/SKILL.md +366 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-http/references/http-patterns.md +448 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-routing/SKILL.md +395 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-routing/references/routing-patterns.md +472 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-signals/SKILL.md +302 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-signals/references/signal-patterns.md +404 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-ssr/SKILL.md +435 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-ssr/references/ssr-patterns.md +497 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-testing/SKILL.md +660 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-testing/references/testing-patterns.md +707 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-tooling/SKILL.md +352 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-tooling/references/tooling-patterns.md +448 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-accessibility/SKILL.md +780 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-components/SKILL.md +718 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-design-systems/SKILL.md +612 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-layouts/SKILL.md +594 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/SKILL.md +432 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/references/animation-patterns.md +427 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/SKILL.md +354 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/references/troubleshooting-guide.md +306 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/SKILL.md +578 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/references/react-patterns.md +353 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/SKILL.md +608 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/advanced-theme-patterns.md +215 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/custom-utilities-guide.md +281 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-mobile-first/SKILL.md +768 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/SKILL.md +374 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/references/bundle-optimization.md +244 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-plugins/SKILL.md +505 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/SKILL.md +601 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/references/advanced-responsive-patterns.md +485 -0
- package/templates/frontend/angular-base/.cursor/skills/web-design-guidelines/SKILL.md +39 -0
- package/templates/frontend/angular-base/package.json +0 -1
- package/templates/frontend/angular-base/pnpm-lock.yaml +8183 -0
- package/templates/frontend/angular-base/package-lock.json +0 -13321
|
@@ -0,0 +1,707 @@
|
|
|
1
|
+
# Angular Testing Patterns
|
|
2
|
+
|
|
3
|
+
## Table of Contents
|
|
4
|
+
- [Vitest Advanced Patterns](#vitest-advanced-patterns)
|
|
5
|
+
- [Component Harnesses](#component-harnesses)
|
|
6
|
+
- [Testing Router](#testing-router)
|
|
7
|
+
- [Testing Forms](#testing-forms)
|
|
8
|
+
- [Testing Directives](#testing-directives)
|
|
9
|
+
- [Testing Pipes](#testing-pipes)
|
|
10
|
+
- [E2E Testing Setup](#e2e-testing-setup)
|
|
11
|
+
|
|
12
|
+
## Vitest Advanced Patterns
|
|
13
|
+
|
|
14
|
+
### Snapshot Testing
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import { describe, it, expect } from 'vitest';
|
|
18
|
+
|
|
19
|
+
describe('UserCard', () => {
|
|
20
|
+
it('should match snapshot', () => {
|
|
21
|
+
const fixture = TestBed.createComponent(UserCard);
|
|
22
|
+
fixture.componentRef.setInput('user', { id: '1', name: 'John', email: 'john@example.com' });
|
|
23
|
+
fixture.detectChanges();
|
|
24
|
+
|
|
25
|
+
expect(fixture.nativeElement.innerHTML).toMatchSnapshot();
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Parameterized Tests
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { describe, it, expect } from 'vitest';
|
|
34
|
+
|
|
35
|
+
describe('Validator', () => {
|
|
36
|
+
it.each([
|
|
37
|
+
{ input: '', expected: false },
|
|
38
|
+
{ input: 'test', expected: false },
|
|
39
|
+
{ input: 'test@example.com', expected: true },
|
|
40
|
+
{ input: 'invalid@', expected: false },
|
|
41
|
+
])('should validate email "$input" as $expected', ({ input, expected }) => {
|
|
42
|
+
expect(isValidEmail(input)).toBe(expected);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Testing with Fake Timers
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
51
|
+
|
|
52
|
+
describe('Debounced Search', () => {
|
|
53
|
+
beforeEach(() => {
|
|
54
|
+
vi.useFakeTimers();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
afterEach(() => {
|
|
58
|
+
vi.useRealTimers();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should debounce search input', async () => {
|
|
62
|
+
const fixture = TestBed.createComponent(Search);
|
|
63
|
+
fixture.detectChanges();
|
|
64
|
+
|
|
65
|
+
fixture.componentInstance.query.set('test');
|
|
66
|
+
|
|
67
|
+
// Search not called yet
|
|
68
|
+
expect(fixture.componentInstance.results()).toEqual([]);
|
|
69
|
+
|
|
70
|
+
// Advance timers
|
|
71
|
+
vi.advanceTimersByTime(300);
|
|
72
|
+
await fixture.whenStable();
|
|
73
|
+
fixture.detectChanges();
|
|
74
|
+
|
|
75
|
+
expect(fixture.componentInstance.results().length).toBeGreaterThan(0);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Module Mocking
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
84
|
+
|
|
85
|
+
// Mock entire module
|
|
86
|
+
vi.mock('./analytics.service', () => ({
|
|
87
|
+
Analytics: class {
|
|
88
|
+
track = vi.fn();
|
|
89
|
+
identify = vi.fn();
|
|
90
|
+
},
|
|
91
|
+
}));
|
|
92
|
+
|
|
93
|
+
describe('with mocked analytics', () => {
|
|
94
|
+
it('should track events', () => {
|
|
95
|
+
const fixture = TestBed.createComponent(Dashboard);
|
|
96
|
+
const analytics = TestBed.inject(Analytics);
|
|
97
|
+
|
|
98
|
+
fixture.detectChanges();
|
|
99
|
+
|
|
100
|
+
expect(analytics.track).toHaveBeenCalledWith('dashboard_viewed');
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Testing Async/Await
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
109
|
+
|
|
110
|
+
describe('User', () => {
|
|
111
|
+
it('should load user data', async () => {
|
|
112
|
+
const mockUser = { id: '1', name: 'Test' };
|
|
113
|
+
const httpMock = TestBed.inject(HttpTestingController);
|
|
114
|
+
const service = TestBed.inject(User);
|
|
115
|
+
|
|
116
|
+
const userPromise = service.loadUser('1');
|
|
117
|
+
|
|
118
|
+
httpMock.expectOne('/api/users/1').flush(mockUser);
|
|
119
|
+
|
|
120
|
+
const user = await userPromise;
|
|
121
|
+
expect(user).toEqual(mockUser);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Coverage Configuration
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
// vite.config.ts
|
|
130
|
+
export default defineConfig({
|
|
131
|
+
test: {
|
|
132
|
+
coverage: {
|
|
133
|
+
provider: 'v8',
|
|
134
|
+
reporter: ['text', 'html', 'lcov'],
|
|
135
|
+
exclude: [
|
|
136
|
+
'node_modules/',
|
|
137
|
+
'src/test-setup.ts',
|
|
138
|
+
'**/*.spec.ts',
|
|
139
|
+
'**/*.d.ts',
|
|
140
|
+
],
|
|
141
|
+
thresholds: {
|
|
142
|
+
statements: 80,
|
|
143
|
+
branches: 80,
|
|
144
|
+
functions: 80,
|
|
145
|
+
lines: 80,
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Vitest UI Mode
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
# Run with UI
|
|
156
|
+
npx vitest --ui
|
|
157
|
+
|
|
158
|
+
# Open UI at specific port
|
|
159
|
+
npx vitest --ui --port 51204
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Concurrent Tests
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
import { describe, it, expect } from 'vitest';
|
|
166
|
+
|
|
167
|
+
// Run tests in this describe block concurrently
|
|
168
|
+
describe.concurrent('API calls', () => {
|
|
169
|
+
it('should fetch users', async () => {
|
|
170
|
+
// ...
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('should fetch products', async () => {
|
|
174
|
+
// ...
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it('should fetch orders', async () => {
|
|
178
|
+
// ...
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### Test Fixtures
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
import { describe, it, expect, beforeEach } from 'vitest';
|
|
187
|
+
|
|
188
|
+
// Shared test fixtures
|
|
189
|
+
const createTestUser = (overrides = {}) => ({
|
|
190
|
+
id: '1',
|
|
191
|
+
name: 'Test User',
|
|
192
|
+
email: 'test@example.com',
|
|
193
|
+
...overrides,
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
const createTestProduct = (overrides = {}) => ({
|
|
197
|
+
id: '1',
|
|
198
|
+
name: 'Test Product',
|
|
199
|
+
price: 99.99,
|
|
200
|
+
...overrides,
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
describe('Order', () => {
|
|
204
|
+
it('should calculate total', () => {
|
|
205
|
+
const fixture = TestBed.createComponent(Order);
|
|
206
|
+
fixture.componentRef.setInput('user', createTestUser());
|
|
207
|
+
fixture.componentRef.setInput('products', [
|
|
208
|
+
createTestProduct({ price: 10 }),
|
|
209
|
+
createTestProduct({ id: '2', price: 20 }),
|
|
210
|
+
]);
|
|
211
|
+
fixture.detectChanges();
|
|
212
|
+
|
|
213
|
+
expect(fixture.componentInstance.total()).toBe(30);
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## Component Harnesses
|
|
219
|
+
|
|
220
|
+
Use Angular CDK component harnesses for more maintainable tests:
|
|
221
|
+
|
|
222
|
+
### Creating a Harness
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
import { ComponentHarness, HarnessPredicate } from '@angular/cdk/testing';
|
|
226
|
+
|
|
227
|
+
export class CounterHarn extends ComponentHarness {
|
|
228
|
+
static hostSelector = 'app-counter';
|
|
229
|
+
|
|
230
|
+
// Locators
|
|
231
|
+
private getIncrementButton = this.locatorFor('button.increment');
|
|
232
|
+
private getDecrementButton = this.locatorFor('button.decrement');
|
|
233
|
+
private getCountDisplay = this.locatorFor('.count');
|
|
234
|
+
|
|
235
|
+
// Actions
|
|
236
|
+
async increment(): Promise<void> {
|
|
237
|
+
const button = await this.getIncrementButton();
|
|
238
|
+
await button.click();
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async decrement(): Promise<void> {
|
|
242
|
+
const button = await this.getDecrementButton();
|
|
243
|
+
await button.click();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Queries
|
|
247
|
+
async getCount(): Promise<number> {
|
|
248
|
+
const display = await this.getCountDisplay();
|
|
249
|
+
const text = await display.text();
|
|
250
|
+
return parseInt(text, 10);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Filter factory
|
|
254
|
+
static with(options: { count?: number } = {}): HarnessPredicate<CounterHarn> {
|
|
255
|
+
return new HarnessPredicate(CounterHarn, options)
|
|
256
|
+
.addOption('count', options.count, async (harness, count) => {
|
|
257
|
+
return (await harness.getCount()) === count;
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
### Using Harnesses in Tests
|
|
264
|
+
|
|
265
|
+
```typescript
|
|
266
|
+
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
|
|
267
|
+
|
|
268
|
+
describe('Counter with Harness', () => {
|
|
269
|
+
let loader: HarnessLoader;
|
|
270
|
+
|
|
271
|
+
beforeEach(async () => {
|
|
272
|
+
await TestBed.configureTestingModule({
|
|
273
|
+
imports: [Counter],
|
|
274
|
+
}).compileComponents();
|
|
275
|
+
|
|
276
|
+
const fixture = TestBed.createComponent(Counter);
|
|
277
|
+
loader = TestbedHarnessEnvironment.loader(fixture);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it('should increment count', async () => {
|
|
281
|
+
const counter = await loader.getHarness(CounterHarn);
|
|
282
|
+
|
|
283
|
+
expect(await counter.getCount()).toBe(0);
|
|
284
|
+
|
|
285
|
+
await counter.increment();
|
|
286
|
+
expect(await counter.getCount()).toBe(1);
|
|
287
|
+
|
|
288
|
+
await counter.increment();
|
|
289
|
+
expect(await counter.getCount()).toBe(2);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it('should find counter with specific count', async () => {
|
|
293
|
+
const counter = await loader.getHarness(CounterHarn);
|
|
294
|
+
await counter.increment();
|
|
295
|
+
await counter.increment();
|
|
296
|
+
|
|
297
|
+
// Find counter with count of 2
|
|
298
|
+
const counterWith2 = await loader.getHarness(CounterHarn.with({ count: 2 }));
|
|
299
|
+
expect(counterWith2).toBeTruthy();
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
## Testing Router
|
|
305
|
+
|
|
306
|
+
### RouterTestingHarness
|
|
307
|
+
|
|
308
|
+
```typescript
|
|
309
|
+
import { RouterTestingHarness } from '@angular/router/testing';
|
|
310
|
+
import { provideRouter } from '@angular/router';
|
|
311
|
+
|
|
312
|
+
describe('Router Navigation', () => {
|
|
313
|
+
let harness: RouterTestingHarness;
|
|
314
|
+
|
|
315
|
+
beforeEach(async () => {
|
|
316
|
+
await TestBed.configureTestingModule({
|
|
317
|
+
providers: [
|
|
318
|
+
provideRouter([
|
|
319
|
+
{ path: '', component: Home },
|
|
320
|
+
{ path: 'users/:id', component: UserCmpt },
|
|
321
|
+
]),
|
|
322
|
+
],
|
|
323
|
+
}).compileComponents();
|
|
324
|
+
|
|
325
|
+
harness = await RouterTestingHarness.create();
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
it('should navigate to user page', async () => {
|
|
329
|
+
const component = await harness.navigateByUrl('/users/123', UserCmpt);
|
|
330
|
+
|
|
331
|
+
expect(component.id()).toBe('123');
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it('should display user name', async () => {
|
|
335
|
+
await harness.navigateByUrl('/users/123');
|
|
336
|
+
|
|
337
|
+
expect(harness.routeNativeElement?.textContent).toContain('User 123');
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
### Testing Guards
|
|
343
|
+
|
|
344
|
+
```typescript
|
|
345
|
+
describe('AuthGuard', () => {
|
|
346
|
+
let authService: jasmine.SpyObj<Auth>;
|
|
347
|
+
|
|
348
|
+
beforeEach(() => {
|
|
349
|
+
authService = jasmine.createSpyObj('Auth', ['isAuthenticated']);
|
|
350
|
+
|
|
351
|
+
TestBed.configureTestingModule({
|
|
352
|
+
providers: [
|
|
353
|
+
{ provide: Auth, useValue: authService },
|
|
354
|
+
provideRouter([
|
|
355
|
+
{ path: 'login', component: Login },
|
|
356
|
+
{
|
|
357
|
+
path: 'dashboard',
|
|
358
|
+
component: Dashboard,
|
|
359
|
+
canActivate: [authGuard],
|
|
360
|
+
},
|
|
361
|
+
]),
|
|
362
|
+
],
|
|
363
|
+
});
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
it('should allow access when authenticated', async () => {
|
|
367
|
+
authService.isAuthenticated.and.returnValue(true);
|
|
368
|
+
|
|
369
|
+
const harness = await RouterTestingHarness.create();
|
|
370
|
+
await harness.navigateByUrl('/dashboard');
|
|
371
|
+
|
|
372
|
+
expect(harness.routeNativeElement?.textContent).toContain('Dashboard');
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
it('should redirect to login when not authenticated', async () => {
|
|
376
|
+
authService.isAuthenticated.and.returnValue(false);
|
|
377
|
+
|
|
378
|
+
const harness = await RouterTestingHarness.create();
|
|
379
|
+
await harness.navigateByUrl('/dashboard');
|
|
380
|
+
|
|
381
|
+
expect(TestBed.inject(Router).url).toBe('/login');
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
## Testing Forms
|
|
387
|
+
|
|
388
|
+
### Testing Signal Forms
|
|
389
|
+
|
|
390
|
+
```typescript
|
|
391
|
+
import { form, FormField, required, email } from '@angular/forms/signals';
|
|
392
|
+
|
|
393
|
+
@Component({
|
|
394
|
+
imports: [FormField],
|
|
395
|
+
template: `
|
|
396
|
+
<form (submit)="onSubmit($event)">
|
|
397
|
+
<input [formField]="loginForm.email" />
|
|
398
|
+
<input [formField]="loginForm.password" type="password" />
|
|
399
|
+
<button type="submit" [disabled]="loginForm().invalid()">Submit</button>
|
|
400
|
+
</form>
|
|
401
|
+
`,
|
|
402
|
+
})
|
|
403
|
+
export class Login {
|
|
404
|
+
model = signal({ email: '', password: '' });
|
|
405
|
+
loginForm = form(this.model, (schemaPath) => {
|
|
406
|
+
required(schemaPath.email);
|
|
407
|
+
email(schemaPath.email);
|
|
408
|
+
required(schemaPath.password);
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
submitted = signal(false);
|
|
412
|
+
|
|
413
|
+
onSubmit(event: Event) {
|
|
414
|
+
event.preventDefault();
|
|
415
|
+
if (this.loginForm().valid()) {
|
|
416
|
+
this.submitted.set(true);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
describe('Login', () => {
|
|
422
|
+
let fixture: ComponentFixture<Login>;
|
|
423
|
+
let component: Login;
|
|
424
|
+
|
|
425
|
+
beforeEach(async () => {
|
|
426
|
+
await TestBed.configureTestingModule({
|
|
427
|
+
imports: [Login],
|
|
428
|
+
}).compileComponents();
|
|
429
|
+
|
|
430
|
+
fixture = TestBed.createComponent(Login);
|
|
431
|
+
component = fixture.componentInstance;
|
|
432
|
+
fixture.detectChanges();
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
it('should be invalid when empty', () => {
|
|
436
|
+
expect(component.loginForm().invalid()).toBeTrue();
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
it('should be valid with correct data', () => {
|
|
440
|
+
component.model.set({
|
|
441
|
+
email: 'test@example.com',
|
|
442
|
+
password: 'password123',
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
expect(component.loginForm().valid()).toBeTrue();
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
it('should show email error for invalid email', () => {
|
|
449
|
+
component.loginForm.email().value.set('invalid');
|
|
450
|
+
fixture.detectChanges();
|
|
451
|
+
|
|
452
|
+
expect(component.loginForm.email().invalid()).toBeTrue();
|
|
453
|
+
expect(component.loginForm.email().errors().some(e => e.kind === 'email')).toBeTrue();
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
it('should disable submit button when invalid', () => {
|
|
457
|
+
const button = fixture.nativeElement.querySelector('button');
|
|
458
|
+
expect(button.disabled).toBeTrue();
|
|
459
|
+
});
|
|
460
|
+
});
|
|
461
|
+
```
|
|
462
|
+
|
|
463
|
+
### Testing Reactive Forms
|
|
464
|
+
|
|
465
|
+
```typescript
|
|
466
|
+
describe('ReactiveForm', () => {
|
|
467
|
+
it('should validate form', () => {
|
|
468
|
+
const fixture = TestBed.createComponent(ProfileForm);
|
|
469
|
+
const component = fixture.componentInstance;
|
|
470
|
+
|
|
471
|
+
expect(component.form.valid).toBeFalse();
|
|
472
|
+
|
|
473
|
+
component.form.patchValue({
|
|
474
|
+
name: 'John',
|
|
475
|
+
email: 'john@example.com',
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
expect(component.form.valid).toBeTrue();
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
it('should show validation errors', () => {
|
|
482
|
+
const fixture = TestBed.createComponent(ProfileForm);
|
|
483
|
+
fixture.detectChanges();
|
|
484
|
+
|
|
485
|
+
const emailControl = fixture.componentInstance.form.controls.email;
|
|
486
|
+
emailControl.setValue('invalid');
|
|
487
|
+
emailControl.markAsTouched();
|
|
488
|
+
fixture.detectChanges();
|
|
489
|
+
|
|
490
|
+
const errorElement = fixture.nativeElement.querySelector('.error');
|
|
491
|
+
expect(errorElement.textContent).toContain('Invalid email');
|
|
492
|
+
});
|
|
493
|
+
});
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
## Testing Directives
|
|
497
|
+
|
|
498
|
+
### Attribute Directive
|
|
499
|
+
|
|
500
|
+
```typescript
|
|
501
|
+
@Directive({
|
|
502
|
+
selector: '[appHighlight]',
|
|
503
|
+
host: {
|
|
504
|
+
'[style.backgroundColor]': 'color()',
|
|
505
|
+
},
|
|
506
|
+
})
|
|
507
|
+
export class Highlight {
|
|
508
|
+
color = input('yellow', { alias: 'appHighlight' });
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
describe('Highlight', () => {
|
|
512
|
+
@Component({
|
|
513
|
+
imports: [Highlight],
|
|
514
|
+
template: `<p appHighlight="lightblue">Test</p>`,
|
|
515
|
+
})
|
|
516
|
+
class Test {}
|
|
517
|
+
|
|
518
|
+
it('should apply background color', () => {
|
|
519
|
+
const fixture = TestBed.createComponent(Test);
|
|
520
|
+
fixture.detectChanges();
|
|
521
|
+
|
|
522
|
+
const p = fixture.nativeElement.querySelector('p');
|
|
523
|
+
expect(p.style.backgroundColor).toBe('lightblue');
|
|
524
|
+
});
|
|
525
|
+
});
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
### Structural Directive
|
|
529
|
+
|
|
530
|
+
```typescript
|
|
531
|
+
@Directive({
|
|
532
|
+
selector: '[appIf]',
|
|
533
|
+
})
|
|
534
|
+
export class If {
|
|
535
|
+
private templateRef = inject(TemplateRef);
|
|
536
|
+
private viewContainer = inject(ViewContainerRef);
|
|
537
|
+
|
|
538
|
+
condition = input.required<boolean>({ alias: 'appIf' });
|
|
539
|
+
|
|
540
|
+
constructor() {
|
|
541
|
+
effect(() => {
|
|
542
|
+
if (this.condition()) {
|
|
543
|
+
this.viewContainer.createEmbeddedView(this.templateRef);
|
|
544
|
+
} else {
|
|
545
|
+
this.viewContainer.clear();
|
|
546
|
+
}
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
describe('If', () => {
|
|
552
|
+
@Component({
|
|
553
|
+
imports: [If],
|
|
554
|
+
template: `<p *appIf="show()">Visible</p>`,
|
|
555
|
+
})
|
|
556
|
+
class TestCmpt {
|
|
557
|
+
show = signal(false);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
it('should show content when condition is true', () => {
|
|
561
|
+
const fixture = TestBed.createComponent(Test);
|
|
562
|
+
fixture.detectChanges();
|
|
563
|
+
|
|
564
|
+
expect(fixture.nativeElement.querySelector('p')).toBeNull();
|
|
565
|
+
|
|
566
|
+
fixture.componentInstance.show.set(true);
|
|
567
|
+
fixture.detectChanges();
|
|
568
|
+
|
|
569
|
+
expect(fixture.nativeElement.querySelector('p')).toBeTruthy();
|
|
570
|
+
});
|
|
571
|
+
});
|
|
572
|
+
```
|
|
573
|
+
|
|
574
|
+
## Testing Pipes
|
|
575
|
+
|
|
576
|
+
```typescript
|
|
577
|
+
@Pipe({ name: 'truncate' })
|
|
578
|
+
export class Truncate implements PipeTransform {
|
|
579
|
+
transform(value: string, length: number = 50): string {
|
|
580
|
+
if (value.length <= length) return value;
|
|
581
|
+
return value.substring(0, length) + '...';
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
describe('Truncate', () => {
|
|
586
|
+
let pipe: Truncate;
|
|
587
|
+
|
|
588
|
+
beforeEach(() => {
|
|
589
|
+
pipe = new Truncate();
|
|
590
|
+
});
|
|
591
|
+
|
|
592
|
+
it('should not truncate short strings', () => {
|
|
593
|
+
expect(pipe.transform('Hello', 10)).toBe('Hello');
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
it('should truncate long strings', () => {
|
|
597
|
+
expect(pipe.transform('Hello World', 5)).toBe('Hello...');
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
it('should use default length', () => {
|
|
601
|
+
const longString = 'a'.repeat(60);
|
|
602
|
+
const result = pipe.transform(longString);
|
|
603
|
+
expect(result.length).toBe(53); // 50 + '...'
|
|
604
|
+
});
|
|
605
|
+
});
|
|
606
|
+
```
|
|
607
|
+
|
|
608
|
+
## E2E Testing Setup
|
|
609
|
+
|
|
610
|
+
### Playwright Configuration
|
|
611
|
+
|
|
612
|
+
```typescript
|
|
613
|
+
// playwright.config.ts
|
|
614
|
+
import { defineConfig } from '@playwright/test';
|
|
615
|
+
|
|
616
|
+
export default defineConfig({
|
|
617
|
+
testDir: './e2e',
|
|
618
|
+
fullyParallel: true,
|
|
619
|
+
forbidOnly: !!process.env.CI,
|
|
620
|
+
retries: process.env.CI ? 2 : 0,
|
|
621
|
+
workers: process.env.CI ? 1 : undefined,
|
|
622
|
+
reporter: 'html',
|
|
623
|
+
use: {
|
|
624
|
+
baseURL: 'http://localhost:4200',
|
|
625
|
+
trace: 'on-first-retry',
|
|
626
|
+
},
|
|
627
|
+
webServer: {
|
|
628
|
+
command: 'npm run start',
|
|
629
|
+
url: 'http://localhost:4200',
|
|
630
|
+
reuseExistingServer: !process.env.CI,
|
|
631
|
+
},
|
|
632
|
+
});
|
|
633
|
+
```
|
|
634
|
+
|
|
635
|
+
### E2E Test Example
|
|
636
|
+
|
|
637
|
+
```typescript
|
|
638
|
+
// e2e/login.spec.ts
|
|
639
|
+
import { test, expect } from '@playwright/test';
|
|
640
|
+
|
|
641
|
+
test.describe('Login', () => {
|
|
642
|
+
test('should login successfully', async ({ page }) => {
|
|
643
|
+
await page.goto('/login');
|
|
644
|
+
|
|
645
|
+
await page.fill('input[name="email"]', 'test@example.com');
|
|
646
|
+
await page.fill('input[name="password"]', 'password123');
|
|
647
|
+
await page.click('button[type="submit"]');
|
|
648
|
+
|
|
649
|
+
await expect(page).toHaveURL('/dashboard');
|
|
650
|
+
await expect(page.locator('h1')).toContainText('Welcome');
|
|
651
|
+
});
|
|
652
|
+
|
|
653
|
+
test('should show error for invalid credentials', async ({ page }) => {
|
|
654
|
+
await page.goto('/login');
|
|
655
|
+
|
|
656
|
+
await page.fill('input[name="email"]', 'wrong@example.com');
|
|
657
|
+
await page.fill('input[name="password"]', 'wrongpassword');
|
|
658
|
+
await page.click('button[type="submit"]');
|
|
659
|
+
|
|
660
|
+
await expect(page.locator('.error')).toBeVisible();
|
|
661
|
+
await expect(page.locator('.error')).toContainText('Invalid credentials');
|
|
662
|
+
});
|
|
663
|
+
});
|
|
664
|
+
```
|
|
665
|
+
|
|
666
|
+
## Test Utilities
|
|
667
|
+
|
|
668
|
+
### Custom Test Helpers
|
|
669
|
+
|
|
670
|
+
```typescript
|
|
671
|
+
// test-utils.ts
|
|
672
|
+
export function setSignalInput<T>(
|
|
673
|
+
fixture: ComponentFixture<any>,
|
|
674
|
+
inputName: string,
|
|
675
|
+
value: T
|
|
676
|
+
): void {
|
|
677
|
+
fixture.componentRef.setInput(inputName, value);
|
|
678
|
+
fixture.detectChanges();
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
export async function waitForSignal<T>(
|
|
682
|
+
signal: () => T,
|
|
683
|
+
predicate: (value: T) => boolean,
|
|
684
|
+
timeout = 5000
|
|
685
|
+
): Promise<T> {
|
|
686
|
+
const start = Date.now();
|
|
687
|
+
while (Date.now() - start < timeout) {
|
|
688
|
+
const value = signal();
|
|
689
|
+
if (predicate(value)) return value;
|
|
690
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
691
|
+
}
|
|
692
|
+
throw new Error('Timeout waiting for signal');
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
// Usage
|
|
696
|
+
it('should load data', async () => {
|
|
697
|
+
const fixture = TestBed.createComponent(Data);
|
|
698
|
+
fixture.detectChanges();
|
|
699
|
+
|
|
700
|
+
await waitForSignal(
|
|
701
|
+
() => fixture.componentInstance.data(),
|
|
702
|
+
data => data !== undefined
|
|
703
|
+
);
|
|
704
|
+
|
|
705
|
+
expect(fixture.componentInstance.data()).toBeDefined();
|
|
706
|
+
});
|
|
707
|
+
```
|