test(platform-browser-dynamic): make `CachedResourceLoader` tests more reliable (#30515)
Previously, [this test][1] would occasionally fail (e.g. on CI) with "Template cache was not found in $templateCache". This was due to a combination of: 1. [That test][2] (which removes the cache) being run right before the failing test. 2. The async `TestBed.compileComponents()` operation run in the `beforeEach()` block (which sets the cache) not having completed before the `it()` block. This commit fixes the issue by ensuring the cache is always set, before instantiating `CachedResourceLoader`. This commit also moves some operations that are only needed in one test from the `beforeEach()` block to that test's `it()` block. [1]:79903b1842/packages/platform-browser-dynamic/test/resource_loader/resource_loader_cache_spec.ts (L50)
[2]:79903b1842/packages/platform-browser-dynamic/test/resource_loader/resource_loader_cache_spec.ts (L37)
Fixes #30499 PR Close #30515
This commit is contained in:
parent
98ded949dd
commit
6bf8b1007c
|
@ -21,17 +21,6 @@ if (isBrowser) {
|
||||||
setTemplateCache({'test.html': '<div>Hello</div>'});
|
setTemplateCache({'test.html': '<div>Hello</div>'});
|
||||||
return new CachedResourceLoader();
|
return new CachedResourceLoader();
|
||||||
}
|
}
|
||||||
beforeEach(fakeAsync(() => {
|
|
||||||
TestBed.configureCompiler({
|
|
||||||
providers: [
|
|
||||||
{provide: UrlResolver, useClass: TestUrlResolver, deps: []},
|
|
||||||
{provide: ResourceLoader, useFactory: createCachedResourceLoader, deps: []}
|
|
||||||
]
|
|
||||||
});
|
|
||||||
|
|
||||||
TestBed.configureTestingModule({declarations: [TestComponent]});
|
|
||||||
TestBed.compileComponents();
|
|
||||||
}));
|
|
||||||
|
|
||||||
it('should throw exception if $templateCache is not found', () => {
|
it('should throw exception if $templateCache is not found', () => {
|
||||||
setTemplateCache(null);
|
setTemplateCache(null);
|
||||||
|
@ -41,13 +30,12 @@ if (isBrowser) {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should resolve the Promise with the cached file content on success', async(() => {
|
it('should resolve the Promise with the cached file content on success', async(() => {
|
||||||
setTemplateCache({'test.html': '<div>Hello</div>'});
|
resourceLoader = createCachedResourceLoader();
|
||||||
resourceLoader = new CachedResourceLoader();
|
|
||||||
resourceLoader.get('test.html').then((text) => { expect(text).toBe('<div>Hello</div>'); });
|
resourceLoader.get('test.html').then((text) => { expect(text).toBe('<div>Hello</div>'); });
|
||||||
}));
|
}));
|
||||||
|
|
||||||
it('should reject the Promise on failure', async(() => {
|
it('should reject the Promise on failure', async(() => {
|
||||||
resourceLoader = new CachedResourceLoader();
|
resourceLoader = createCachedResourceLoader();
|
||||||
resourceLoader.get('unknown.html')
|
resourceLoader.get('unknown.html')
|
||||||
.then((text) => { throw new Error('Not expected to succeed.'); })
|
.then((text) => { throw new Error('Not expected to succeed.'); })
|
||||||
.catch((error) => {/** success */});
|
.catch((error) => {/** success */});
|
||||||
|
@ -55,6 +43,12 @@ if (isBrowser) {
|
||||||
|
|
||||||
it('should allow fakeAsync Tests to load components with templateUrl synchronously',
|
it('should allow fakeAsync Tests to load components with templateUrl synchronously',
|
||||||
fakeAsync(() => {
|
fakeAsync(() => {
|
||||||
|
TestBed.configureCompiler({
|
||||||
|
providers: [
|
||||||
|
{provide: UrlResolver, useClass: TestUrlResolver, deps: []},
|
||||||
|
{provide: ResourceLoader, useFactory: createCachedResourceLoader, deps: []}
|
||||||
|
]
|
||||||
|
});
|
||||||
TestBed.configureTestingModule({declarations: [TestComponent]});
|
TestBed.configureTestingModule({declarations: [TestComponent]});
|
||||||
TestBed.compileComponents();
|
TestBed.compileComponents();
|
||||||
tick();
|
tick();
|
||||||
|
|
Loading…
Reference in New Issue