refactor(ivy): avoid code duplication in `ngcc` tests (#28963)

PR Close #28963
This commit is contained in:
George Kalpakas 2019-03-18 16:24:56 +02:00 committed by Matias Niemelä
parent a8d84660e5
commit c439e14d39
1 changed files with 33 additions and 43 deletions

View File

@ -92,8 +92,9 @@ describe('DecorationAnalyzer', () => {
let testHandler: jasmine.SpyObj<DecoratorHandler<any, any>>; let testHandler: jasmine.SpyObj<DecoratorHandler<any, any>>;
let result: DecorationAnalyses; let result: DecorationAnalyses;
beforeEach(() => { // Helpers
const {options, host, ...bundle} = makeTestBundleProgram([TEST_PROGRAM]); const setUpAndAnalyzeProgram = (...progArgs: Parameters<typeof makeTestBundleProgram>) => {
const {options, host, ...bundle} = makeTestBundleProgram(...progArgs);
program = bundle.program; program = bundle.program;
const reflectionHost = new Esm2015ReflectionHost(false, program.getTypeChecker()); const reflectionHost = new Esm2015ReflectionHost(false, program.getTypeChecker());
@ -104,50 +105,48 @@ describe('DecorationAnalyzer', () => {
testHandler = createTestHandler(); testHandler = createTestHandler();
analyzer.handlers = [testHandler]; analyzer.handlers = [testHandler];
result = analyzer.analyzeProgram(); result = analyzer.analyzeProgram();
}); };
it('should return an object containing a reference to the original source file', () => { describe('basic usage', () => {
const file = program.getSourceFile(TEST_PROGRAM.name) !; beforeEach(() => setUpAndAnalyzeProgram([TEST_PROGRAM]));
expect(result.get(file) !.sourceFile).toBe(file);
});
it('should call detect on the decorator handlers with each class from the parsed file', () => { it('should return an object containing a reference to the original source file', () => {
expect(testHandler.detect).toHaveBeenCalledTimes(2); const file = program.getSourceFile(TEST_PROGRAM.name) !;
expect(testHandler.detect.calls.allArgs()[0][1]).toEqual([jasmine.objectContaining( expect(result.get(file) !.sourceFile).toBe(file);
{name: 'Component'})]); });
expect(testHandler.detect.calls.allArgs()[1][1]).toEqual([jasmine.objectContaining(
{name: 'Injectable'})]);
});
it('should return an object containing the classes that were analyzed', () => { it('should call detect on the decorator handlers with each class from the parsed file',
const file = program.getSourceFile(TEST_PROGRAM.name) !; () => {
const compiledFile = result.get(file) !; expect(testHandler.detect).toHaveBeenCalledTimes(2);
expect(compiledFile.compiledClasses.length).toEqual(1); expect(testHandler.detect.calls.allArgs()[0][1]).toEqual([jasmine.objectContaining(
expect(compiledFile.compiledClasses[0].name).toEqual('MyComponent'); {name: 'Component'})]);
}); expect(testHandler.detect.calls.allArgs()[1][1]).toEqual([jasmine.objectContaining(
{name: 'Injectable'})]);
});
it('should analyze and compile the classes that are detected', () => { it('should return an object containing the classes that were analyzed', () => {
expect(testHandler.analyze).toHaveBeenCalledTimes(1); const file = program.getSourceFile(TEST_PROGRAM.name) !;
expect(testHandler.analyze.calls.allArgs()[0][1].name).toEqual('Component'); const compiledFile = result.get(file) !;
expect(compiledFile.compiledClasses.length).toEqual(1);
expect(compiledFile.compiledClasses[0].name).toEqual('MyComponent');
});
expect(testHandler.compile).toHaveBeenCalledTimes(1); it('should analyze and compile the classes that are detected', () => {
expect(testHandler.compile.calls.allArgs()[0][1]).toEqual('Component'); expect(testHandler.analyze).toHaveBeenCalledTimes(1);
expect(testHandler.analyze.calls.allArgs()[0][1].name).toEqual('Component');
expect(testHandler.compile).toHaveBeenCalledTimes(1);
expect(testHandler.compile.calls.allArgs()[0][1]).toEqual('Component');
});
}); });
describe('internal components', () => { describe('internal components', () => {
beforeEach(() => setUpAndAnalyzeProgram(INTERNAL_COMPONENT_PROGRAM));
// The problem of exposing the type of these internal components in the .d.ts typing files // The problem of exposing the type of these internal components in the .d.ts typing files
// is not yet solved. // is not yet solved.
it('should analyze an internally imported component, which is not publicly exported from the entry-point', it('should analyze an internally imported component, which is not publicly exported from the entry-point',
() => { () => {
const {program, options, host} = makeTestBundleProgram(INTERNAL_COMPONENT_PROGRAM);
const reflectionHost = new Esm2015ReflectionHost(false, program.getTypeChecker());
const referencesRegistry = new NgccReferencesRegistry(reflectionHost);
const analyzer = new DecorationAnalyzer(
program, options, host, program.getTypeChecker(), reflectionHost, referencesRegistry,
[AbsoluteFsPath.fromUnchecked('/')], false);
const testHandler = createTestHandler();
analyzer.handlers = [testHandler];
const result = analyzer.analyzeProgram();
const file = program.getSourceFile('component.js') !; const file = program.getSourceFile('component.js') !;
const analysis = result.get(file) !; const analysis = result.get(file) !;
expect(analysis).toBeDefined(); expect(analysis).toBeDefined();
@ -157,15 +156,6 @@ describe('DecorationAnalyzer', () => {
}); });
it('should analyze an internally defined component, which is not exported at all', () => { it('should analyze an internally defined component, which is not exported at all', () => {
const {program, options, host} = makeTestBundleProgram(INTERNAL_COMPONENT_PROGRAM);
const reflectionHost = new Esm2015ReflectionHost(false, program.getTypeChecker());
const referencesRegistry = new NgccReferencesRegistry(reflectionHost);
const analyzer = new DecorationAnalyzer(
program, options, host, program.getTypeChecker(), reflectionHost, referencesRegistry,
[AbsoluteFsPath.fromUnchecked('/')], false);
const testHandler = createTestHandler();
analyzer.handlers = [testHandler];
const result = analyzer.analyzeProgram();
const file = program.getSourceFile('entrypoint.js') !; const file = program.getSourceFile('entrypoint.js') !;
const analysis = result.get(file) !; const analysis = result.get(file) !;
expect(analysis).toBeDefined(); expect(analysis).toBeDefined();