Skip to content

Commit

Permalink
feat(angular): use zone eventCoalescing by default (#22802)
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Apr 19, 2024
1 parent f910a29 commit 537be46
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -620,12 +620,12 @@ bootstrapApplication(AppComponent, appConfig).catch((err) =>
`;
exports[`app --standalone should generate a standalone app correctly with routing 2`] = `
"import { ApplicationConfig } from '@angular/core';
"import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router';
import { appRoutes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [provideRouter(appRoutes) ]
providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(appRoutes) ]
};
"
`;
Expand Down Expand Up @@ -698,10 +698,10 @@ bootstrapApplication(AppComponent, appConfig).catch((err) =>
`;
exports[`app --standalone should generate a standalone app correctly without routing 2`] = `
"import { ApplicationConfig } from '@angular/core';
"import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
export const appConfig: ApplicationConfig = {
providers: []
providers: [provideZoneChangeDetection({ eventCoalescing: true }), ]
};
"
`;
Expand Down Expand Up @@ -753,6 +753,17 @@ describe('AppComponent', () => {
"
`;
exports[`app --standalone should should not use event coalescing in versions lower than v18 1`] = `
"import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { appRoutes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [provideRouter(appRoutes) ]
};
"
`;
exports[`app --strict should enable strict type checking: app tsconfig.json 1`] = `
{
"angularCompilerOptions": {
Expand Down
34 changes: 34 additions & 0 deletions packages/angular/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -896,13 +896,47 @@ describe('app', () => {
appTree.read('standalone/src/app/nx-welcome.component.ts', 'utf-8')
).toContain('standalone: true');
});

it('should should not use event coalescing in versions lower than v18', async () => {
updateJson(appTree, 'package.json', (json) => ({
...json,
dependencies: { ...json.dependencies, '@angular/core': '~17.0.0' },
}));

await generateApp(appTree, 'standalone', { standalone: true });

expect(
appTree.read('standalone/src/app/app.config.ts', 'utf-8')
).toMatchSnapshot();
});
});

it('should generate correct main.ts', async () => {
// ACT
await generateApp(appTree, 'myapp');

// ASSERT
expect(appTree.read('myapp/src/main.ts', 'utf-8')).toMatchInlineSnapshot(`
"import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
platformBrowserDynamic()
.bootstrapModule(AppModule, {
ngZoneEventCoalescing: true
})
.catch((err) => console.error(err));
"
`);
});

it('should should not use event coalescing in versions lower than v18', async () => {
updateJson(appTree, 'package.json', (json) => ({
...json,
dependencies: { ...json.dependencies, '@angular/core': '~17.0.0' },
}));

await generateApp(appTree, 'myapp');

expect(appTree.read('myapp/src/main.ts', 'utf-8')).toMatchInlineSnapshot(`
"import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';

platformBrowserDynamic()
.bootstrapModule(AppModule)
.bootstrapModule(AppModule<% if (useEventCoalescing) { %>, {
ngZoneEventCoalescing: true
}<% } %>)
.catch((err) => console.error(err));
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApplicationConfig } from <% if (angularMajorVersion >= 16) { %>'@angular/core';<% } else { %>'@angular/platform-browser';<% } %><% if (routing) { %>
import { ApplicationConfig<% if (useEventCoalescing) { %>, provideZoneChangeDetection<% } %> } from <% if (angularMajorVersion >= 16) { %>'@angular/core';<% } else { %>'@angular/platform-browser';<% } %><% if (routing) { %>
import { provideRouter } from '@angular/router';
import { appRoutes } from './app.routes';<% } %>

export const appConfig: ApplicationConfig = {
providers: [<% if (routing) { %>provideRouter(appRoutes) <% } %>]
providers: [<% if (useEventCoalescing) { %>provideZoneChangeDetection({ eventCoalescing: true }), <% } %><% if (routing) { %>provideRouter(appRoutes) <% } %>]
};
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export async function createFiles(
angularMajorVersion,
rootOffset,
isUsingApplicationBuilder,
useEventCoalescing: angularMajorVersion >= 18,
tpl: '',
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ exports[`Host App Generator --ssr should generate the correct files 2`] = `
import { AppModule } from './app/app.module';
platformBrowserDynamic()
.bootstrapModule(AppModule)
.bootstrapModule(AppModule, {
ngZoneEventCoalescing: true,
})
.catch((err) => console.error(err));
"
`;
Expand Down Expand Up @@ -408,13 +410,17 @@ export const appRoutes: Route[] = [
`;
exports[`Host App Generator --ssr should generate the correct files for standalone 8`] = `
"import { ApplicationConfig } from '@angular/core';
"import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router';
import { appRoutes } from './app.routes';
import { provideClientHydration } from '@angular/platform-browser';
export const appConfig: ApplicationConfig = {
providers: [provideClientHydration(), provideRouter(appRoutes)],
providers: [
provideClientHydration(),
provideZoneChangeDetection({ eventCoalescing: true }),
provideRouter(appRoutes),
],
};
"
`;
Expand Down Expand Up @@ -619,13 +625,17 @@ export const appRoutes: Route[] = [
`;
exports[`Host App Generator --ssr should generate the correct files for standalone when --typescript=true 8`] = `
"import { ApplicationConfig } from '@angular/core';
"import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router';
import { appRoutes } from './app.routes';
import { provideClientHydration } from '@angular/platform-browser';
export const appConfig: ApplicationConfig = {
providers: [provideClientHydration(), provideRouter(appRoutes)],
providers: [
provideClientHydration(),
provideZoneChangeDetection({ eventCoalescing: true }),
provideRouter(appRoutes),
],
};
"
`;
Expand Down Expand Up @@ -716,7 +726,9 @@ exports[`Host App Generator --ssr should generate the correct files when --types
import { AppModule } from './app/app.module';
platformBrowserDynamic()
.bootstrapModule(AppModule)
.bootstrapModule(AppModule, {
ngZoneEventCoalescing: true,
})
.catch((err) => console.error(err));
"
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ export class AppModule {}
`;
exports[`NgRxRootStoreGenerator Standalone APIs should add a facade when --facade=true 1`] = `
"import { ApplicationConfig } from '@angular/core';
"import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router';
import { appRoutes } from './app.routes';
import { provideStore, provideState } from '@ngrx/store';
Expand All @@ -436,7 +436,7 @@ import { UsersEffects } from './+state/users.effects';
import { UsersFacade } from './+state/users.facade';
export const appConfig: ApplicationConfig = {
providers: [provideEffects(UsersEffects),provideState(fromUsers.USERS_FEATURE_KEY, fromUsers.usersReducer),UsersFacade,provideEffects(),provideStore(),provideRouter(appRoutes) ]
providers: [provideEffects(UsersEffects),provideState(fromUsers.USERS_FEATURE_KEY, fromUsers.usersReducer),UsersFacade,provideEffects(),provideStore(),provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(appRoutes) ]
};
"
`;
Expand Down Expand Up @@ -473,7 +473,7 @@ export class UsersFacade {
`;
exports[`NgRxRootStoreGenerator Standalone APIs should add a root module and root state when --minimal=false 1`] = `
"import { ApplicationConfig } from '@angular/core';
"import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router';
import { appRoutes } from './app.routes';
import { provideStore, provideState } from '@ngrx/store';
Expand All @@ -482,7 +482,7 @@ import * as fromUsers from './+state/users.reducer';
import { UsersEffects } from './+state/users.effects';
export const appConfig: ApplicationConfig = {
providers: [provideEffects(UsersEffects),provideState(fromUsers.USERS_FEATURE_KEY, fromUsers.usersReducer),provideEffects(),provideStore(),provideRouter(appRoutes) ]
providers: [provideEffects(UsersEffects),provideState(fromUsers.USERS_FEATURE_KEY, fromUsers.usersReducer),provideEffects(),provideStore(),provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(appRoutes) ]
};
"
`;
Expand Down Expand Up @@ -722,28 +722,28 @@ describe('Users Selectors', () => {
`;
exports[`NgRxRootStoreGenerator Standalone APIs should add an empty root module when --minimal=true 1`] = `
"import { ApplicationConfig } from '@angular/core';
"import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router';
import { appRoutes } from './app.routes';
import { provideStore } from '@ngrx/store';
import { provideEffects } from '@ngrx/effects';
export const appConfig: ApplicationConfig = {
providers: [provideEffects(),provideStore(),provideRouter(appRoutes) ]
providers: [provideEffects(),provideStore(),provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(appRoutes) ]
};
"
`;
exports[`NgRxRootStoreGenerator Standalone APIs should instrument the store devtools when "addDevTools: true" 1`] = `
"import { ApplicationConfig, isDevMode } from '@angular/core';
"import { ApplicationConfig, provideZoneChangeDetection, isDevMode } from '@angular/core';
import { provideRouter } from '@angular/router';
import { appRoutes } from './app.routes';
import { provideStore } from '@ngrx/store';
import { provideEffects } from '@ngrx/effects';
import { provideStoreDevtools } from '@ngrx/store-devtools';
export const appConfig: ApplicationConfig = {
providers: [provideStoreDevtools({ logOnly: !isDevMode() }),provideEffects(),provideStore(),provideRouter(appRoutes) ]
providers: [provideStoreDevtools({ logOnly: !isDevMode() }),provideEffects(),provideStore(),provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(appRoutes) ]
};
"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ bootstrapApplication(AppComponent, appConfig).catch((err) =>
`;
exports[`ngrx Standalone APIs should add a root module with feature module when minimal is set to false 2`] = `
"import { ApplicationConfig } from '@angular/core';
"import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router';
import { appRoutes } from './app.routes';
import { provideStore, provideState } from '@ngrx/store';
Expand All @@ -765,7 +765,7 @@ import * as fromUsers from './+state/users.reducer';
import { UsersEffects } from './+state/users.effects';
export const appConfig: ApplicationConfig = {
providers: [provideEffects(UsersEffects),provideState(fromUsers.USERS_FEATURE_KEY, fromUsers.usersReducer),provideEffects(),provideStore(),provideRouter(appRoutes) ]
providers: [provideEffects(UsersEffects),provideState(fromUsers.USERS_FEATURE_KEY, fromUsers.usersReducer),provideEffects(),provideStore(),provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(appRoutes) ]
};
"
`;
Expand All @@ -782,14 +782,14 @@ bootstrapApplication(AppComponent, appConfig).catch((err) =>
`;
exports[`ngrx Standalone APIs should add an empty provideStore when minimal and root are set to true 2`] = `
"import { ApplicationConfig } from '@angular/core';
"import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router';
import { appRoutes } from './app.routes';
import { provideStore, provideState } from '@ngrx/store';
import { provideEffects } from '@ngrx/effects';
export const appConfig: ApplicationConfig = {
providers: [provideEffects(),provideStore(),provideRouter(appRoutes) ]
providers: [provideEffects(),provideStore(),provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(appRoutes) ]
};
"
`;
Expand All @@ -806,7 +806,7 @@ bootstrapApplication(AppComponent, appConfig).catch((err) =>
`;
exports[`ngrx Standalone APIs should add facade provider when facade is true 2`] = `
"import { ApplicationConfig } from '@angular/core';
"import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router';
import { appRoutes } from './app.routes';
import { provideStore, provideState } from '@ngrx/store';
Expand All @@ -816,7 +816,7 @@ import { UsersEffects } from './+state/users.effects';
import { UsersFacade } from './+state/users.facade';
export const appConfig: ApplicationConfig = {
providers: [provideEffects(UsersEffects),provideState(fromUsers.USERS_FEATURE_KEY, fromUsers.usersReducer),provideEffects(),provideStore(),UsersFacade,provideRouter(appRoutes) ]
providers: [provideEffects(UsersEffects),provideState(fromUsers.USERS_FEATURE_KEY, fromUsers.usersReducer),provideEffects(),provideStore(),UsersFacade,provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(appRoutes) ]
};
"
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ exports[`MF Remote App Generator --ssr should generate the correct files 2`] = `
import { AppModule } from './app/app.module';
platformBrowserDynamic()
.bootstrapModule(AppModule)
.bootstrapModule(AppModule, {
ngZoneEventCoalescing: true,
})
.catch((err) => console.error(err));
"
`;
Expand Down Expand Up @@ -333,7 +335,9 @@ exports[`MF Remote App Generator --ssr should generate the correct files when --
import { AppModule } from './app/app.module';
platformBrowserDynamic()
.bootstrapModule(AppModule)
.bootstrapModule(AppModule, {
ngZoneEventCoalescing: true,
})
.catch((err) => console.error(err));
"
`;
Expand Down
16 changes: 10 additions & 6 deletions packages/angular/src/generators/setup-ssr/setup-ssr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ describe('setupSSR', () => {
import { AppModule } from './app/app.module';
platformBrowserDynamic()
.bootstrapModule(AppModule)
.bootstrapModule(AppModule, {
ngZoneEventCoalescing: true,
})
.catch((err) => console.error(err));
"
`);
Expand Down Expand Up @@ -214,7 +216,9 @@ describe('setupSSR', () => {
import { AppModule } from './app/app.module';
platformBrowserDynamic()
.bootstrapModule(AppModule)
.bootstrapModule(AppModule, {
ngZoneEventCoalescing: true
})
.catch((err) => console.error(err));
"
`);
Expand Down Expand Up @@ -464,13 +468,13 @@ describe('setupSSR', () => {
// ASSERT
expect(tree.read('app1/src/app/app.config.ts', 'utf-8'))
.toMatchInlineSnapshot(`
"import { ApplicationConfig } from '@angular/core';
"import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router';
import { appRoutes } from './app.routes';
import { provideClientHydration } from '@angular/platform-browser';
export const appConfig: ApplicationConfig = {
providers: [provideClientHydration(),provideRouter(appRoutes) ]
providers: [provideClientHydration(),provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(appRoutes) ]
};
"
`);
Expand Down Expand Up @@ -542,12 +546,12 @@ describe('setupSSR', () => {

expect(tree.read('app1/src/app/app.config.ts', 'utf-8'))
.toMatchInlineSnapshot(`
"import { ApplicationConfig } from '@angular/core';
"import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideRouter, withEnabledBlockingInitialNavigation } from '@angular/router';
import { appRoutes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [provideRouter(appRoutes, withEnabledBlockingInitialNavigation()) ]
providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(appRoutes, withEnabledBlockingInitialNavigation()) ]
};
"
`);
Expand Down

0 comments on commit 537be46

Please sign in to comment.