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:
George Kalpakas 2019-05-16 18:14:41 +03:00 committed by Jason Aden
parent 98ded949dd
commit 6bf8b1007c
1 changed files with 8 additions and 14 deletions

View File

@ -21,17 +21,6 @@ if (isBrowser) {
setTemplateCache({'test.html': '<div>Hello</div>'});
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', () => {
setTemplateCache(null);
@ -41,13 +30,12 @@ if (isBrowser) {
});
it('should resolve the Promise with the cached file content on success', async(() => {
setTemplateCache({'test.html': '<div>Hello</div>'});
resourceLoader = new CachedResourceLoader();
resourceLoader = createCachedResourceLoader();
resourceLoader.get('test.html').then((text) => { expect(text).toBe('<div>Hello</div>'); });
}));
it('should reject the Promise on failure', async(() => {
resourceLoader = new CachedResourceLoader();
resourceLoader = createCachedResourceLoader();
resourceLoader.get('unknown.html')
.then((text) => { throw new Error('Not expected to succeed.'); })
.catch((error) => {/** success */});
@ -55,6 +43,12 @@ if (isBrowser) {
it('should allow fakeAsync Tests to load components with templateUrl synchronously',
fakeAsync(() => {
TestBed.configureCompiler({
providers: [
{provide: UrlResolver, useClass: TestUrlResolver, deps: []},
{provide: ResourceLoader, useFactory: createCachedResourceLoader, deps: []}
]
});
TestBed.configureTestingModule({declarations: [TestComponent]});
TestBed.compileComponents();
tick();