angular/devtools/projects/ng-devtools-backend/src/lib/component-tree.spec.ts
Doug Parker 39e8bd0f4b refactor(devtools): ignore getInjector when it is not supported (#60206)
Previously Angular DevTools would throw when run on an application which does not support `getInjector`, now it safely ignores it and assumes dependency injection is not supported.

PR Close #60206
2025-03-05 12:05:46 -08:00

42 lines
1.2 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.dev/license
*/
import {Injector, ɵGlobalDevModeUtils} from '@angular/core';
import {getInjectorFromElementNode} from './component-tree';
type Ng = ɵGlobalDevModeUtils['ng'];
describe('component-tree', () => {
afterEach(() => {
delete (globalThis as any).ng;
});
describe('getInjectorFromElementNode', () => {
it('returns injector', () => {
const injector = Injector.create({
providers: [],
});
const ng: Partial<Ng> = {
getInjector: jasmine.createSpy('getInjector').and.returnValue(injector),
};
(globalThis as any).ng = ng;
const el = document.createElement('div');
expect(getInjectorFromElementNode(el)).toBe(injector);
expect(ng.getInjector).toHaveBeenCalledOnceWith(el);
});
it('returns `null` when `getInjector` is not supported', () => {
(globalThis as any).ng = {};
const el = document.createElement('div');
expect(getInjectorFromElementNode(el)).toBeNull();
});
});
});