diff --git a/angular.json b/angular.json index 1b26be98..b1d1b982 100644 --- a/angular.json +++ b/angular.json @@ -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 } @@ -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"] @@ -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 } diff --git a/package.json b/package.json index 27f15054..defd3d6c 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -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" diff --git a/projects/coreui-angular-chartjs/schematics/ng-add/index.spec.ts b/projects/coreui-angular-chartjs/schematics/ng-add/index.spec.ts new file mode 100644 index 00000000..9aed04e5 --- /dev/null +++ b/projects/coreui-angular-chartjs/schematics/ng-add/index.spec.ts @@ -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']); + }); +}); diff --git a/projects/coreui-angular-chartjs/schematics/ng-add/package-config.ts b/projects/coreui-angular-chartjs/schematics/ng-add/package-config.ts index 95c3c1d1..a51d8cae 100644 --- a/projects/coreui-angular-chartjs/schematics/ng-add/package-config.ts +++ b/projects/coreui-angular-chartjs/schematics/ng-add/package-config.ts @@ -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) { - return Object.keys(obj) - .sort() - .reduce( - (result, key) => { - result[key] = obj[key]; - return result; - }, - {} as Record - ); -} - -/** 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')) { diff --git a/projects/coreui-angular-chartjs/tsconfig.schematics.json b/projects/coreui-angular-chartjs/tsconfig.schematics.json index 01a4040b..94ca3a75 100644 --- a/projects/coreui-angular-chartjs/tsconfig.schematics.json +++ b/projects/coreui-angular-chartjs/tsconfig.schematics.json @@ -23,5 +23,5 @@ "types": ["node"] }, "include": ["./schematics/**/*"], - "exclude": ["./schematics/*/files/**/*"] + "exclude": ["./schematics/*/files/**/*", "./schematics/**/*.spec.ts"] } diff --git a/projects/coreui-angular-chartjs/tsconfig.spec.json b/projects/coreui-angular-chartjs/tsconfig.spec.json index 249738c6..d70f4e2e 100644 --- a/projects/coreui-angular-chartjs/tsconfig.spec.json +++ b/projects/coreui-angular-chartjs/tsconfig.spec.json @@ -10,6 +10,7 @@ }, "files": [], "include": ["**/*.spec.ts", "**/*.d.ts"], + "exclude": ["schematics/**"], "angularCompilerOptions": { "extendedDiagnostics": { "checks": { diff --git a/projects/coreui-angular/schematics/ng-add/index.spec.ts b/projects/coreui-angular/schematics/ng-add/index.spec.ts new file mode 100644 index 00000000..7693f5ed --- /dev/null +++ b/projects/coreui-angular/schematics/ng-add/index.spec.ts @@ -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']); + }); +}); diff --git a/projects/coreui-angular/schematics/ng-add/package-config.ts b/projects/coreui-angular/schematics/ng-add/package-config.ts index 95c3c1d1..a51d8cae 100644 --- a/projects/coreui-angular/schematics/ng-add/package-config.ts +++ b/projects/coreui-angular/schematics/ng-add/package-config.ts @@ -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) { - return Object.keys(obj) - .sort() - .reduce( - (result, key) => { - result[key] = obj[key]; - return result; - }, - {} as Record - ); -} - -/** 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')) { diff --git a/projects/coreui-angular/tsconfig.schematics.json b/projects/coreui-angular/tsconfig.schematics.json index 4ab4726d..dc1abf98 100644 --- a/projects/coreui-angular/tsconfig.schematics.json +++ b/projects/coreui-angular/tsconfig.schematics.json @@ -23,5 +23,5 @@ "types": ["node"] }, "include": ["./schematics/**/*"], - "exclude": ["./schematics/*/files/**/*"] + "exclude": ["./schematics/*/files/**/*", "./schematics/**/*.spec.ts"] } diff --git a/projects/coreui-angular/tsconfig.spec.json b/projects/coreui-angular/tsconfig.spec.json index 249738c6..d70f4e2e 100644 --- a/projects/coreui-angular/tsconfig.spec.json +++ b/projects/coreui-angular/tsconfig.spec.json @@ -10,6 +10,7 @@ }, "files": [], "include": ["**/*.spec.ts", "**/*.d.ts"], + "exclude": ["schematics/**"], "angularCompilerOptions": { "extendedDiagnostics": { "checks": { diff --git a/projects/coreui-icons-angular/schematics/ng-add/index.spec.ts b/projects/coreui-icons-angular/schematics/ng-add/index.spec.ts new file mode 100644 index 00000000..dfa9194c --- /dev/null +++ b/projects/coreui-icons-angular/schematics/ng-add/index.spec.ts @@ -0,0 +1,63 @@ +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 the Angular packages it needs at the @angular/core version of the project', async () => { + const { dependencies } = await runNgAdd({ dependencies: { '@angular/core': '^22.1.0' } }); + expect(dependencies).toStrictEqual({ + '@angular/common': '^22.1.0', + '@angular/core': '^22.1.0', + '@angular/platform-browser': '^22.1.0', + [pkg.name]: `~${pkg.version}` + }); + }); + + it('keeps the Angular packages the project already has', async () => { + const angularPackages = { '@angular/common': '~22.0.3', '@angular/platform-browser': '~22.0.5' }; + const { dependencies } = await runNgAdd({ dependencies: { '@angular/core': '^22.1.0', ...angularPackages } }); + expect(dependencies).toMatchObject(angularPackages); + }); + + it('replaces an older version of the library', async () => { + const { dependencies } = await runNgAdd({ dependencies: { '@angular/core': '^22.1.0', [pkg.name]: '~4.0.0' } }); + expect(dependencies[pkg.name]).toBe(`~${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']); + }); +}); diff --git a/projects/coreui-icons-angular/schematics/ng-add/package-config.ts b/projects/coreui-icons-angular/schematics/ng-add/package-config.ts index 95c3c1d1..a51d8cae 100644 --- a/projects/coreui-icons-angular/schematics/ng-add/package-config.ts +++ b/projects/coreui-icons-angular/schematics/ng-add/package-config.ts @@ -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) { - return Object.keys(obj) - .sort() - .reduce( - (result, key) => { - result[key] = obj[key]; - return result; - }, - {} as Record - ); -} - -/** 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')) { diff --git a/projects/coreui-icons-angular/tsconfig.schematics.json b/projects/coreui-icons-angular/tsconfig.schematics.json index c759ee9d..e11276c6 100644 --- a/projects/coreui-icons-angular/tsconfig.schematics.json +++ b/projects/coreui-icons-angular/tsconfig.schematics.json @@ -23,5 +23,5 @@ "types": ["node"] }, "include": ["./schematics/**/*"], - "exclude": ["./schematics/*/files/**/*"] + "exclude": ["./schematics/*/files/**/*", "./schematics/**/*.spec.ts"] } diff --git a/projects/coreui-icons-angular/tsconfig.spec.json b/projects/coreui-icons-angular/tsconfig.spec.json index ad7a72b6..11561c01 100644 --- a/projects/coreui-icons-angular/tsconfig.spec.json +++ b/projects/coreui-icons-angular/tsconfig.spec.json @@ -10,6 +10,7 @@ }, "files": [], "include": ["**/*.spec.ts", "**/*.d.ts"], + "exclude": ["schematics/**"], "angularCompilerOptions": { "extendedDiagnostics": { "checks": { diff --git a/vitest.schematics.config.mts b/vitest.schematics.config.mts new file mode 100644 index 00000000..036b88f0 --- /dev/null +++ b/vitest.schematics.config.mts @@ -0,0 +1,8 @@ +import { defineConfig } from 'vitest/config'; + +export default defineConfig({ + test: { + environment: 'node', + include: ['projects/*/schematics/**/*.spec.ts'] + } +});