angular-docs-cn/packages/language-service/ivy/test/language_service_adapter_spec.ts
Keen Yee Liau 63624a2d46 feat(language-service): [Ivy] getSemanticDiagnostics for external templates (#39065)
This PR enables `getSemanticDiagnostics()` to be called on external templates.

Several changes are needed to land this feature:

1. The adapter needs to implement two additional methods:
   a. `readResource()`
       Load the template from snapshot instead of reading from disk
   b. `getModifiedResourceFiles()`
       Inform the compiler that external templates have changed so that the
       loader could invalidate its internal cache.
2. Create `ScriptInfo` for external templates in MockHost.
   Prior to this, MockHost only track changes in TypeScript files. Now it
   needs to create `ScriptInfo` for external templates as well.

For (1), in order to make sure we don't reload the template if it hasn't
changed, we need to keep track of its version. Since the complexity has
increased, the adapter is refactored into its own class.

PR Close #39065
2020-10-08 08:45:54 -07:00

50 lines
1.9 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* 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
*/
import {LanguageServiceAdapter} from '../language_service_adapter';
import {setup, TEST_TEMPLATE} from './mock_host';
const {project, service} = setup();
describe('Language service adapter', () => {
it('should register update if it has not seen the template before', () => {
const adapter = new LanguageServiceAdapter(project);
// Note that readResource() has never been called, so the adapter has no
// knowledge of the template at all.
const isRegistered = adapter.registerTemplateUpdate(TEST_TEMPLATE);
expect(isRegistered).toBeTrue();
expect(adapter.getModifiedResourceFiles().size).toBe(1);
});
it('should not register update if template has not changed', () => {
const adapter = new LanguageServiceAdapter(project);
adapter.readResource(TEST_TEMPLATE);
const isRegistered = adapter.registerTemplateUpdate(TEST_TEMPLATE);
expect(isRegistered).toBeFalse();
expect(adapter.getModifiedResourceFiles().size).toBe(0);
});
it('should register update if template has changed', () => {
const adapter = new LanguageServiceAdapter(project);
adapter.readResource(TEST_TEMPLATE);
service.overwrite(TEST_TEMPLATE, '<p>Hello World</p>');
const isRegistered = adapter.registerTemplateUpdate(TEST_TEMPLATE);
expect(isRegistered).toBe(true);
expect(adapter.getModifiedResourceFiles().size).toBe(1);
});
it('should clear template updates on read', () => {
const adapter = new LanguageServiceAdapter(project);
const isRegistered = adapter.registerTemplateUpdate(TEST_TEMPLATE);
expect(isRegistered).toBeTrue();
expect(adapter.getModifiedResourceFiles().size).toBe(1);
adapter.readResource(TEST_TEMPLATE);
expect(adapter.getModifiedResourceFiles().size).toBe(0);
});
});