Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(a19): correct detection of standalone flag #10306 #10583 #10751

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/,
);
});
});
});
Loading