Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
feat(schematics): Add example component generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
bardosmisi authored and ffriedl89 committed Jul 6, 2020
1 parent e97879b commit 138cb87
Show file tree
Hide file tree
Showing 13 changed files with 769 additions and 0 deletions.
6 changes: 6 additions & 0 deletions libs/workspace/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
"factory": "./src/schematics/dt-component-dev/index",
"schema": "./src/schematics/dt-component-dev/schema.json",
"aliases": ["dt-dev"]
},
"dt-components-example": {
"description": "Create a new barista example",
"factory": "./src/schematics/dt-component-example/index",
"schema": "./src/schematics/dt-component-example/schema.json",
"aliases": ["dt-example"]
}
}
}
21 changes: 21 additions & 0 deletions libs/workspace/src/schematics/dt-component-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Shipped schematics

This schematics generates the component for barista examples. The only needed
parameters are the name of the component and the example.

The schematics is compatible with previously added components, so if the
component already has example, a new example for that component will be
generated.

Note: the compatibility mentioned above can be only guaranteed if the original
example component was generated with this schematics, and is not altered with
hand.

## Usage

- Please run: `ng build workspace` and after that,
`ng g ./dist/libs/workspace:dt-example --name={name of your example} --component={name of your component}`

### Testing

The schematics can be tested with the `ng test workspace` command.
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Add Example Component should change files appropriately 1`] = `
"/**
* @license
* Copyright 2020 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the \\"License\\");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an \\"AS IS\\" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { NgModule } from '@angular/core';
import { DtTestComponentModule } from '@dynatrace/barista-components/test-component';
import { DtExampleSimpleTestComponent } from './test-component-simple-example/test-component-simple-example';
import { DtExampleComplexTestComponent } from './test-component-complex-example/test-component-complex-example';
export const DT_TEST_COMPONENT_EXAMPLES = [
DtExampleSimpleTestComponent,
DtExampleComplexTestComponent,
];
@NgModule({
imports: [DtTestComponentModule],
declarations: [...DT_TEST_COMPONENT_EXAMPLES],
entryComponents: [...DT_TEST_COMPONENT_EXAMPLES],
})
export class DtExamplesTestComponentModule {}
"
`;

exports[`Add Example Component should change files appropriately 2`] = `
"/**
* @license
* Copyright 2020 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the \\"License\\");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an \\"AS IS\\" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from './test-component-examples.module';
export * from './test-component-simple-example/test-component-simple-example';
export * from './test-component-complex-example/test-component-complex-example';
"
`;

exports[`Add Example Component should generate files with the appropriate content 1`] = `
"/**
* @license
* Copyright 2020 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the \\"License\\");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an \\"AS IS\\" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from './test-component-examples.module';
export * from './test-component-simple-example/test-component-simple-example';
"
`;

exports[`Add Example Component should generate files with the appropriate content 2`] = `
"/**
* @license
* Copyright 2020 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the \\"License\\");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an \\"AS IS\\" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { NgModule } from '@angular/core';
import { DtTestComponentModule } from '@dynatrace/barista-components/test-component';
import { DtExampleSimpleTestComponent } from './test-component-simple-example/test-component-simple-example';
export const DT_TEST_COMPONENT_EXAMPLES = [DtExampleSimpleTestComponent];
@NgModule({
imports: [DtTestComponentModule],
declarations: [...DT_TEST_COMPONENT_EXAMPLES],
entryComponents: [...DT_TEST_COMPONENT_EXAMPLES],
})
export class DtExamplesTestComponentModule {}
"
`;

exports[`Add Example Component should generate files with the appropriate content 3`] = `
"<dt-test-component></dt-test-component>
"
`;

exports[`Add Example Component should generate files with the appropriate content 4`] = `
"/**
* @license
* Copyright 2020 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the \\"License\\");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an \\"AS IS\\" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Component } from '@angular/core';
@Component({
selector: 'dt-example-simple-test-component',
templateUrl: './test-component-simple-example.html',
})
export class DtExampleSimpleTestComponent {}
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<<%= componentSelector %>></<%= componentSelector %>>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @license
* Copyright 2020 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Component } from '@angular/core';

