Skip to content

Commit c6b6e71

Browse files
committed
docs(docs-infra): remove deprecated module and add test cases
1 parent bbb46e5 commit c6b6e71

File tree

1 file changed

+153
-6
lines changed

1 file changed

+153
-6
lines changed

adev/src/app/core/services/content-loader.service.spec.ts

Lines changed: 153 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,170 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import {HttpErrorResponse, provideHttpClient} from '@angular/common/http';
10+
import {HttpTestingController, provideHttpClientTesting} from '@angular/common/http/testing';
911
import {TestBed} from '@angular/core/testing';
10-
12+
import {DocContent} from '@angular/docs';
1113
import {ContentLoader} from './content-loader.service';
12-
import {HttpClientTestingModule} from '@angular/common/http/testing';
1314

1415
describe('ContentLoader', () => {
1516
let service: ContentLoader;
17+
let httpTestingController: HttpTestingController;
1618

1719
beforeEach(() => {
1820
TestBed.configureTestingModule({
19-
providers: [ContentLoader],
20-
imports: [HttpClientTestingModule],
21+
providers: [ContentLoader, provideHttpClient(), provideHttpClientTesting()],
2122
});
23+
2224
service = TestBed.inject(ContentLoader);
25+
httpTestingController = TestBed.inject(HttpTestingController);
26+
});
27+
28+
afterEach(() => {
29+
httpTestingController.verify();
2330
});
2431

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+
});
27174
});
28175
});

0 commit comments

Comments
 (0)