refactor(ngcc): share code between CommonJsReflectionHost and UmdReflectionHost (#34512)

While different, CommonJS and UMD have a lot in common regarding the
their exports are constructed. Therefore, there was some code
duplication between `CommonJsReflectionHost` and `UmdReflectionHost`.

This commit extracts some of the common bits into a separate file as
helpers to allow reusing the code in both `ReflectionHost`s.

PR Close #34512
This commit is contained in:
George Kalpakas
2019-12-20 15:38:11 +02:00
committed by Alex Rickabaugh
parent d5fd742763
commit 17d5e2bc99
7 changed files with 179 additions and 159 deletions

View File

@ -6,7 +6,31 @@
* found in the LICENSE file at https://angular.io/license
*/
import {isRelativePath} from '../src/utils';
import {getOrDefault, isRelativePath, stripExtension} from '../src/utils';
describe('getOrDefault()', () => {
it('should return an existing value', () => {
const map = new Map([['k1', 'v1'], ['k2', 'v2']]);
const factorySpy = jasmine.createSpy('factory');
expect(getOrDefault(map, 'k1', factorySpy)).toBe('v1');
expect(getOrDefault(map, 'k2', factorySpy)).toBe('v2');
expect(factorySpy).not.toHaveBeenCalled();
});
it('should create, store and return the value if it does not exist', () => {
const map = new Map([['k1', 'v1'], ['k2', 'v2']]);
const factorySpy = jasmine.createSpy('factory').and.returnValues('v3', 'never gonna happen');
expect(getOrDefault(map, 'k3', factorySpy)).toBe('v3');
expect(factorySpy).toHaveBeenCalledTimes(1);
factorySpy.calls.reset();
expect(getOrDefault(map, 'k3', factorySpy)).toBe('v3');
expect(factorySpy).not.toHaveBeenCalled();
});
});
describe('isRelativePath()', () => {
it('should return true for relative paths', () => {
@ -34,3 +58,17 @@ describe('isRelativePath()', () => {
expect(isRelativePath('@abc/xyz')).toBe(false);
});
});
describe('stripExtension()', () => {
it('should strip the extension from a file name', () => {
expect(stripExtension('foo.ts')).toBe('foo');
expect(stripExtension('/foo/bar.ts')).toBe('/foo/bar');
expect(stripExtension('/foo/bar.d.ts')).toBe('/foo/bar');
});
it('should do nothing if there is no extension in a file name', () => {
expect(stripExtension('foo')).toBe('foo');
expect(stripExtension('/foo/bar')).toBe('/foo/bar');
expect(stripExtension('/fo-o/b_ar')).toBe('/fo-o/b_ar');
});
});