Skip to content

fix(material/schematics): always add a custom theme with ng add #31522

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
247 changes: 7 additions & 240 deletions src/material/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('ng-add schematic', () => {
const workspace = await readWorkspace(tree);
const project = getProjectFromWorkspace(workspace, baseOptions.project);

expectProjectStyleFile(project, '@angular/material/prebuilt-themes/azure-blue.css');
expectProjectStyleFile(project, 'projects/material/src/custom-theme.scss');
});

it('should support adding a custom theme', async () => {
Expand All @@ -96,7 +96,7 @@ describe('ng-add schematic', () => {

const tree = await runner.runSchematic(
'ng-add-setup-project',
{...baseOptions, theme: 'custom'},
{...baseOptions, theme: 'azure-blue'},
appTree,
);
const workspace = await readWorkspace(tree);
Expand All @@ -116,7 +116,7 @@ describe('ng-add schematic', () => {

const tree = await runner.runSchematic(
'ng-add-setup-project',
{...baseOptions, theme: 'custom'},
{...baseOptions, theme: 'azure-blue'},
appTree,
);
const workspace = await readWorkspace(tree);
Expand Down Expand Up @@ -213,108 +213,14 @@ describe('ng-add schematic', () => {
runner.runSchematic('ng-add-setup-project', baseOptions, appTree),
).toBeRejected();
});

it('should warn if the "test" target has been changed', async () => {
overwriteTargetBuilder(appTree, 'test', 'thirdparty-test-builder');
await runner.runSchematic('ng-add-setup-project', baseOptions, appTree);

expect(errorOutput.length).toBe(0);
expect(warnOutput.length).toBe(1);
expect(warnOutput[0]).toMatch(
/not using the default builders.*cannot add the configured theme/,
);
});
});

describe('theme files', () => {
/** Path to the default prebuilt theme file that will be added when running ng-add. */
const defaultPrebuiltThemePath = '@angular/material/prebuilt-themes/azure-blue.css';

/** Writes a specific style file to the workspace in the given tree */
function writeStyleFileToWorkspace(tree: Tree, stylePath: string) {
tree.overwrite(
'/angular.json',
JSON.stringify(
{
version: 1,
projects: {
material: {
projectType: 'application',
root: 'projects/material',
sourceRoot: 'projects/material/src',
prefix: 'app',
architect: {
build: {
builder: '@angular-devkit/build-angular:application',
options: {
outputPath: 'dist/material',
index: 'projects/material/src/index.html',
browser: 'projects/material/src/main.ts',
styles: ['projects/material/src/styles.css', stylePath],
},
},
},
},
},
},
null,
2,
),
);
}

it('should replace existing prebuilt theme files', async () => {
const existingThemePath = '@angular/material/prebuilt-themes/purple-green.css';
writeStyleFileToWorkspace(appTree, existingThemePath);

const tree = await runner.runSchematic('ng-add-setup-project', baseOptions, appTree);
const workspace = await readWorkspace(tree);
const project = getProjectFromWorkspace(workspace, baseOptions.project);
const styles = getProjectTargetOptions(project, 'build')['styles'];

expect(styles)
.not.withContext('Expected the existing prebuilt theme file to be removed.')
.toContain(existingThemePath);
expect(styles)
.withContext('Expected the default prebuilt theme to be added.')
.toContain(defaultPrebuiltThemePath);
});

it('should not replace existing custom theme files', async () => {
writeStyleFileToWorkspace(appTree, './projects/material/custom-theme.scss');

const tree = await runner.runSchematic('ng-add-setup-project', baseOptions, appTree);
const workspace = await readWorkspace(tree);
const project = getProjectFromWorkspace(workspace, baseOptions.project);
const styles = getProjectTargetOptions(project, 'build')['styles'];

expect(styles)
.not.withContext('Expected the default prebuilt theme to be not configured.')
.toContain(defaultPrebuiltThemePath);
expect(errorOutput.length).toBe(1);
expect(errorOutput[0]).toMatch(/Could not add the selected theme/);
});

it('should not add a theme file multiple times', async () => {
writeStyleFileToWorkspace(appTree, defaultPrebuiltThemePath);

const tree = await runner.runSchematic('ng-add-setup-project', baseOptions, appTree);
const workspace = await readWorkspace(tree);
const project = getProjectFromWorkspace(workspace, baseOptions.project);
const styles = getProjectTargetOptions(project, 'build')['styles'];

expect(styles)
.withContext(
'Expected the "styles.css" file and default prebuilt theme to be ' + 'the only styles',
)
.toEqual(['projects/material/src/styles.css', defaultPrebuiltThemePath]);
});

it('should not overwrite existing custom theme files', async () => {
appTree.create('/projects/material/custom-theme.scss', 'custom-theme');
const tree = await runner.runSchematic(
'ng-add-setup-project',
{...baseOptions, theme: 'custom'},
{...baseOptions, theme: 'azure-blue'},
appTree,
);
expect(tree.readContent('/projects/material/custom-theme.scss'))
Expand All @@ -323,117 +229,6 @@ describe('ng-add schematic', () => {
});
});

it('should add the global typography class if the body has no classes', async () => {
const tree = await runner.runSchematic(
'ng-add-setup-project',
{
...baseOptions,
typography: true,
},
appTree,
);
const workspace = await readWorkspace(tree);
const project = getProjectFromWorkspace(workspace, baseOptions.project);

const indexFiles = getProjectIndexFiles(project);
expect(indexFiles.length).toBe(1);

indexFiles.forEach(indexPath => {
const buffer = tree.read(indexPath)!;
expect(buffer.toString()).toContain('<body class="mat-typography">');
});
});

it('should add the global typography class if the body has existing classes', async () => {
appTree.overwrite(
'projects/material/src/index.html',
`
<html>
<head></head>
<body class="one two"></body>
</html>
`,
);

const tree = await runner.runSchematic(
'ng-add-setup-project',
{
...baseOptions,
typography: true,
},
appTree,
);
const workspace = await readWorkspace(tree);
const project = getProjectFromWorkspace(workspace, baseOptions.project);
const indexFiles = getProjectIndexFiles(project);
expect(indexFiles.length).toBe(1);

indexFiles.forEach(indexPath => {
const buffer = tree.read(indexPath)!;
expect(buffer.toString()).toContain('<body class="one two mat-typography">');
});
});

it('should not add the global typography class if it exists already', async () => {
appTree.overwrite(
'projects/material/src/index.html',
`
<html>
<head></head>
<body class="one mat-typography two"></body>
</html>
`,
);

const tree = await runner.runSchematic(
'ng-add-setup-project',
{
...baseOptions,
typography: true,
},
appTree,
);
const workspace = await readWorkspace(tree);
const project = getProjectFromWorkspace(workspace, baseOptions.project);
const indexFiles = getProjectIndexFiles(project);
expect(indexFiles.length).toBe(1);

indexFiles.forEach(indexPath => {
const buffer = tree.read(indexPath)!;
expect(buffer.toString()).toContain('<body class="one mat-typography two">');
});
});

it('should not add the global typography class if the user did not opt into it', async () => {
appTree.overwrite(
'projects/material/src/index.html',
`
<html>
<head></head>
<body class="one two"></body>
</html>
`,
);

const tree = await runner.runSchematic(
'ng-add-setup-project',
{
...baseOptions,
typography: false,
},
appTree,
);
const workspace = await readWorkspace(tree);
const project = getProjectFromWorkspace(workspace, baseOptions.project);
const indexFiles = getProjectIndexFiles(project);
expect(indexFiles.length).toBe(1);

indexFiles.forEach(indexPath => {
const buffer = tree.read(indexPath)!;
expect(buffer.toString()).toContain('<body class="one two">');
});
});

describe('using browser builder', () => {
beforeEach(() => {
const config = {
Expand Down Expand Up @@ -476,21 +271,7 @@ describe('ng-add schematic', () => {
const workspace = await readWorkspace(tree);
const project = getProjectFromWorkspace(workspace, baseOptions.project);

expectProjectStyleFile(project, '@angular/material/prebuilt-themes/azure-blue.css');
});

it('should add material app styles', async () => {
const tree = await runner.runSchematic('ng-add-setup-project', baseOptions, appTree);
const workspace = await readWorkspace(tree);
const project = getProjectFromWorkspace(workspace, baseOptions.project);

const defaultStylesPath = getProjectStyleFile(project)!;
const htmlContent = tree.read(defaultStylesPath)!.toString();

expect(htmlContent).toContain('html, body { height: 100%; }');
expect(htmlContent).toContain(
'body { margin: 0; font-family: Roboto, "Helvetica Neue", sans-serif; }',
);
expectProjectStyleFile(project, 'projects/material/src/custom-theme.scss');
});
});

Expand Down Expand Up @@ -536,21 +317,7 @@ describe('ng-add schematic', () => {
const workspace = await readWorkspace(tree);
const project = getProjectFromWorkspace(workspace, baseOptions.project);

expectProjectStyleFile(project, '@angular/material/prebuilt-themes/azure-blue.css');
});

it('should add material app styles', async () => {
const tree = await runner.runSchematic('ng-add-setup-project', baseOptions, appTree);
const workspace = await readWorkspace(tree);
const project = getProjectFromWorkspace(workspace, baseOptions.project);

const defaultStylesPath = getProjectStyleFile(project)!;
const htmlContent = tree.read(defaultStylesPath)!.toString();

expect(htmlContent).toContain('html, body { height: 100%; }');
expect(htmlContent).toContain(
'body { margin: 0; font-family: Roboto, "Helvetica Neue", sans-serif; }',
);
expectProjectStyleFile(project, 'projects/material/src/custom-theme.scss');
});
});

Expand Down Expand Up @@ -587,7 +354,7 @@ describe('ng-add schematic', () => {
const workspace = await readWorkspace(tree);
const project = getProjectFromWorkspace(workspace, baseOptions.project);

expectProjectStyleFile(project, '@angular/material/prebuilt-themes/azure-blue.css');
expectProjectStyleFile(project, 'projects/material/src/custom-theme.scss');
});
});
});
Expand Down
11 changes: 2 additions & 9 deletions src/material/schematics/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"type": "string",
"default": "azure-blue",
"x-prompt": {
"message": "Choose a prebuilt theme name, or \"custom\" for a custom theme:",
"message": "Select a pair of starter prebuilt color palettes for your Angular Material theme",
"type": "list",
"items": [
{
Expand All @@ -34,16 +34,9 @@
{
"value": "cyan-orange",
"label": "Cyan/Orange [Preview: https://material.angular.dev?theme=cyan-orange]"
},
{"value": "custom", "label": "Custom"}
}
]
}
},
"typography": {
"type": "boolean",
"default": false,
"description": "Whether to set up global typography styles.",
"x-prompt": "Set up global Angular Material typography styles?"
}
},
"required": []
Expand Down
5 changes: 2 additions & 3 deletions src/material/schematics/ng-add/setup-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* found in the LICENSE file at https://angular.dev/license
*/

import {chain, noop, Rule, SchematicContext, Tree} from '@angular-devkit/schematics';
import {chain, Rule, SchematicContext, Tree} from '@angular-devkit/schematics';
import {getProjectFromWorkspace, getProjectStyleFile} from '@angular/cdk/schematics';
import {readWorkspace} from '@schematics/angular/utility';
import {ProjectType} from '@schematics/angular/utility/workspace-models';
import {addFontsToIndex} from './fonts/material-fonts';
import {Schema} from './schema';
import {addThemeToAppStyles, addTypographyClass} from './theming/theming';
import {addThemeToAppStyles} from './theming/theming';

/**
* Scaffolds the basics of a Angular Material application, this includes:
Expand All @@ -29,7 +29,6 @@ export default function (options: Schema): Rule {
addThemeToAppStyles(options),
addFontsToIndex(options),
addMaterialAppStyles(options),
options.typography ? addTypographyClass(options) : noop(),
]);
}
context.logger.warn(
Expand Down
Loading
Loading