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

Commit

Permalink
test: updatest test to latest API
Browse files Browse the repository at this point in the history
  • Loading branch information
bohoffi committed Feb 25, 2024
1 parent 9568307 commit 63bf716
Showing 1 changed file with 101 additions and 83 deletions.
184 changes: 101 additions & 83 deletions libs/ngx-doc-gen/src/generators/configure/generator.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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>): [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>
): [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', () => {
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down

0 comments on commit 63bf716

Please sign in to comment.