forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex_spec.ts
More file actions
132 lines (112 loc) · 5.44 KB
/
index_spec.ts
File metadata and controls
132 lines (112 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { Schema as ApplicationOptions, Style } from '../application/schema';
import { Schema as WorkspaceOptions } from '../workspace/schema';
async function createTestApp(
runner: SchematicTestRunner,
appOptions: ApplicationOptions,
style = Style.Css,
): Promise<UnitTestTree> {
const workspaceOptions: WorkspaceOptions = {
name: 'workspace',
newProjectRoot: 'projects',
version: '6.0.0',
};
const appTree = await runner.runSchematic('workspace', workspaceOptions);
return runner.runSchematic('application', { ...appOptions, style }, appTree);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getWorkspace(tree: UnitTestTree): any {
return JSON.parse(tree.readContent('/angular.json'));
}
describe('Tailwind Schematic', () => {
const schematicRunner = new SchematicTestRunner(
'@schematics/angular',
require.resolve('../collection.json'),
);
const appOptions: ApplicationOptions = {
name: 'bar',
inlineStyle: false,
inlineTemplate: false,
routing: false,
style: Style.Css,
skipTests: false,
skipPackageJson: false,
};
let appTree: UnitTestTree;
beforeEach(async () => {
appTree = await createTestApp(schematicRunner, appOptions);
});
it('should add tailwind dependencies', async () => {
const tree = await schematicRunner.runSchematic('tailwind', { project: 'bar' }, appTree);
const packageJson = JSON.parse(tree.readContent('/package.json'));
expect(packageJson.devDependencies['tailwindcss']).toBeDefined();
expect(packageJson.devDependencies['postcss']).toBeDefined();
expect(packageJson.devDependencies['@tailwindcss/postcss']).toBeDefined();
});
it('should add tailwind imports to styles.css', async () => {
const tree = await schematicRunner.runSchematic('tailwind', { project: 'bar' }, appTree);
const stylesContent = tree.readContent('/projects/bar/src/styles.css');
expect(stylesContent).toContain(`@import 'tailwindcss';`);
});
it('should not add duplicate tailwind imports to styles.css', async () => {
let tree = await schematicRunner.runSchematic('tailwind', { project: 'bar' }, appTree);
const stylesContent = tree.readContent('/projects/bar/src/styles.css');
expect(stylesContent.match(/@import 'tailwindcss';/g)?.length).toBe(1);
tree = await schematicRunner.runSchematic('tailwind', { project: 'bar' }, tree);
const stylesContentAfter = tree.readContent('/projects/bar/src/styles.css');
expect(stylesContentAfter.match(/@import 'tailwindcss';/g)?.length).toBe(1);
});
describe('with scss styles', () => {
beforeEach(async () => {
appTree = await createTestApp(schematicRunner, appOptions, Style.Scss);
});
it('should create a tailwind.css file', async () => {
const tree = await schematicRunner.runSchematic('tailwind', { project: 'bar' }, appTree);
expect(tree.exists('/projects/bar/src/tailwind.css')).toBe(true);
const stylesContent = tree.readContent('/projects/bar/src/tailwind.css');
expect(stylesContent).toContain(`@import 'tailwindcss';`);
});
it('should add tailwind.css to angular.json', async () => {
const tree = await schematicRunner.runSchematic('tailwind', { project: 'bar' }, appTree);
const workspace = getWorkspace(tree);
const styles = workspace.projects.bar.architect.build.options.styles;
expect(styles).toEqual(['projects/bar/src/tailwind.css', 'projects/bar/src/styles.scss']);
});
it('should not add tailwind imports to styles.scss', async () => {
const tree = await schematicRunner.runSchematic('tailwind', { project: 'bar' }, appTree);
const stylesContent = tree.readContent('/projects/bar/src/styles.scss');
expect(stylesContent).not.toContain(`@import 'tailwindcss';`);
});
});
describe('with postcss configuration', () => {
it('should create a .postcssrc.json if one does not exist', async () => {
const tree = await schematicRunner.runSchematic('tailwind', { project: 'bar' }, appTree);
expect(tree.exists('/projects/bar/.postcssrc.json')).toBe(true);
});
it('should update an existing .postcssrc.json in the project root', async () => {
appTree.create(
'/projects/bar/.postcssrc.json',
JSON.stringify({ plugins: { autoprefixer: {} } }),
);
const tree = await schematicRunner.runSchematic('tailwind', { project: 'bar' }, appTree);
const postCssConfig = JSON.parse(tree.readContent('/projects/bar/.postcssrc.json'));
expect(postCssConfig.plugins['@tailwindcss/postcss']).toBeDefined();
expect(postCssConfig.plugins['autoprefixer']).toBeDefined();
});
it('should update an existing postcss.config.json in the workspace root', async () => {
appTree.create('/postcss.config.json', JSON.stringify({ plugins: { autoprefixer: {} } }));
const tree = await schematicRunner.runSchematic('tailwind', { project: 'bar' }, appTree);
const postCssConfig = JSON.parse(tree.readContent('/postcss.config.json'));
expect(postCssConfig.plugins['@tailwindcss/postcss']).toBeDefined();
expect(postCssConfig.plugins['autoprefixer']).toBeDefined();
expect(tree.exists('/projects/bar/.postcssrc.json')).toBe(false);
});
});
});