Skip to content

Commit

Permalink
fix(a19): correct detection of standalone flag #10306 #10583 (#10751)
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime authored Jan 12, 2025
2 parents 6a140b3 + 8963df3 commit fec44bd
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 3 deletions.
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
5 changes: 3 additions & 2 deletions libs/ng-mocks/src/lib/common/func.is-standalone.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ngMocksUniverse from '../common/ng-mocks-universe';
import collectDeclarations from '../resolve/collect-declarations';

import { getNgType } from './func.get-ng-type';
Expand All @@ -7,9 +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;
}

return collectDeclarations(declaration)[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;
1 change: 1 addition & 0 deletions libs/ng-mocks/src/lib/mock-render/func.create-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export default (
selector: 'mock-render',
template: mockTemplate,
viewProviders: flags.viewProviders,
standalone: false,
};

ctor = generateWrapperComponent({ ...meta, bindings, options });
Expand Down
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 fec44bd

Please sign in to comment.