Skip to content

Commit

Permalink
fix(a19): correct detection of standalone flag #10306 #10583
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Jan 12, 2025
1 parent fac711a commit 8963df3
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 42 deletions.
1 change: 0 additions & 1 deletion libs/ng-mocks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import './lib/common/ng-mocks-stack';
import './lib/common/ng-mocks-global-overrides';

export * from './lib/common/core.tokens';
export * from './lib/common/func.is-standalone';

export { getTestBedInjection, getInjection } from './lib/common/core.helpers';

Expand Down
3 changes: 3 additions & 0 deletions libs/ng-mocks/src/lib/common/core.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { VERSION } from '@angular/core';

export default {
flags: ['cacheModule', 'cacheComponent', 'cacheDirective', 'cacheProvider', 'correctModuleExports'],
mockRenderCacheSize: 25,
Expand Down Expand Up @@ -56,6 +58,7 @@ export default {
onMockBuilderMissingDependency: 'throw',
onMockInstanceRestoreNeed: 'warn',
onTestBedFlushNeed: 'warn',
defaultStandalone: Number.parseInt(VERSION.major, 10) >= 19,

dependencies: [
'declarations',
Expand Down
13 changes: 3 additions & 10 deletions libs/ng-mocks/src/lib/common/func.is-standalone.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { VERSION } from '@angular/core';

import ngMocksUniverse from '../common/ng-mocks-universe';
import collectDeclarations from '../resolve/collect-declarations';

import { getNgType } from './func.get-ng-type';
Expand All @@ -9,15 +8,9 @@ import { getNgType } from './func.get-ng-type';
*/
export function isStandalone(declaration: any): boolean {
const type = getNgType(declaration);
if (!type || type === 'Injectable') {
if (!type || type === 'Injectable' || type === 'NgModule') {
return false;
}

// 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;
return collectDeclarations(declaration)[type].standalone ?? ngMocksUniverse.global.get('flags').defaultStandalone;
}
1 change: 1 addition & 0 deletions libs/ng-mocks/src/lib/common/ng-mocks-universe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ ngMocksUniverse.global.set('flags', {
onMockInstanceRestoreNeed: coreConfig.onMockInstanceRestoreNeed,
// @deprecated and will be changed in A13 to 'throw'
onTestBedFlushNeed: coreConfig.onTestBedFlushNeed,
defaultStandalone: coreConfig.defaultStandalone,
});

ngMocksUniverse.getOverrides = globalMap('overrides');
Expand Down
8 changes: 7 additions & 1 deletion libs/ng-mocks/src/lib/mock-helper/mock-helper.object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ import mockHelperRender from './render/mock-helper.render';
import mockHelperFindTemplateRef from './template-ref/mock-helper.find-template-ref';
import mockHelperFindTemplateRefs from './template-ref/mock-helper.find-template-refs';

const flagNames = ['onMockBuilderMissingDependency', 'onMockInstanceRestoreNeed', 'onTestBedFlushNeed'] as const;
const flagNames = [
'onMockBuilderMissingDependency',
'onMockInstanceRestoreNeed',
'onTestBedFlushNeed',
'defaultStandalone',
] as const;

export default {
autoSpy: mockHelperAutoSpy,
Expand All @@ -50,6 +55,7 @@ export default {
onMockBuilderMissingDependency?: 'throw' | 'warn' | 'i-know-but-disable' | null;
onMockInstanceRestoreNeed?: 'throw' | 'warn' | 'i-know-but-disable' | null;
onTestBedFlushNeed?: 'throw' | 'warn' | 'i-know-but-disable' | null;
defaultStandalone?: boolean | null;
}) => {
const flags = ngMocksUniverse.global.get('flags');
for (const flag of flagNames) {
Expand Down
1 change: 1 addition & 0 deletions libs/ng-mocks/src/lib/mock-helper/mock-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1050,5 +1050,6 @@ export const ngMocks: {
onMockBuilderMissingDependency?: 'throw' | 'warn' | 'i-know-but-disable' | null;
onMockInstanceRestoreNeed?: 'throw' | 'warn' | 'i-know-but-disable' | null;
onTestBedFlushNeed?: 'throw' | 'warn' | 'i-know-but-disable' | null;
defaultStandalone?: boolean | null;
}): void;
} = mockHelperObject;
30 changes: 0 additions & 30 deletions tests/func.is-standalone.spec.ts

This file was deleted.

27 changes: 27 additions & 0 deletions tests/issue-10306/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Component } from '@angular/core';

import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';

@Component({
selector: 'target-10306',
template: 'real',
})
class TargetComponent {}

// @see https://github.com/help-me-mom/ng-mocks/issues/10306
// Angular 19 treats missing standalone property as true by default.
// It caused issues in how MockRender generates a wrapper component and other mocks.
// This fix respects new behavior and sets standalone flag accordingly.
describe('issue-10306', () => {
describe('MockRender', () => {
ngMocks.throwOnConsole();

beforeEach(() => MockBuilder(null, TargetComponent));

it('does not throw forgot to flush TestBed', () => {
expect(() => MockRender(TargetComponent)).not.toThrowError(
/Forgot to flush TestBed/,
);
});
});
});

0 comments on commit 8963df3

Please sign in to comment.