-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-runner.mjs
More file actions
433 lines (367 loc) · 12 KB
/
test-runner.mjs
File metadata and controls
433 lines (367 loc) · 12 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// Simple test runner for our Angular components
// This will run basic unit tests without the full Angular testing framework
import { signal } from '@angular/core';
import { of, throwError } from 'rxjs';
// Mock implementations
const mockSportsService = {
getLeagues: () => of([
{
idLeague: '1',
strLeague: 'Premier League',
strSport: 'Soccer',
strLeagueAlternate: 'EPL'
}
]),
getSeasonBadge: (id) => of('https://example.com/badge.png')
};
// Test AppComponent signals and computed values
function testAppComponent() {
console.log('Testing AppComponent...');
// Create mock component instance
const component = {
searchTerm: signal(''),
selectedSport: signal('All'),
leagues: signal([
{
idLeague: '1',
strLeague: 'Premier League',
strSport: 'Soccer',
strLeagueAlternate: 'EPL'
},
{
idLeague: '2',
strLeague: 'NBA',
strSport: 'Basketball',
strLeagueAlternate: 'National Basketball Association'
}
]),
uniqueSports: function() {
const sports = this.leagues().map(league => league.strSport);
return [...new Set(sports)].sort();
},
filteredLeagues: function() {
const leagues = this.leagues();
const search = this.searchTerm().toLowerCase();
const sport = this.selectedSport();
return leagues.filter(league => {
const nameMatch = league.strLeague.toLowerCase().includes(search) ||
(league.strLeagueAlternate && league.strLeagueAlternate.toLowerCase().includes(search));
const sportMatch = sport === 'All' || league.strSport === sport;
return nameMatch && sportMatch;
});
},
onSearchChange: function(term) {
this.searchTerm.set(term);
},
onSportChange: function(sport) {
this.selectedSport.set(sport);
}
};
// Test 1: Initial state
console.log('✓ Test 1: Initial search term should be empty');
if (component.searchTerm() !== '') {
throw new Error('Initial search term should be empty');
}
// Test 2: Unique sports computation
console.log('✓ Test 2: Unique sports computation');
const uniqueSports = component.uniqueSports();
if (uniqueSports.length !== 2 || !uniqueSports.includes('Soccer') || !uniqueSports.includes('Basketball')) {
throw new Error('Unique sports computation failed');
}
// Test 3: Filtering by search term
console.log('✓ Test 3: Filtering by search term');
component.onSearchChange('Premier');
const filtered = component.filteredLeagues();
if (filtered.length !== 1 || filtered[0].strLeague !== 'Premier League') {
throw new Error('Search filtering failed');
}
// Test 4: Filtering by sport
console.log('✓ Test 4: Filtering by sport');
component.onSearchChange(''); // Reset search
component.onSportChange('Basketball');
const basketballLeagues = component.filteredLeagues();
if (basketballLeagues.length !== 1 || basketballLeagues[0].strSport !== 'Basketball') {
throw new Error('Sport filtering failed');
}
// Test 5: Case insensitive search
console.log('✓ Test 5: Case insensitive search');
component.onSportChange('All'); // Reset sport filter
component.onSearchChange('premier');
const caseInsensitive = component.filteredLeagues();
if (caseInsensitive.length !== 1 || caseInsensitive[0].strLeague !== 'Premier League') {
throw new Error('Case insensitive search failed');
}
console.log('✅ AppComponent tests passed!');
}
// Test SearchFilterComponent behavior
function testSearchFilterComponent() {
console.log('Testing SearchFilterComponent...');
const mockSports = ['Soccer', 'Basketball', 'Tennis'];
let emittedSearchValue = '';
let emittedSportValue = '';
const component = {
sports: signal(mockSports),
searchChange: {
emit: (value) => { emittedSearchValue = value; }
},
sportChange: {
emit: (value) => { emittedSportValue = value; }
},
onSearchChange: function(event) {
const value = event.target.value;
this.searchChange.emit(value);
},
onSportChange: function(event) {
const value = event.target.value;
this.sportChange.emit(value);
}
};
// Test 1: Sports input
console.log('✓ Test 1: Sports input signal');
if (component.sports().length !== 3) {
throw new Error('Sports input failed');
}
// Test 2: Search change emission
console.log('✓ Test 2: Search change emission');
const mockSearchEvent = { target: { value: 'Premier League' } };
component.onSearchChange(mockSearchEvent);
if (emittedSearchValue !== 'Premier League') {
throw new Error('Search change emission failed');
}
// Test 3: Sport change emission
console.log('✓ Test 3: Sport change emission');
const mockSportEvent = { target: { value: 'Soccer' } };
component.onSportChange(mockSportEvent);
if (emittedSportValue !== 'Soccer') {
throw new Error('Sport change emission failed');
}
console.log('✅ SearchFilterComponent tests passed!');
}
// Test LeagueListComponent behavior
function testLeagueListComponent() {
console.log('Testing LeagueListComponent...');
const mockLeagues = [
{
idLeague: '1',
strLeague: 'Premier League',
strSport: 'Soccer',
strLeagueAlternate: 'EPL'
},
{
idLeague: '2',
strLeague: 'La Liga',
strSport: 'Soccer',
strLeagueAlternate: 'Spanish League'
}
];
const component = {
leagues: signal(mockLeagues),
selectedLeagueId: signal(null),
badgeResource: signal({ state: 'idle' }),
selectLeague: function(id) {
if (this.selectedLeagueId() === id) {
this.selectedLeagueId.set(null); // Toggle off
} else {
this.selectedLeagueId.set(id);
this.badgeResource.set({ state: 'loading' });
// Simulate async badge loading
setTimeout(() => {
this.badgeResource.set({
state: 'loaded',
badgeUrl: 'https://example.com/badge.png'
});
}, 0);
}
}
};
// Test 1: Initial state
console.log('✓ Test 1: Initial state');
if (component.selectedLeagueId() !== null) {
throw new Error('Initial selected league should be null');
}
// Test 2: League selection
console.log('✓ Test 2: League selection');
component.selectLeague('1');
if (component.selectedLeagueId() !== '1') {
throw new Error('League selection failed');
}
// Test 3: League deselection
console.log('✓ Test 3: League deselection');
component.selectLeague('1'); // Click same league again
if (component.selectedLeagueId() !== null) {
throw new Error('League deselection failed');
}
// Test 4: Badge loading state
console.log('✓ Test 4: Badge loading state');
component.selectLeague('1');
if (component.badgeResource().state !== 'loading') {
throw new Error('Badge loading state failed');
}
console.log('✅ LeagueListComponent tests passed!');
}
// Test SportsService caching behavior
function testSportsService() {
console.log('Testing SportsService caching...');
let requestCount = 0;
let cachedData = null;
const mockHttpGet = (url) => {
requestCount++;
return of({
leagues: [
{
idLeague: '1',
strLeague: 'Premier League',
strSport: 'Soccer',
strLeagueAlternate: 'EPL'
}
]
});
};
const service = {
leaguesCache: null,
getLeagues: function() {
if (!this.leaguesCache) {
this.leaguesCache = mockHttpGet('leagues-url').pipe(
map(response => response.leagues || [])
);
}
return this.leaguesCache;
}
};
// Test 1: First call makes HTTP request
console.log('✓ Test 1: First call makes HTTP request');
service.getLeagues().subscribe();
if (requestCount !== 1) {
throw new Error('First call should make HTTP request');
}
// Test 2: Second call uses cache
console.log('✓ Test 2: Second call uses cache');
const initialCount = requestCount;
service.getLeagues().subscribe();
if (requestCount !== initialCount) {
throw new Error('Second call should use cache');
}
console.log('✅ SportsService tests passed!');
}
// Test CacheService functionality
function testCacheService() {
console.log('Testing CacheService...');
const mockStore = new Map();
const service = {
store: mockStore,
set: function(storeName, key, data) {
const entry = {
key,
data,
timestamp: Date.now(),
storeName
};
this.store.set(`${storeName}:${key}`, entry);
return of(void 0);
},
get: function(storeName, key) {
const entry = this.store.get(`${storeName}:${key}`);
if (!entry) return of(null);
// Check TTL (1 hour for leagues)
const ttl = storeName === 'leagues' ? 60 * 60 * 1000 : 24 * 60 * 60 * 1000;
if (Date.now() - entry.timestamp > ttl) {
this.store.delete(`${storeName}:${key}`);
return of(null);
}
return of(entry.data);
},
clear: function(storeName) {
const keysToDelete = [];
for (const [key, entry] of this.store.entries()) {
if (entry.storeName === storeName) {
keysToDelete.push(key);
}
}
keysToDelete.forEach(key => this.store.delete(key));
return of(void 0);
},
cleanup: function() {
let cleanedCount = 0;
const now = Date.now();
for (const [key, entry] of this.store.entries()) {
const ttl = entry.storeName === 'leagues' ? 60 * 60 * 1000 : 24 * 60 * 60 * 1000;
if (now - entry.timestamp > ttl) {
this.store.delete(key);
cleanedCount++;
}
}
return of(cleanedCount);
}
};
// Test 1: Store and retrieve data
console.log('✓ Test 1: Store and retrieve data');
const testData = { name: 'Premier League', sport: 'Soccer' };
service.set('leagues', 'test-key', testData).subscribe();
service.get('leagues', 'test-key').subscribe(result => {
if (!result || result.name !== 'Premier League') {
throw new Error('Store and retrieve failed');
}
});
// Test 2: Return null for non-existent key
console.log('✓ Test 2: Return null for non-existent key');
service.get('leagues', 'non-existent').subscribe(result => {
if (result !== null) {
throw new Error('Should return null for non-existent key');
}
});
// Test 3: Clear cache
console.log('✓ Test 3: Clear cache');
service.clear('leagues').subscribe();
service.get('leagues', 'test-key').subscribe(result => {
if (result !== null) {
throw new Error('Cache clear failed');
}
});
// Test 4: TTL expiration simulation
console.log('✓ Test 4: TTL expiration simulation');
const expiredEntry = {
key: 'expired-key',
data: { name: 'Expired League' },
timestamp: Date.now() - (2 * 60 * 60 * 1000), // 2 hours ago
storeName: 'leagues'
};
service.store.set('leagues:expired-key', expiredEntry);
service.get('leagues', 'expired-key').subscribe(result => {
if (result !== null) {
throw new Error('Expired entry should return null');
}
});
// Test 5: Cleanup expired entries
console.log('✓ Test 5: Cleanup expired entries');
service.cleanup().subscribe(cleanedCount => {
if (cleanedCount < 0) {
throw new Error('Cleanup should return non-negative count');
}
});
console.log('✅ CacheService tests passed!');
}
// Run all tests
async function runAllTests() {
console.log('🚀 Starting unit tests...\n');
try {
testAppComponent();
console.log('');
testSearchFilterComponent();
console.log('');
testLeagueListComponent();
console.log('');
testSportsService();
console.log('');
testCacheService();
console.log('');
console.log('🎉 All tests passed successfully!');
process.exit(0);
} catch (error) {
console.error('❌ Test failed:', error.message);
process.exit(1);
}
}
// Import map function from rxjs
const { map } = await import('rxjs/operators');
// Run the tests
runAllTests();