Skip to content
Merged
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
3 changes: 3 additions & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"builder": "@angular/build:unit-test",
"options": {
"tsConfig": "projects/coreui-angular/tsconfig.spec.json",
"exclude": ["schematics/**"],
"providersFile": "projects/coreui-angular/src/test-providers.ts",
"runnerConfig": true
}
Expand Down Expand Up @@ -84,6 +85,7 @@
"builder": "@angular/build:unit-test",
"options": {
"tsConfig": "projects/coreui-angular-chartjs/tsconfig.spec.json",
"exclude": ["schematics/**"],
"providersFile": "projects/coreui-angular-chartjs/src/test-providers.ts",
"runnerConfig": true,
"setupFiles": ["projects/coreui-angular-chartjs/src/test-setup.spec.ts"]
Expand Down Expand Up @@ -132,6 +134,7 @@
"builder": "@angular/build:unit-test",
"options": {
"tsConfig": "projects/coreui-icons-angular/tsconfig.spec.json",
"exclude": ["schematics/**"],
"providersFile": "projects/coreui-icons-angular/src/test-providers.ts",
"runnerConfig": true
}
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"test:lib:dev": "ng test coreui-angular",
"test:lib:cov": "ng test --watch --coverage coreui-angular ",
"test:lib:prod": "ng test coreui-angular --watch=false",
"prepublish:lib": "npm run prepublish:icons && ng lint coreui-angular && ng test coreui-angular --watch=false && npm run build:lib:prod",
"posttest:lib:prod": "vitest run --config vitest.schematics.config.mts projects/coreui-angular/",
"prepublish:lib": "npm run prepublish:icons && ng lint coreui-angular && npm run test:lib:prod && npm run build:lib:prod",
"publish:lib": "cd dist/coreui-angular/ && npm publish --tag next --dry-run",
"lint:lib": "ng lint coreui-angular",
"link:lib": "cd dist/coreui-angular/ && npm link",
Expand All @@ -24,7 +25,8 @@
"test:chartjs:dev": "ng test coreui-angular-chartjs",
"test:chartjs:cov": "ng test --watch --coverage coreui-angular-chartjs",
"test:chartjs:prod": "ng test coreui-angular-chartjs --watch=false",
"prepublish:chartjs": "ng lint coreui-angular-chartjs && ng test coreui-angular-chartjs --watch=false && npm run build:chartjs:prod",
"posttest:chartjs:prod": "vitest run --config vitest.schematics.config.mts projects/coreui-angular-chartjs/",
"prepublish:chartjs": "ng lint coreui-angular-chartjs && npm run test:chartjs:prod && npm run build:chartjs:prod",
"publish:chartjs": "cd dist/coreui-angular-chartjs/ && npm publish --tag next --dry-run",
"lint:chartjs": "ng lint coreui-angular-chartjs",
"link:chartjs": "cd dist/coreui-angular-chartjs/ && npm link",
Expand All @@ -35,7 +37,8 @@
"test:icons:dev": "ng test coreui-icons-angular",
"test:icons:cov": "ng test --watch --coverage coreui-icons-angular",
"test:icons:prod": "ng test coreui-icons-angular --watch=false",
"prepublish:icons": "ng lint coreui-icons-angular && ng test coreui-icons-angular --watch=false && npm run build:icons:prod",
"posttest:icons:prod": "vitest run --config vitest.schematics.config.mts projects/coreui-icons-angular/",
"prepublish:icons": "ng lint coreui-icons-angular && npm run test:icons:prod && npm run build:icons:prod",
"publish:icons": "cd dist/coreui-icons-angular/ && npm publish --tag next --dry-run",
"lint:icons": "ng lint coreui-icons-angular",
"link:icons": "cd dist/coreui-icons-angular/ && npm link"
Expand Down
71 changes: 71 additions & 0 deletions projects/coreui-angular-chartjs/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { HostTree } from '@angular-devkit/schematics';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { fileURLToPath } from 'node:url';
import { lastValueFrom } from 'rxjs';
import { describe, expect, it } from 'vitest';
import * as pkgJson from '../../package.json';
import collection from '../collection.json';
import { ngAdd } from './index';
import { PackageJson } from './package-config';

const pkg = pkgJson as unknown as PackageJson;

async function runNgAdd(packageJson?: object) {
const runner = new SchematicTestRunner(pkg.name, fileURLToPath(new URL('../collection.json', import.meta.url)));
const tree = new UnitTestTree(new HostTree());
if (packageJson) {
tree.create('package.json', JSON.stringify(packageJson));
}
await lastValueFrom(runner.callRule(ngAdd(), tree));
return { dependencies: JSON.parse(tree.readContent('package.json')).dependencies, tasks: runner.tasks };
}

describe('ng-add', () => {
it('registers the exported ngAdd factory', async () => {
const [path, name] = collection.schematics['ng-add'].factory.split('#');
const factoryModule = await import(/* @vite-ignore */ new URL(`../${path}.ts`, import.meta.url).href);
expect(factoryModule[name]).toBe(ngAdd);
});

it('throws when the project has no package.json', async () => {
await expect(runNgAdd()).rejects.toThrow('@angular/core version not found');
});

it('throws when @angular/core is not a dependency', async () => {
await expect(runNgAdd({ dependencies: { rxjs: '~7.8.0' } })).rejects.toThrow('@angular/core version not found');
});

it('adds the library and its dependencies at the versions the library declares', async () => {
const { dependencies } = await runNgAdd({ dependencies: { '@angular/core': '^22.1.0' } });
expect(dependencies).toStrictEqual({
'@angular/core': '^22.1.0',
'@coreui/chartjs': pkg.peerDependencies['@coreui/chartjs'],
'chart.js': pkg.peerDependencies['chart.js'],
'lodash-es': pkg.dependencies['lodash-es'],
[pkg.name]: `~${pkg.version}`
});
});

it('replaces older versions of the library and its dependencies', async () => {
const { dependencies } = await runNgAdd({
dependencies: {
'@angular/core': '^22.1.0',
'@coreui/chartjs': '^3.0.0',
'chart.js': '^3.0.0',
'lodash-es': '^4.0.0',
[pkg.name]: '~4.0.0'
}
});
expect(dependencies).toMatchObject({
'@coreui/chartjs': pkg.peerDependencies['@coreui/chartjs'],
'chart.js': pkg.peerDependencies['chart.js'],
'lodash-es': pkg.dependencies['lodash-es'],
[pkg.name]: `~${pkg.version}`
});
});

it('schedules a package install', async () => {
const { tasks } = await runNgAdd({ dependencies: { '@angular/core': '^22.1.0' } });
expect(tasks.map((task) => task.name)).toEqual(['node-package']);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,6 @@ export interface PackageJson {
version: string;
}

/**
* Sorts the keys of the given object.
* @returns A new object instance with sorted keys
*/
function sortObjectByKeys(obj: Record<string, string>) {
return Object.keys(obj)
.sort()
.reduce(
(result, key) => {
result[key] = obj[key];
return result;
},
{} as Record<string, string>
);
}

/** Adds a package to the package.json in the given host tree. */
export function addPackageToPackageJson(host: Tree, pkg: string, version: string): Tree {
if (host.exists('package.json')) {
const sourceText = host.read('package.json')!.toString('utf-8');
const json = JSON.parse(sourceText) as PackageJson;

if (!json.dependencies) {
json.dependencies = {};
}

if (!json.dependencies[pkg]) {
json.dependencies[pkg] = version;
json.dependencies = sortObjectByKeys(json.dependencies);
}

host.overwrite('package.json', JSON.stringify(json, null, 2));
}

return host;
}

/** Gets the version of the specified package by looking at the package.json in the given tree. */
export function getPackageVersionFromPackageJson(tree: Tree, name: string): string | null {
if (!tree.exists('package.json')) {
Expand Down
2 changes: 1 addition & 1 deletion projects/coreui-angular-chartjs/tsconfig.schematics.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
"types": ["node"]
},
"include": ["./schematics/**/*"],
"exclude": ["./schematics/*/files/**/*"]
"exclude": ["./schematics/*/files/**/*", "./schematics/**/*.spec.ts"]
}
1 change: 1 addition & 0 deletions projects/coreui-angular-chartjs/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"files": [],
"include": ["**/*.spec.ts", "**/*.d.ts"],
"exclude": ["schematics/**"],
"angularCompilerOptions": {
"extendedDiagnostics": {
"checks": {
Expand Down
95 changes: 95 additions & 0 deletions projects/coreui-angular/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { HostTree } from '@angular-devkit/schematics';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { fileURLToPath } from 'node:url';
import { lastValueFrom } from 'rxjs';
import { describe, expect, it } from 'vitest';
import * as pkgJson from '../../package.json';
import collection from '../collection.json';
import { ngAdd } from './index';
import { PackageJson } from './package-config';

const pkg = pkgJson as unknown as PackageJson;
const coreuiStyles = pkg.name.endsWith('-pro') ? '@coreui/coreui-pro' : '@coreui/coreui';

async function runNgAdd(packageJson?: object) {
const runner = new SchematicTestRunner(pkg.name, fileURLToPath(new URL('../collection.json', import.meta.url)));
const tree = new UnitTestTree(new HostTree());
if (packageJson) {
tree.create('package.json', JSON.stringify(packageJson));
}
await lastValueFrom(runner.callRule(ngAdd(), tree));
return { dependencies: JSON.parse(tree.readContent('package.json')).dependencies, tasks: runner.tasks };
}

describe('ng-add', () => {
it('registers the exported ngAdd factory', async () => {
const [path, name] = collection.schematics['ng-add'].factory.split('#');
const factoryModule = await import(/* @vite-ignore */ new URL(`../${path}.ts`, import.meta.url).href);
expect(factoryModule[name]).toBe(ngAdd);
});

it('throws when the project has no package.json', async () => {
await expect(runNgAdd()).rejects.toThrow('@angular/core version not found');
});

it('throws when @angular/core is not a dependency', async () => {
await expect(runNgAdd({ dependencies: { rxjs: '~7.8.0' } })).rejects.toThrow('@angular/core version not found');
});

it('adds Angular packages at the @angular/core version of the project', async () => {
const { dependencies } = await runNgAdd({ dependencies: { '@angular/core': '^22.1.0' } });
expect(dependencies).toMatchObject({
'@angular/common': '^22.1.0',
'@angular/core': '^22.1.0',
'@angular/forms': '^22.1.0',
'@angular/router': '^22.1.0'
});
});

it('keeps the Angular packages the project already has', async () => {
const angularPackages = {
'@angular/aria': '~22.0.1',
'@angular/cdk': '~22.0.2',
'@angular/common': '~22.0.3',
'@angular/forms': '~22.0.4',
'@angular/router': '~22.0.5'
};
const { dependencies } = await runNgAdd({ dependencies: { '@angular/core': '^22.1.0', ...angularPackages } });
expect(dependencies).toMatchObject(angularPackages);
});

it('adds the library and its dependencies at the versions the library declares', async () => {
const { dependencies } = await runNgAdd({ dependencies: { '@angular/core': '^22.1.0' } });
expect(dependencies).toMatchObject({
'@angular/aria': pkg.peerDependencies['@angular/aria'],
'@angular/cdk': pkg.peerDependencies['@angular/cdk'],
[coreuiStyles]: pkg.peerDependencies[coreuiStyles],
'@coreui/icons-angular': pkg.peerDependencies['@coreui/icons-angular'],
'@popperjs/core': pkg.dependencies['@popperjs/core'],
[pkg.name]: `~${pkg.version}`
});
});

it('replaces older versions of the library and its CoreUI dependencies', async () => {
const { dependencies } = await runNgAdd({
dependencies: {
'@angular/core': '^22.1.0',
[coreuiStyles]: '^4.0.0',
'@coreui/icons-angular': '~4.0.0',
'@popperjs/core': '2.0.0',
[pkg.name]: '~4.0.0'
}
});
expect(dependencies).toMatchObject({
[coreuiStyles]: pkg.peerDependencies[coreuiStyles],
'@coreui/icons-angular': pkg.peerDependencies['@coreui/icons-angular'],
'@popperjs/core': pkg.dependencies['@popperjs/core'],
[pkg.name]: `~${pkg.version}`
});
});

it('schedules a package install', async () => {
const { tasks } = await runNgAdd({ dependencies: { '@angular/core': '^22.1.0' } });
expect(tasks.map((task) => task.name)).toEqual(['node-package']);
});
});
37 changes: 0 additions & 37 deletions projects/coreui-angular/schematics/ng-add/package-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,6 @@ export interface PackageJson {
version: string;
}

/**
* Sorts the keys of the given object.
* @returns A new object instance with sorted keys
*/
function sortObjectByKeys(obj: Record<string, string>) {
return Object.keys(obj)
.sort()
.reduce(
(result, key) => {
result[key] = obj[key];
return result;
},
{} as Record<string, string>
);
}

/** Adds a package to the package.json in the given host tree. */
export function addPackageToPackageJson(host: Tree, pkg: string, version: string): Tree {
if (host.exists('package.json')) {
const sourceText = host.read('package.json')!.toString('utf-8');
const json = JSON.parse(sourceText) as PackageJson;

if (!json.dependencies) {
json.dependencies = {};
}

if (!json.dependencies[pkg]) {
json.dependencies[pkg] = version;
json.dependencies = sortObjectByKeys(json.dependencies);
}

host.overwrite('package.json', JSON.stringify(json, null, 2));
}

return host;
}

/** Gets the version of the specified package by looking at the package.json in the given tree. */
export function getPackageVersionFromPackageJson(tree: Tree, name: string): string | null {
if (!tree.exists('package.json')) {
Expand Down
2 changes: 1 addition & 1 deletion projects/coreui-angular/tsconfig.schematics.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
"types": ["node"]
},
"include": ["./schematics/**/*"],
"exclude": ["./schematics/*/files/**/*"]
"exclude": ["./schematics/*/files/**/*", "./schematics/**/*.spec.ts"]
}
1 change: 1 addition & 0 deletions projects/coreui-angular/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"files": [],
"include": ["**/*.spec.ts", "**/*.d.ts"],
"exclude": ["schematics/**"],
"angularCompilerOptions": {
"extendedDiagnostics": {
"checks": {
Expand Down
Loading
Loading