@Component({
selector: '<%= selector %>',
templateUrl: './<%= exampleComponent.template %>',
})
export class <%= exampleComponent.component %> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @license
* Copyright 2020 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export * from './test-component-examples.module';
export * from './test-component-simple-example/test-component-simple-example';
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @license
* Copyright 2020 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { NgModule } from '@angular/core';
import { DtTestComponentModule } from '@dynatrace/barista-components/test-component';
import { DtExampleSimpleTestComponent } from './test-component-simple-example/test-component-simple-example';

export const DT_TEST_COMPONENT_EXAMPLES = [DtExampleSimpleTestComponent];

@NgModule({
imports: [DtTestComponentModule],
declarations: [...DT_TEST_COMPONENT_EXAMPLES],
entryComponents: [...DT_TEST_COMPONENT_EXAMPLES],
})
export class DtExamplesTestComponentModule {}
145 changes: 145 additions & 0 deletions libs/workspace/src/schematics/dt-component-example/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/**
* @license
* Copyright 2020 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Tree } from '@angular-devkit/schematics';
import {
SchematicTestRunner,
UnitTestTree,
} from '@angular-devkit/schematics/testing';
import {
Schema as ApplicationOptions,
Style,
} from '@schematics/angular/application/schema';
import { Schema as WorkspaceOptions } from '@schematics/angular/workspace/schema';
import { join } from 'path';
import { addFixtureToTree, getNewFiles } from '../utils/test-utils';

const fixturesFolder = join(__dirname, './fixtures');

describe('Add Example Component', () => {
const workspaceOptions: WorkspaceOptions = {
name: 'workspace',
newProjectRoot: 'apps',
version: '8.0.0',
};

const appOptions: ApplicationOptions = {
name: 'myapp',
inlineStyle: false,
inlineTemplate: false,
routing: false,
style: Style.Scss,
skipTests: true,
skipPackageJson: true,
};

let initialTree: UnitTestTree;
let schematicRunner: SchematicTestRunner;
let initialFiles: string[];

beforeEach(async () => {
schematicRunner = new SchematicTestRunner(
'@dynatrace/barista-components/schematics',
join(__dirname, '../../../collection.json'),
);

initialTree = await schematicRunner
.runExternalSchematicAsync(
'@schematics/angular',
'workspace',
workspaceOptions,
Tree.empty(),
)
.toPromise();

initialTree = await schematicRunner
.runExternalSchematicAsync(
'@schematics/angular',
'application',
appOptions,
initialTree,
)
.toPromise();

initialFiles = initialTree.files;
});

it('should add the necessary files', async () => {
const result = await schematicRunner
.runSchematicAsync(
'dt-example',
{ name: 'simple', component: 'TestComponent' },
initialTree,
)
.toPromise();

const newFiles = getNewFiles(result.files, initialFiles);

expect(newFiles).toEqual([
'/libs/examples/src/test-component/index.ts',
'/libs/examples/src/test-component/test-component-examples.module.ts',
'/libs/examples/src/test-component/test-component-simple-example/test-component-simple-example.html',
'/libs/examples/src/test-component/test-component-simple-example/test-component-simple-example.ts',
]);
});

it('should generate files with the appropriate content', async () => {
const result = await schematicRunner
.runSchematicAsync(
'dt-example',
{ name: 'simple', component: 'TestComponent' },
initialTree,
)
.toPromise();

const newFiles = getNewFiles(result.files, initialFiles);
newFiles.map((file) => expect(result.readContent(file)).toMatchSnapshot());
});

it('should change files appropriately', async () => {
await addFixtureToTree(
initialTree,
'test-component-examples.module.ts.fixture',
'/libs/examples/src/test-component/test-component-examples.module.ts',
fixturesFolder,
);

await addFixtureToTree(
initialTree,
'index.ts.fixture',
'/libs/examples/src/test-component/index.ts',
fixturesFolder,
);

const result = await schematicRunner
.runSchematicAsync(
'dt-example',
{ name: 'complex', component: 'TestComponent' },
initialTree,
)
.toPromise();

expect(
result.readContent(
'/libs/examples/src/test-component/test-component-examples.module.ts',
),
).toMatchSnapshot();

expect(
result.readContent('/libs/examples/src/test-component/index.ts'),
).toMatchSnapshot();
});
});
Loading

0 comments on commit 138cb87

Please sign in to comment.