Skip to content

Commit 15040fb

Browse files
crisbetoangular-robot[bot]
authored andcommitted
fix(cdk/schematics): update drag-drop schematic to support standalone
1 parent e112308 commit 15040fb

File tree

5 files changed

+60
-18
lines changed

5 files changed

+60
-18
lines changed

src/cdk/schematics/ng-generate/drag-drop/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.spec.ts.template

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
import { DragDropModule } from '@angular/cdk/drag-drop';
2-
import { waitForAsync, ComponentFixture, TestBed } from '@angular/core/testing';
3-
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
1+
import { <% if(!standalone) { %>waitForAsync, <% } %>ComponentFixture, TestBed } from '@angular/core/testing';<% if(!standalone) { %>
2+
import { DragDropModule } from '@angular/cdk/drag-drop';<% } %>
43
import { <%= classify(name) %>Component } from './<%= dasherize(name) %>.component';
54

65
describe('<%= classify(name) %>Component', () => {
76
let component: <%= classify(name) %>Component;
8-
let fixture: ComponentFixture<<%= classify(name) %>Component>;
7+
let fixture: ComponentFixture<<%= classify(name) %>Component>;<% if(!standalone) { %>
98

109
beforeEach(waitForAsync(() => {
1110
TestBed.configureTestingModule({
12-
declarations: [ <%= classify(name) %>Component ],
13-
imports: [
14-
NoopAnimationsModule,
15-
DragDropModule,
16-
]
11+
declarations: [<%= classify(name) %>Component],
12+
imports: [DragDropModule]
1713
}).compileComponents();
18-
}));
14+
}));<% } %>
1915

2016
beforeEach(() => {
2117
fixture = TestBed.createComponent(<%= classify(name) %>Component);

src/cdk/schematics/ng-generate/drag-drop/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.ts.template

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%><% if(changeDetection !== 'Default') { %>, ChangeDetectionStrategy<% }%> } from '@angular/core';
2-
import { CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';
2+
import { <% if(standalone) { %>CdkDrag, CdkDropList, <% } %>CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/drag-drop';
33

44
@Component({
55
selector: '<%= selector %>',<% if(inlineTemplate) { %>
@@ -9,10 +9,12 @@ import { CdkDragDrop, moveItemInArray, transferArrayItem } from '@angular/cdk/dr
99
templateUrl: './<%= dasherize(name) %>.component.html',<% } if(inlineStyle) { %>
1010
styles: [`
1111
<%= indentTextContent(resolvedFiles.stylesheet, 4) %>
12-
`],<% } else { %>
13-
styleUrls: ['./<%= dasherize(name) %>.component.<%= style %>'],<% } %><% if(!!viewEncapsulation) { %>
14-
encapsulation: ViewEncapsulation.<%= viewEncapsulation %>,<% } if (changeDetection !== 'Default') { %>
15-
changeDetection: ChangeDetectionStrategy.<%= changeDetection %>,<% } %>
12+
`]<% } else { %>
13+
styleUrls: ['./<%= dasherize(name) %>.component.<%= style %>']<% } %><% if(!!viewEncapsulation) { %>,
14+
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
15+
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %><% if(standalone) { %>,
16+
standalone: true,
17+
imports: [CdkDrag, CdkDropList]<% } %>
1618
})
1719
export class <%= classify(name) %>Component {
1820
todo = [

src/cdk/schematics/ng-generate/drag-drop/index.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,37 @@ describe('CDK drag-drop schematic', () => {
4141
expect(moduleContent).toContain('DragDropModule');
4242
});
4343

44+
describe('standalone option', () => {
45+
it('should generate a standalone component', async () => {
46+
const app = await createTestApp(runner);
47+
const tree = await runner.runSchematic('drag-drop', {...baseOptions, standalone: true}, app);
48+
const module = getFileContent(tree, '/projects/material/src/app/app.module.ts');
49+
const component = getFileContent(tree, '/projects/material/src/app/foo/foo.component.ts');
50+
51+
expect(module).not.toContain('DragDropModule');
52+
expect(module).not.toContain('FooComponent');
53+
54+
expect(component).not.toContain('DragDropModule');
55+
expect(component).toContain('standalone: true');
56+
expect(component).toContain('imports: [');
57+
});
58+
59+
it('should infer the standalone option from the project structure', async () => {
60+
const app = await createTestApp(runner, {standalone: true});
61+
const tree = await runner.runSchematic('drag-drop', baseOptions, app);
62+
const component = getFileContent(tree, '/projects/material/src/app/foo/foo.component.ts');
63+
const test = getFileContent(tree, '/projects/material/src/app/foo/foo.component.spec.ts');
64+
65+
expect(tree.exists('/projects/material/src/app/app.module.ts')).toBe(false);
66+
67+
expect(component).toContain('standalone: true');
68+
expect(component).toContain('imports: [');
69+
70+
expect(test).not.toContain('TestBed.configureTestingModule');
71+
expect(test).not.toContain('DragDropModule');
72+
});
73+
});
74+
4475
describe('style option', () => {
4576
it('should respect the option value', async () => {
4677
const tree = await runner.runSchematic(

src/cdk/schematics/ng-generate/drag-drop/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
*/
88

99
import {chain, noop, Rule, Tree} from '@angular-devkit/schematics';
10-
import {addModuleImportToModule, buildComponent, findModuleFromOptions} from '../../utils';
10+
import {
11+
addModuleImportToModule,
12+
buildComponent,
13+
findModuleFromOptions,
14+
isStandaloneSchematic,
15+
} from '../../utils';
1116
import {Schema} from './schema';
1217

1318
/** Scaffolds a new Angular component that uses the Drag and Drop module. */
@@ -29,7 +34,11 @@ export default function (options: Schema): Rule {
2934
/** Adds the required modules to the main module of the CLI project. */
3035
function addDragDropModulesToModule(options: Schema) {
3136
return async (host: Tree) => {
32-
const modulePath = await findModuleFromOptions(host, options);
33-
addModuleImportToModule(host, modulePath!, 'DragDropModule', '@angular/cdk/drag-drop');
37+
const isStandalone = await isStandaloneSchematic(host, options);
38+
39+
if (!isStandalone) {
40+
const modulePath = await findModuleFromOptions(host, options);
41+
addModuleImportToModule(host, modulePath!, 'DragDropModule', '@angular/cdk/drag-drop');
42+
}
3443
};
3544
}

src/cdk/schematics/ng-generate/drag-drop/schema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
"type": "boolean",
4040
"alias": "t"
4141
},
42+
"standalone": {
43+
"description": "Whether the generated component is standalone.",
44+
"type": "boolean"
45+
},
4246
"viewEncapsulation": {
4347
"description": "Specifies the view encapsulation strategy.",
4448
"enum": ["Emulated", "None"],

0 commit comments

Comments
 (0)