|
6 | 6 | * found in the LICENSE file at https://angular.dev/license
|
7 | 7 | */
|
8 | 8 |
|
| 9 | +import {HttpErrorResponse, provideHttpClient} from '@angular/common/http'; |
| 10 | +import {HttpTestingController, provideHttpClientTesting} from '@angular/common/http/testing'; |
9 | 11 | import {TestBed} from '@angular/core/testing';
|
10 |
| - |
| 12 | +import {DocContent} from '@angular/docs'; |
11 | 13 | import {ContentLoader} from './content-loader.service';
|
12 |
| -import {HttpClientTestingModule} from '@angular/common/http/testing'; |
13 | 14 |
|
14 | 15 | describe('ContentLoader', () => {
|
15 | 16 | let service: ContentLoader;
|
| 17 | + let httpTestingController: HttpTestingController; |
16 | 18 |
|
17 | 19 | beforeEach(() => {
|
18 | 20 | TestBed.configureTestingModule({
|
19 |
| - providers: [ContentLoader], |
20 |
| - imports: [HttpClientTestingModule], |
| 21 | + providers: [ContentLoader, provideHttpClient(), provideHttpClientTesting()], |
21 | 22 | });
|
| 23 | + |
22 | 24 | service = TestBed.inject(ContentLoader);
|
| 25 | + httpTestingController = TestBed.inject(HttpTestingController); |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(() => { |
| 29 | + httpTestingController.verify(); |
23 | 30 | });
|
24 | 31 |
|
25 |
| - it('should be created', () => { |
26 |
| - expect(service).toBeTruthy(); |
| 32 | + describe('getContent', () => { |
| 33 | + it('should fetch content successfully', async () => { |
| 34 | + const path = 'test-document.md.html'; |
| 35 | + const mockContent = '<h1>Test Document</h1>'; |
| 36 | + const expectedDocContent: DocContent = { |
| 37 | + contents: mockContent, |
| 38 | + id: path, |
| 39 | + }; |
| 40 | + |
| 41 | + const contentPromise = service.getContent(path); |
| 42 | + |
| 43 | + const req = httpTestingController.expectOne(`assets/content/${path}`); |
| 44 | + expect(req.request.method).toEqual('GET'); |
| 45 | + expect(req.request.responseType).toEqual('text'); |
| 46 | + req.flush(mockContent); |
| 47 | + |
| 48 | + const result = await contentPromise; |
| 49 | + expect(result).toEqual(expectedDocContent); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should add .md.html extension when path has no extension', async () => { |
| 53 | + const path = 'test-document'; |
| 54 | + const expectedPath = 'test-document.md.html'; |
| 55 | + const mockContent = '<h1>Test Document</h1>'; |
| 56 | + |
| 57 | + const contentPromise = service.getContent(path); |
| 58 | + |
| 59 | + const req = httpTestingController.expectOne(`assets/content/${expectedPath}`); |
| 60 | + expect(req.request.method).toEqual('GET'); |
| 61 | + req.flush(mockContent); |
| 62 | + |
| 63 | + const result = await contentPromise; |
| 64 | + expect(result.id).toEqual(expectedPath); |
| 65 | + expect(result.contents).toEqual(mockContent); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should not modify path when it already has an extension', async () => { |
| 69 | + const path = 'test-document.html'; |
| 70 | + const mockContent = '<h1>Test Document</h1>'; |
| 71 | + |
| 72 | + const contentPromise = service.getContent(path); |
| 73 | + |
| 74 | + const req = httpTestingController.expectOne(`assets/content/${path}`); |
| 75 | + req.flush(mockContent); |
| 76 | + |
| 77 | + const result = await contentPromise; |
| 78 | + expect(result.id).toEqual(path); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should cache content and return cached version on subsequent calls', async () => { |
| 82 | + const path = 'cached-document.md.html'; |
| 83 | + const mockContent = '<h1>Cached Document</h1>'; |
| 84 | + |
| 85 | + // First call |
| 86 | + const firstCallPromise = service.getContent(path); |
| 87 | + const req1 = httpTestingController.expectOne(`assets/content/${path}`); |
| 88 | + req1.flush(mockContent); |
| 89 | + const firstResult = await firstCallPromise; |
| 90 | + |
| 91 | + // Second call - should use cache, no HTTP request |
| 92 | + const secondResult = await service.getContent(path); |
| 93 | + |
| 94 | + expect(firstResult).toEqual(secondResult); |
| 95 | + expect(firstResult.contents).toEqual(mockContent); |
| 96 | + httpTestingController.expectNone(`assets/content/${path}`); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should handle 404 errors and keep them in cache', async () => { |
| 100 | + const path = 'non-existent.md.html'; |
| 101 | + |
| 102 | + const contentPromise = service.getContent(path); |
| 103 | + |
| 104 | + const req = httpTestingController.expectOne(`assets/content/${path}`); |
| 105 | + req.flush('Not Found', {status: 404, statusText: 'Not Found'}); |
| 106 | + |
| 107 | + await expectAsync(contentPromise).toBeRejectedWithError(HttpErrorResponse); |
| 108 | + |
| 109 | + // Second call should also fail without making another HTTP request |
| 110 | + // because 404 errors are cached |
| 111 | + await expectAsync(service.getContent(path)).toBeRejectedWithError(HttpErrorResponse); |
| 112 | + httpTestingController.expectNone(`assets/content/${path}`); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should not cache non-404 errors and retry on subsequent calls', async () => { |
| 116 | + const path = 'server-error.md.html'; |
| 117 | + |
| 118 | + // First call - server error |
| 119 | + const firstCallPromise = service.getContent(path); |
| 120 | + const req1 = httpTestingController.expectOne(`assets/content/${path}`); |
| 121 | + req1.flush('Server Error', {status: 500, statusText: 'Internal Server Error'}); |
| 122 | + |
| 123 | + await expectAsync(firstCallPromise).toBeRejectedWithError(HttpErrorResponse); |
| 124 | + |
| 125 | + // Second call should make another HTTP request since 500 errors are not cached |
| 126 | + const secondCallPromise = service.getContent(path); |
| 127 | + const req2 = httpTestingController.expectOne(`assets/content/${path}`); |
| 128 | + req2.flush('Success content'); |
| 129 | + |
| 130 | + const result = await secondCallPromise; |
| 131 | + expect(result.contents).toEqual('Success content'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should handle network errors and not cache them', async () => { |
| 135 | + const path = 'network-error.md.html'; |
| 136 | + |
| 137 | + // First call - network error |
| 138 | + const firstCallPromise = service.getContent(path); |
| 139 | + const req1 = httpTestingController.expectOne(`assets/content/${path}`); |
| 140 | + req1.error(new ProgressEvent('Network error')); |
| 141 | + |
| 142 | + await expectAsync(firstCallPromise).toBeRejected(); |
| 143 | + |
| 144 | + // Second call should make another HTTP request since network errors are not cached |
| 145 | + const secondCallPromise = service.getContent(path); |
| 146 | + const req2 = httpTestingController.expectOne(`assets/content/${path}`); |
| 147 | + req2.flush('Success after network error'); |
| 148 | + |
| 149 | + const result = await secondCallPromise; |
| 150 | + expect(result.contents).toEqual('Success after network error'); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should handle paths with different extensions correctly', async () => { |
| 154 | + const testCases = [ |
| 155 | + {input: 'document.txt', expected: 'document.txt'}, |
| 156 | + {input: 'document.json', expected: 'document.json'}, |
| 157 | + {input: 'document.xml', expected: 'document.xml'}, |
| 158 | + {input: 'document', expected: 'document.md.html'}, |
| 159 | + ]; |
| 160 | + |
| 161 | + for (const testCase of testCases) { |
| 162 | + const mockContent = `Content for ${testCase.input}`; |
| 163 | + |
| 164 | + const contentPromise = service.getContent(testCase.input); |
| 165 | + |
| 166 | + const req = httpTestingController.expectOne(`assets/content/${testCase.expected}`); |
| 167 | + req.flush(mockContent); |
| 168 | + |
| 169 | + const result = await contentPromise; |
| 170 | + expect(result.id).toEqual(testCase.expected); |
| 171 | + expect(result.contents).toEqual(mockContent); |
| 172 | + } |
| 173 | + }); |
27 | 174 | });
|
28 | 175 | });
|
0 commit comments