Skip to content

Commit 6ce7c4c

Browse files
dgp1130mmalerba
authored andcommitted
fix(devtools): call ng.getDirectiveMetadata with the component instance (angular#60991)
`ng.getDirectiveMetadata` receives the component instance, not the raw DOM element. This assumes that `ng.getComponent` is implemented in all environments and that the root element itself is a component. PR Close angular#60991
1 parent 4bcf183 commit 6ce7c4c

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

devtools/projects/ng-devtools-backend/src/lib/ng-debug-api/ng-debug-api.spec.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ const mockRoot = () => {
2525
};
2626

2727
/** Creates an `ng` object with a `getDirectiveMetadata` mock. */
28-
const createNgWithDirectiveMetadata = (
29-
framework: Framework,
30-
): Partial<Record<keyof Ng, () => void>> => ({
28+
const fakeNgGlobal = (framework: Framework): Partial<Record<keyof Ng, () => void>> => ({
29+
getComponent(): {} {
30+
return {};
31+
},
3132
getDirectiveMetadata(): Partial<ɵDirectiveDebugMetadata> {
3233
return {
3334
framework,
@@ -79,15 +80,15 @@ describe('ng-debug-api', () => {
7980
beforeEach(() => mockRoot());
8081

8182
it('should support Profiler API', () => {
82-
(globalThis as any).ng = createNgWithDirectiveMetadata(Framework.Angular);
83+
(globalThis as any).ng = fakeNgGlobal(Framework.Angular);
8384
expect(ngDebugProfilerApiIsSupported()).toBeTrue();
8485

85-
(globalThis as any).ng = createNgWithDirectiveMetadata(Framework.ACX);
86+
(globalThis as any).ng = fakeNgGlobal(Framework.ACX);
8687
expect(ngDebugProfilerApiIsSupported()).toBeTrue();
8788
});
8889

8990
it('should NOT support Profiler API', () => {
90-
(globalThis as any).ng = createNgWithDirectiveMetadata(Framework.Wiz);
91+
(globalThis as any).ng = fakeNgGlobal(Framework.Wiz);
9192

9293
expect(ngDebugRoutesApiIsSupported()).toBeFalse();
9394
});
@@ -99,15 +100,15 @@ describe('ng-debug-api', () => {
99100
beforeEach(() => mockRoot());
100101

101102
it('should support Routes API', () => {
102-
(globalThis as any).ng = createNgWithDirectiveMetadata(Framework.Angular);
103+
(globalThis as any).ng = fakeNgGlobal(Framework.Angular);
103104
expect(ngDebugRoutesApiIsSupported()).toBeTrue();
104105

105-
(globalThis as any).ng = createNgWithDirectiveMetadata(Framework.ACX);
106+
(globalThis as any).ng = fakeNgGlobal(Framework.ACX);
106107
expect(ngDebugRoutesApiIsSupported()).toBeTrue();
107108
});
108109

109110
it('should NOT support Routes API', () => {
110-
(globalThis as any).ng = createNgWithDirectiveMetadata(Framework.Wiz);
111+
(globalThis as any).ng = fakeNgGlobal(Framework.Wiz);
111112

112113
expect(ngDebugRoutesApiIsSupported()).toBeFalse();
113114
});

devtools/projects/ng-devtools-backend/src/lib/ng-debug-api/ng-debug-api.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ export function ngDebugProfilerApiIsSupported(): boolean {
5353
const roots = getRoots();
5454
return (
5555
!!roots.length &&
56-
!roots.some((el) => ng.getDirectiveMetadata?.(el)?.framework === Framework.Wiz)
56+
!roots.some((el) => {
57+
const comp = ng.getComponent!(el)!;
58+
return ng.getDirectiveMetadata?.(comp)?.framework === Framework.Wiz;
59+
})
5760
);
5861
}
5962

@@ -66,6 +69,9 @@ export function ngDebugRoutesApiIsSupported(): boolean {
6669
const roots = getRoots();
6770
return (
6871
!!roots.length &&
69-
!roots.some((el) => ng.getDirectiveMetadata?.(el)?.framework === Framework.Wiz)
72+
!roots.some((el) => {
73+
const comp = ng.getComponent!(el)!;
74+
return ng.getDirectiveMetadata?.(comp)?.framework === Framework.Wiz;
75+
})
7076
);
7177
}

0 commit comments

Comments
 (0)