Skip to content

Commit f5e6894

Browse files
committed
test(@schematics/angular): refactor tailwind schematic tests for clarity
The tests for the Tailwind schematic have been refactored to improve readability and reduce boilerplate code. Helper functions (`createTestApp`, `getWorkspace`) have been introduced to handle the repetitive setup of the test application and the parsing of the workspace configuration. The existing tests were updated to use these new helpers, making them more concise and easier to maintain.
1 parent a217e08 commit f5e6894

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

packages/schematics/angular/tailwind/index_spec.ts

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,33 @@ import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/te
1010
import { Schema as ApplicationOptions, Style } from '../application/schema';
1111
import { Schema as WorkspaceOptions } from '../workspace/schema';
1212

13-
describe('Tailwind Schematic', () => {
14-
const schematicRunner = new SchematicTestRunner(
15-
'@schematics/angular',
16-
require.resolve('../collection.json'),
17-
);
18-
13+
async function createTestApp(
14+
runner: SchematicTestRunner,
15+
appOptions: ApplicationOptions,
16+
style = Style.Css,
17+
): Promise<UnitTestTree> {
1918
const workspaceOptions: WorkspaceOptions = {
2019
name: 'workspace',
2120
newProjectRoot: 'projects',
2221
version: '6.0.0',
2322
};
2423

24+
const appTree = await runner.runSchematic('workspace', workspaceOptions);
25+
26+
return runner.runSchematic('application', { ...appOptions, style }, appTree);
27+
}
28+
29+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
30+
function getWorkspace(tree: UnitTestTree): any {
31+
return JSON.parse(tree.readContent('/angular.json'));
32+
}
33+
34+
describe('Tailwind Schematic', () => {
35+
const schematicRunner = new SchematicTestRunner(
36+
'@schematics/angular',
37+
require.resolve('../collection.json'),
38+
);
39+
2540
const appOptions: ApplicationOptions = {
2641
name: 'bar',
2742
inlineStyle: false,
@@ -35,8 +50,7 @@ describe('Tailwind Schematic', () => {
3550
let appTree: UnitTestTree;
3651

3752
beforeEach(async () => {
38-
appTree = await schematicRunner.runSchematic('workspace', workspaceOptions);
39-
appTree = await schematicRunner.runSchematic('application', appOptions, appTree);
53+
appTree = await createTestApp(schematicRunner, appOptions);
4054
});
4155

4256
it('should add tailwind dependencies', async () => {
@@ -47,17 +61,6 @@ describe('Tailwind Schematic', () => {
4761
expect(packageJson.devDependencies['@tailwindcss/postcss']).toBeDefined();
4862
});
4963

50-
it('should create a .postcssrc.json file in the project root', async () => {
51-
const tree = await schematicRunner.runSchematic('tailwind', { project: 'bar' }, appTree);
52-
expect(tree.exists('/projects/bar/.postcssrc.json')).toBe(true);
53-
});
54-
55-
it('should configure tailwindcss plugin in .postcssrc.json', async () => {
56-
const tree = await schematicRunner.runSchematic('tailwind', { project: 'bar' }, appTree);
57-
const postCssConfig = JSON.parse(tree.readContent('/projects/bar/.postcssrc.json'));
58-
expect(postCssConfig.plugins['@tailwindcss/postcss']).toBeDefined();
59-
});
60-
6164
it('should add tailwind imports to styles.css', async () => {
6265
const tree = await schematicRunner.runSchematic('tailwind', { project: 'bar' }, appTree);
6366
const stylesContent = tree.readContent('/projects/bar/src/styles.css');
@@ -76,12 +79,7 @@ describe('Tailwind Schematic', () => {
7679

7780
describe('with scss styles', () => {
7881
beforeEach(async () => {
79-
appTree = await schematicRunner.runSchematic('workspace', workspaceOptions);
80-
appTree = await schematicRunner.runSchematic(
81-
'application',
82-
{ ...appOptions, style: Style.Scss },
83-
appTree,
84-
);
82+
appTree = await createTestApp(schematicRunner, appOptions, Style.Scss);
8583
});
8684

8785
it('should create a tailwind.css file', async () => {
@@ -93,8 +91,8 @@ describe('Tailwind Schematic', () => {
9391

9492
it('should add tailwind.css to angular.json', async () => {
9593
const tree = await schematicRunner.runSchematic('tailwind', { project: 'bar' }, appTree);
96-
const angularJson = JSON.parse(tree.readContent('/angular.json'));
97-
const styles = angularJson.projects.bar.architect.build.options.styles;
94+
const workspace = getWorkspace(tree);
95+
const styles = workspace.projects.bar.architect.build.options.styles;
9896
expect(styles).toEqual(['projects/bar/src/tailwind.css', 'projects/bar/src/styles.scss']);
9997
});
10098

0 commit comments

Comments
 (0)