From 63bf716b4c338dac8960269b64ac311658909f9f Mon Sep 17 00:00:00 2001 From: bohoffi Date: Sun, 25 Feb 2024 09:14:43 +0100 Subject: [PATCH] test: updatest test to latest API --- .../generators/configure/generator.spec.ts | 184 ++++++++++-------- 1 file changed, 101 insertions(+), 83 deletions(-) diff --git a/libs/ngx-doc-gen/src/generators/configure/generator.spec.ts b/libs/ngx-doc-gen/src/generators/configure/generator.spec.ts index de70e24..98f250c 100644 --- a/libs/ngx-doc-gen/src/generators/configure/generator.spec.ts +++ b/libs/ngx-doc-gen/src/generators/configure/generator.spec.ts @@ -1,5 +1,11 @@ -import { addProjectConfiguration, getProjects, ProjectConfiguration, ProjectType, Tree } from '@nrwl/devkit'; -import { createTreeWithEmptyV1Workspace } from '@nrwl/devkit/testing'; +import { + addProjectConfiguration, + getProjects, + ProjectConfiguration, + ProjectType, + Tree, +} from '@nrwl/devkit'; +import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; import { ConfigureGeneratorOptions } from './configure-generator.options'; import configure from './generator'; import { createNonExistingProjectsErrorMessage } from './utils'; @@ -11,43 +17,62 @@ interface TestProject { const createApplication = (projectName: string): TestProject => ({ projectName, - projectConfiguration: createProjectConfig(projectName, 'application', true) + projectConfiguration: createProjectConfig(projectName, 'application', true), }); -const createLibrary = (projectName: string, buildable?: boolean): TestProject => ({ +const createLibrary = ( + projectName: string, + buildable?: boolean +): TestProject => ({ projectName, - projectConfiguration: createProjectConfig(projectName, 'library', buildable) + projectConfiguration: createProjectConfig(projectName, 'library', buildable), }); -const createProjectConfig = (projectName: string, projectType: ProjectType, buildable?: boolean): ProjectConfiguration => { +const createProjectConfig = ( + projectName: string, + projectType: ProjectType, + buildable?: boolean +): ProjectConfiguration => { const configuration: ProjectConfiguration = { root: `${projectType === 'application' ? 'apps' : 'libs'}/${projectName}`, - projectType + projectType, }; if (buildable) { configuration.targets = { build: { - executor: projectType === 'application' - ? '@angular-devkit/build-angular:browser' - : '@nrwl/angular:package' - } + executor: + projectType === 'application' + ? '@angular-devkit/build-angular:browser' + : '@nrwl/angular:package', + }, }; } return configuration; }; -const getAffectedProjects = (workspace: Map): [string, ProjectConfiguration][] => Array.from(workspace.entries()) - .filter(([, projectConfiguration]) => !!projectConfiguration.targets?.['doc-gen']); - -const checkResult = (expected: { - affected: string[] -}, actual: [string, ProjectConfiguration][]): void => { +const getAffectedProjects = ( + workspace: Map +): [string, ProjectConfiguration][] => + Array.from(workspace.entries()).filter( + ([, projectConfiguration]) => !!projectConfiguration.targets?.['doc-gen'] + ); + +const checkResult = ( + expected: { + affected: string[]; + }, + actual: [string, ProjectConfiguration][] +): void => { // check length expect(actual).toHaveLength(expected.affected.length); // check expected project was configured - expect(expected.affected.every(p => actual.map(([projectName]) => projectName).includes(p))).toBeTruthy(); + expect( + expected.affected.every((p) => + actual.map(([projectName]) => projectName).includes(p) + ) + ).toBeTruthy(); }; describe('configure generator', () => { @@ -65,21 +90,21 @@ describe('configure generator', () => { configureOptions = {}; workspace = new Map(); configuredWorkspace = new Map(); - workspaceTree = createTreeWithEmptyV1Workspace(); + workspaceTree = createTreeWithEmptyWorkspace(); }); describe('using default options (`{}`)', () => { - it('should configure no projects as there are no libraries', async () => { - // setup workspace const workspaceProjects: TestProject[] = [ createApplication('demo'), createApplication('administration'), - createApplication('client-side') + createApplication('client-side'), ]; - workspaceProjects.forEach(tp => workspace.set(tp.projectName, tp.projectConfiguration)); + workspaceProjects.forEach((tp) => + workspace.set(tp.projectName, tp.projectConfiguration) + ); createWorkspace(workspace); @@ -89,23 +114,25 @@ describe('configure generator', () => { const affectedProjects = getAffectedProjects(configuredWorkspace); - checkResult({ - affected: [] - }, + checkResult( + { + affected: [], + }, affectedProjects ); }); it('should configure no projects as there are no "buildable" libraries', async () => { - // setup workspace const workspaceProjects: TestProject[] = [ createLibrary('lib-a'), createLibrary('lib-b'), - createLibrary('lib-c') + createLibrary('lib-c'), ]; - workspaceProjects.forEach(tp => workspace.set(tp.projectName, tp.projectConfiguration)); + workspaceProjects.forEach((tp) => + workspace.set(tp.projectName, tp.projectConfiguration) + ); createWorkspace(workspace); @@ -115,24 +142,26 @@ describe('configure generator', () => { const affectedProjects = getAffectedProjects(configuredWorkspace); - checkResult({ - affected: [] - }, + checkResult( + { + affected: [], + }, affectedProjects ); }); it('should configure "buildable" projects when libraries', async () => { - // setup workspace const workspaceProjects: TestProject[] = [ createApplication('demo'), createApplication('administration'), createLibrary('lib-a', true), - createLibrary('lib-b') + createLibrary('lib-b'), ]; - workspaceProjects.forEach(tp => workspace.set(tp.projectName, tp.projectConfiguration)); + workspaceProjects.forEach((tp) => + workspace.set(tp.projectName, tp.projectConfiguration) + ); createWorkspace(workspace); @@ -142,34 +171,31 @@ describe('configure generator', () => { const affectedProjects = getAffectedProjects(configuredWorkspace); - checkResult({ - affected: [ - 'lib-a' - ] - }, + checkResult( + { + affected: ['lib-a'], + }, affectedProjects ); }); }); describe('using passed options', () => { - it('should configure no projects as passed projects are no libraries', async () => { configureOptions = { - projects: [ - 'administration', - 'client-side' - ] + projects: ['administration', 'client-side'], }; // setup workspace const workspaceProjects: TestProject[] = [ createApplication('demo'), createApplication('administration'), - createApplication('client-side') + createApplication('client-side'), ]; - workspaceProjects.forEach(tp => workspace.set(tp.projectName, tp.projectConfiguration)); + workspaceProjects.forEach((tp) => + workspace.set(tp.projectName, tp.projectConfiguration) + ); createWorkspace(workspace); @@ -179,30 +205,29 @@ describe('configure generator', () => { const affectedProjects = getAffectedProjects(configuredWorkspace); - checkResult({ - affected: [] - }, + checkResult( + { + affected: [], + }, affectedProjects ); }); it('should configure no projects as passed projects are no "buildable" libraries', async () => { configureOptions = { - projects: [ - 'lib-a', - 'lib-b', - 'lib-c' - ] + projects: ['lib-a', 'lib-b', 'lib-c'], }; // setup workspace const workspaceProjects: TestProject[] = [ createLibrary('lib-a'), createLibrary('lib-b'), - createLibrary('lib-c') + createLibrary('lib-c'), ]; - workspaceProjects.forEach(tp => workspace.set(tp.projectName, tp.projectConfiguration)); + workspaceProjects.forEach((tp) => + workspace.set(tp.projectName, tp.projectConfiguration) + ); createWorkspace(workspace); @@ -212,30 +237,29 @@ describe('configure generator', () => { const affectedProjects = getAffectedProjects(configuredWorkspace); - checkResult({ - affected: [] - }, + checkResult( + { + affected: [], + }, affectedProjects ); }); it('should configure only "buildable" libraries', async () => { configureOptions = { - projects: [ - 'lib-a', - 'lib-b', - 'lib-c' - ] + projects: ['lib-a', 'lib-b', 'lib-c'], }; // setup workspace const workspaceProjects: TestProject[] = [ createLibrary('lib-a'), createLibrary('lib-b', true), - createLibrary('lib-c') + createLibrary('lib-c'), ]; - workspaceProjects.forEach(tp => workspace.set(tp.projectName, tp.projectConfiguration)); + workspaceProjects.forEach((tp) => + workspace.set(tp.projectName, tp.projectConfiguration) + ); createWorkspace(workspace); @@ -245,38 +269,32 @@ describe('configure generator', () => { const affectedProjects = getAffectedProjects(configuredWorkspace); - checkResult({ - affected: [ - 'lib-b' - ] - }, + checkResult( + { + affected: ['lib-b'], + }, affectedProjects ); }); }); describe('error handling', () => { - it('should throw an error if a non existing project is passed in options', async () => { - const nonExistingProjects = [ - 'lib-b', - 'lib-c' - ]; + const nonExistingProjects = ['lib-b', 'lib-c']; configureOptions = { - projects: [ - 'lib-a', - ...nonExistingProjects - ] + projects: ['lib-a', ...nonExistingProjects], }; // setup workspace const workspaceProjects: TestProject[] = [ createApplication('administration'), createApplication('client-side'), - createLibrary('lib-a') + createLibrary('lib-a'), ]; - workspaceProjects.forEach(tp => workspace.set(tp.projectName, tp.projectConfiguration)); + workspaceProjects.forEach((tp) => + workspace.set(tp.projectName, tp.projectConfiguration) + ); createWorkspace(workspace);