Skip to content

Commit ed271eb

Browse files
devversiondylhunn
authored andcommitted
test(compiler-cli): add tests to verify import generation in TCB files/blocks (#54983)
This commit adds some unit tests verifying the import generation in TCB files and inline blocks. We don't seem to have any unit tests for these in general. This commit adds some, verifying some characteristics we would like to guarantee. PR Close #54983
1 parent 6734b59 commit ed271eb

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

packages/compiler-cli/src/ngtsc/typecheck/test/type_check_block_spec.ts

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
import ts from 'typescript';
1010

11+
import {absoluteFrom, getSourceFileOrError} from '../../file_system';
1112
import {initMockFileSystem} from '../../file_system/testing';
1213
import {Reference} from '../../imports';
13-
import {TypeCheckingConfig} from '../api';
14-
import {ALL_ENABLED_CONFIG, tcb, TestDeclaration, TestDirective} from '../testing';
14+
import {OptimizeFor, TypeCheckingConfig} from '../api';
15+
import {ALL_ENABLED_CONFIG, setup, tcb, TestDeclaration, TestDirective} from '../testing';
1516

1617

1718
describe('type check blocks', () => {
@@ -1744,4 +1745,55 @@ describe('type check blocks', () => {
17441745
expect(result).toContain('(this).trackingFn(_t2, _t1, ((this).prop));');
17451746
});
17461747
});
1748+
1749+
describe('import generation', () => {
1750+
const TEMPLATE = `<div dir [test]="null"></div>`;
1751+
const DIRECTIVE: TestDeclaration = {
1752+
type: 'directive',
1753+
name: 'Dir',
1754+
selector: '[dir]',
1755+
inputs: {
1756+
test: {
1757+
isSignal: true,
1758+
bindingPropertyName: 'test',
1759+
classPropertyName: 'test',
1760+
required: true,
1761+
transform: null,
1762+
}
1763+
}
1764+
};
1765+
1766+
it('should prefer namespace imports in type check files for new imports', () => {
1767+
const result = tcb(TEMPLATE, [DIRECTIVE]);
1768+
1769+
expect(result).toContain(`import * as i1 from '@angular/core';`);
1770+
expect(result).toContain(`[i1.ɵINPUT_SIGNAL_BRAND_WRITE_TYPE]`);
1771+
});
1772+
1773+
it('should re-use existing imports from original source files', () => {
1774+
// This is especially important for inline type check blocks.
1775+
// See: https://github.com/angular/angular/pull/53521#pullrequestreview-1778130879.
1776+
const {templateTypeChecker, program, programStrategy} = setup([{
1777+
fileName: absoluteFrom('/test.ts'),
1778+
templates: {'AppComponent': TEMPLATE},
1779+
declarations: [DIRECTIVE],
1780+
source: `
1781+
import {Component} from '@angular/core'; // should be re-used
1782+
1783+
class AppComponent {}
1784+
export class Dir {}
1785+
`,
1786+
}]);
1787+
1788+
// Trigger type check block generation.
1789+
templateTypeChecker.getDiagnosticsForFile(
1790+
getSourceFileOrError(program, absoluteFrom('/test.ts')), OptimizeFor.SingleFile);
1791+
1792+
const testSf = getSourceFileOrError(programStrategy.getProgram(), absoluteFrom('/test.ts'));
1793+
expect(testSf.text)
1794+
.toContain(
1795+
`import { Component, ɵINPUT_SIGNAL_BRAND_WRITE_TYPE } from '@angular/core'; // should be re-used`);
1796+
expect(testSf.text).toContain(`[ɵINPUT_SIGNAL_BRAND_WRITE_TYPE]`);
1797+
});
1798+
});
17471799
});

0 commit comments

Comments
 (0)