Skip to content

Commit 6572a69

Browse files
cexbrayatalan-agius4
authored andcommitted
fix(@schematics/angular): default components to OnPush change detection
1 parent 29df5ed commit 6572a69

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

packages/schematics/angular/component/files/__name@dasherize@if-flat__/__name@dasherize__.__type@dasherize__.ts.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { <% if(changeDetection !== 'Default') { %>ChangeDetectionStrategy, <% }%>Component<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%> } from '@angular/core';
1+
import { <% if(changeDetection !== 'OnPush') { %>ChangeDetectionStrategy, <% }%>Component<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%> } from '@angular/core';
22

33
@Component({<% if(!skipSelector) {%>
44
selector: '<%= selector %>',<%}%><% if(standalone) {%>
@@ -16,7 +16,7 @@ import { <% if(changeDetection !== 'Default') { %>ChangeDetectionStrategy, <% }%
1616
}
1717
<% } %>`,<% } else if (style !== 'none') { %>
1818
styleUrl: './<%= dasherize(name) %><%= type ? '.' + dasherize(type): '' %>.<%= style %>',<% } %><% if(!!viewEncapsulation) { %>
19-
encapsulation: ViewEncapsulation.<%= viewEncapsulation %>,<% } if (changeDetection !== 'Default') { %>
19+
encapsulation: ViewEncapsulation.<%= viewEncapsulation %>,<% } if (changeDetection !== 'OnPush') { %>
2020
changeDetection: ChangeDetectionStrategy.<%= changeDetection %>,<% } %>
2121
})
2222
export <% if(exportDefault) {%>default <%}%>class <%= classifiedName %> {

packages/schematics/angular/component/index_spec.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('Component Schematic', () => {
2323
inlineStyle: false,
2424
inlineTemplate: false,
2525
displayBlock: false,
26-
changeDetection: ChangeDetection.Eager,
26+
changeDetection: ChangeDetection.OnPush,
2727
style: Style.Css,
2828
type: 'Component',
2929
skipTests: false,
@@ -64,12 +64,23 @@ describe('Component Schematic', () => {
6464
expect(tsContent).toContain('compileComponents()');
6565
});
6666

67-
it('should set change detection to OnPush', async () => {
68-
const options = { ...defaultOptions, changeDetection: 'OnPush' };
67+
it('should not set change detection when default is OnPush', async () => {
68+
const options = { ...defaultOptions };
69+
70+
const tree = await schematicRunner.runSchematic('component', options, appTree);
71+
const tsContent = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
72+
expect(tsContent).not.toMatch(/import.*ChangeDetectionStrategy/);
73+
expect(tsContent).not.toMatch(/changeDetection:/);
74+
expect(tsContent).not.toMatch(/ChangeDetectionStrategy/);
75+
});
76+
77+
it('should set changeDetection to Eager when requested', async () => {
78+
const options = { ...defaultOptions, changeDetection: 'Eager' };
6979

7080
const tree = await schematicRunner.runSchematic('component', options, appTree);
7181
const tsContent = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
72-
expect(tsContent).toMatch(/changeDetection: ChangeDetectionStrategy.OnPush/);
82+
expect(tsContent).toMatch(/import .*ChangeDetectionStrategy/);
83+
expect(tsContent).toContain('changeDetection: ChangeDetectionStrategy.Eager,');
7384
});
7485

7586
it('should not set view encapsulation', async () => {
@@ -348,27 +359,27 @@ describe('Component Schematic', () => {
348359
expect(tree.files).not.toContain('/projects/bar/src/app/foo/foo.component.spec.ts');
349360
});
350361

351-
it('should respect templateUrl when style=none and changeDetection=OnPush', async () => {
352-
const options = { ...defaultOptions, style: Style.None, changeDetection: 'OnPush' };
362+
it('should respect templateUrl when style=none and changeDetection=Eager', async () => {
363+
const options = { ...defaultOptions, style: Style.None, changeDetection: 'Eager' };
353364
const tree = await schematicRunner.runSchematic('component', options, appTree);
354365
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
355366
expect(content).not.toMatch(/styleUrls: /);
356367
expect(content).toMatch(/templateUrl: '.\/foo.component.html',\n/);
357-
expect(content).toMatch(/changeDetection: ChangeDetectionStrategy.OnPush/);
368+
expect(content).toMatch(/changeDetection: ChangeDetectionStrategy.Eager/);
358369
});
359370

360-
it('should respect inlineTemplate when style=none and changeDetection=OnPush', async () => {
371+
it('should respect inlineTemplate when style=none and changeDetection=Eager', async () => {
361372
const options = {
362373
...defaultOptions,
363374
style: Style.None,
364-
changeDetection: 'OnPush',
375+
changeDetection: 'Eager',
365376
inlineTemplate: true,
366377
};
367378
const tree = await schematicRunner.runSchematic('component', options, appTree);
368379
const content = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
369380
expect(content).not.toMatch(/styleUrls: /);
370381
expect(content).toMatch(/template: `(\n(.|)*){3}\n\s*`,\n/);
371-
expect(content).toMatch(/changeDetection: ChangeDetectionStrategy.OnPush/);
382+
expect(content).toMatch(/changeDetection: ChangeDetectionStrategy.Eager,/);
372383
});
373384

374385
it('should create a standalone component', async () => {

packages/schematics/angular/component/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"description": "Configures the change detection strategy for the component.",
6868
"enum": ["Eager", "OnPush"],
6969
"type": "string",
70-
"default": "Eager",
70+
"default": "OnPush",
7171
"alias": "c"
7272
},
7373
"prefix": {

0 commit comments

Comments
 (0)