2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-06-23 12:47:54 -04:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2017-03-02 15:12:46 -05:00
|
|
|
import {MockResourceLoader} from '@angular/compiler/testing/src/resource_loader_mock';
|
2015-01-30 03:43:21 -05:00
|
|
|
|
2017-12-16 17:42:55 -05:00
|
|
|
{
|
2016-08-17 12:24:44 -04:00
|
|
|
describe('MockResourceLoader', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let resourceLoader: MockResourceLoader;
|
2015-01-30 03:43:21 -05:00
|
|
|
|
2020-04-08 13:14:18 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
resourceLoader = new MockResourceLoader();
|
|
|
|
});
|
2015-01-30 03:43:21 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
function expectResponse(
|
2020-04-08 13:14:18 -04:00
|
|
|
request: Promise<string>, url: string, response: string, done: () => void = null!) {
|
2015-06-02 20:41:44 -04:00
|
|
|
function onResponse(text: string): string {
|
2015-01-30 03:43:21 -05:00
|
|
|
if (response === null) {
|
|
|
|
throw `Unexpected response ${url} -> ${text}`;
|
|
|
|
} else {
|
|
|
|
expect(text).toEqual(response);
|
2017-03-02 12:37:01 -05:00
|
|
|
if (done != null) done();
|
2015-01-30 03:43:21 -05:00
|
|
|
}
|
2015-06-02 20:41:44 -04:00
|
|
|
return text;
|
2015-01-30 03:43:21 -05:00
|
|
|
}
|
|
|
|
|
2015-06-02 20:41:44 -04:00
|
|
|
function onError(error: string): string {
|
2015-01-30 03:43:21 -05:00
|
|
|
if (response !== null) {
|
|
|
|
throw `Unexpected error ${url}`;
|
|
|
|
} else {
|
|
|
|
expect(error).toEqual(`Failed to load ${url}`);
|
2017-03-02 12:37:01 -05:00
|
|
|
if (done != null) done();
|
2015-01-30 03:43:21 -05:00
|
|
|
}
|
2015-06-02 20:41:44 -04:00
|
|
|
return error;
|
2015-01-30 03:43:21 -05:00
|
|
|
}
|
|
|
|
|
2016-08-02 18:53:34 -04:00
|
|
|
request.then(onResponse, onError);
|
2015-01-30 03:43:21 -05:00
|
|
|
}
|
|
|
|
|
2021-05-23 16:16:02 -04:00
|
|
|
it('should return a response from the definitions', done => {
|
|
|
|
const url = '/foo';
|
|
|
|
const response = 'bar';
|
|
|
|
resourceLoader.when(url, response);
|
|
|
|
expectResponse(resourceLoader.get(url), url, response, () => done());
|
|
|
|
resourceLoader.flush();
|
|
|
|
});
|
2015-01-30 03:43:21 -05:00
|
|
|
|
2021-05-23 16:16:02 -04:00
|
|
|
it('should return an error from the definitions', done => {
|
|
|
|
const url = '/foo';
|
|
|
|
const response: string = null!;
|
|
|
|
resourceLoader.when(url, response);
|
|
|
|
expectResponse(resourceLoader.get(url), url, response, () => done());
|
|
|
|
resourceLoader.flush();
|
|
|
|
});
|
2015-01-30 03:43:21 -05:00
|
|
|
|
2021-05-23 16:16:02 -04:00
|
|
|
it('should return a response from the expectations', done => {
|
|
|
|
const url = '/foo';
|
|
|
|
const response = 'bar';
|
|
|
|
resourceLoader.expect(url, response);
|
|
|
|
expectResponse(resourceLoader.get(url), url, response, () => done());
|
|
|
|
resourceLoader.flush();
|
|
|
|
});
|
2015-01-30 03:43:21 -05:00
|
|
|
|
2021-05-23 16:16:02 -04:00
|
|
|
it('should return an error from the expectations', done => {
|
|
|
|
const url = '/foo';
|
|
|
|
const response: string = null!;
|
|
|
|
resourceLoader.expect(url, response);
|
|
|
|
expectResponse(resourceLoader.get(url), url, response, () => done());
|
|
|
|
resourceLoader.flush();
|
|
|
|
});
|
2015-01-30 03:43:21 -05:00
|
|
|
|
|
|
|
it('should not reuse expectations', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const url = '/foo';
|
|
|
|
const response = 'bar';
|
2016-08-17 12:24:44 -04:00
|
|
|
resourceLoader.expect(url, response);
|
|
|
|
resourceLoader.get(url);
|
|
|
|
resourceLoader.get(url);
|
2020-04-08 13:14:18 -04:00
|
|
|
expect(() => {
|
|
|
|
resourceLoader.flush();
|
|
|
|
}).toThrowError('Unexpected request /foo');
|
2015-01-30 03:43:21 -05:00
|
|
|
});
|
|
|
|
|
2021-05-23 16:16:02 -04:00
|
|
|
it('should return expectations before definitions', done => {
|
|
|
|
const url = '/foo';
|
|
|
|
resourceLoader.when(url, 'when');
|
|
|
|
resourceLoader.expect(url, 'expect');
|
|
|
|
expectResponse(resourceLoader.get(url), url, 'expect');
|
|
|
|
expectResponse(resourceLoader.get(url), url, 'when', () => done());
|
|
|
|
resourceLoader.flush();
|
|
|
|
});
|
2015-01-30 03:43:21 -05:00
|
|
|
|
|
|
|
it('should throw when there is no definitions or expectations', () => {
|
2016-08-17 12:24:44 -04:00
|
|
|
resourceLoader.get('/foo');
|
2020-04-08 13:14:18 -04:00
|
|
|
expect(() => {
|
|
|
|
resourceLoader.flush();
|
|
|
|
}).toThrowError('Unexpected request /foo');
|
2015-01-30 03:43:21 -05:00
|
|
|
});
|
|
|
|
|
2016-08-17 12:24:44 -04:00
|
|
|
it('should throw when flush is called without any pending requests', () => {
|
2020-04-08 13:14:18 -04:00
|
|
|
expect(() => {
|
|
|
|
resourceLoader.flush();
|
|
|
|
}).toThrowError('No pending requests to flush');
|
2016-08-17 12:24:44 -04:00
|
|
|
});
|
2015-01-30 03:43:21 -05:00
|
|
|
|
2015-12-16 02:47:48 -05:00
|
|
|
it('should throw on unsatisfied expectations', () => {
|
2016-08-17 12:24:44 -04:00
|
|
|
resourceLoader.expect('/foo', 'bar');
|
|
|
|
resourceLoader.when('/bar', 'foo');
|
|
|
|
resourceLoader.get('/bar');
|
2020-04-08 13:14:18 -04:00
|
|
|
expect(() => {
|
|
|
|
resourceLoader.flush();
|
|
|
|
}).toThrowError('Unsatisfied requests: /foo');
|
2015-01-30 03:43:21 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|