From aa2b371840c5c86c0390a6b2b9890712d68e2f1c Mon Sep 17 00:00:00 2001 From: re-alam Date: Tue, 26 Nov 2024 16:33:49 +0000 Subject: [PATCH] fix(a19): respecting default standalone flag since angular 19 Added standalone false as default for mock-render and updated isStandalone method to handle angular 19+ default standalone behavior for components. --- .../src/lib/common/func.is-standalone.spec.ts | 33 +++++++++++++++++++ .../src/lib/common/func.is-standalone.ts | 10 +++++- .../lib/mock-render/func.create-wrapper.ts | 1 + 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 libs/ng-mocks/src/lib/common/func.is-standalone.spec.ts diff --git a/libs/ng-mocks/src/lib/common/func.is-standalone.spec.ts b/libs/ng-mocks/src/lib/common/func.is-standalone.spec.ts new file mode 100644 index 0000000000..c59e2c09af --- /dev/null +++ b/libs/ng-mocks/src/lib/common/func.is-standalone.spec.ts @@ -0,0 +1,33 @@ +import { Component, VERSION } from '@angular/core'; + +import { isStandalone } from './func.is-standalone'; + +@Component({ + selector: 'standalone', + template: `
+

Angular 19 standalone

+
`, +}) +class StandaloneComponent {} + +describe('func.is-standalone', () => { + describe('Angular Angular 19+ specific tests', () => { + let originalVersion: { major: string }; + const setVersionMajor = (major: number) => { + Object.assign(VERSION, { major: major.toString() }); + }; + + beforeAll(() => { + originalVersion = { ...VERSION }; + }); + + afterEach(() => { + Object.assign(VERSION, originalVersion); + }); + + it('should return true when standalone is undefined', () => { + setVersionMajor(19); + expect(isStandalone(StandaloneComponent)).toBeTruthy(); + }); + }); +}); diff --git a/libs/ng-mocks/src/lib/common/func.is-standalone.ts b/libs/ng-mocks/src/lib/common/func.is-standalone.ts index 621d1415b9..f600d5d225 100644 --- a/libs/ng-mocks/src/lib/common/func.is-standalone.ts +++ b/libs/ng-mocks/src/lib/common/func.is-standalone.ts @@ -1,3 +1,5 @@ +import { VERSION } from '@angular/core'; + import collectDeclarations from '../resolve/collect-declarations'; import { getNgType } from './func.get-ng-type'; @@ -11,5 +13,11 @@ export function isStandalone(declaration: any): boolean { return false; } - return collectDeclarations(declaration)[type].standalone === true; + // Handle Angular 19+ default standalone behavior + const declarations = collectDeclarations(declaration); + if (Number(VERSION.major) >= 19 && type !== 'NgModule' && declarations[type].standalone === undefined) { + return true; + } + + return declarations[type].standalone === true; } diff --git a/libs/ng-mocks/src/lib/mock-render/func.create-wrapper.ts b/libs/ng-mocks/src/lib/mock-render/func.create-wrapper.ts index fddb8e5927..601b523c32 100644 --- a/libs/ng-mocks/src/lib/mock-render/func.create-wrapper.ts +++ b/libs/ng-mocks/src/lib/mock-render/func.create-wrapper.ts @@ -140,6 +140,7 @@ export default ( selector: 'mock-render', template: mockTemplate, viewProviders: flags.viewProviders, + standalone: false, }; ctor = generateWrapperComponent({ ...meta, bindings, options });