|
| 1 | +// @vitest-environment jsdom |
| 2 | +/** |
| 3 | + * SPDX-License-Identifier: EUPL-1.2 |
| 4 | + * Copyright (C) 2026 Conduction B.V. |
| 5 | + * |
| 6 | + * The application form offers only the licences its licence type allows, and |
| 7 | + * the organisation form only the collaboration types its type allows: the |
| 8 | + * library's CnFormDialog (from @conduction/nextcloud-vue 2.181.0, the version |
| 9 | + * package.json requires) narrows its options with the |
| 10 | + * x-openregister-dependent-values tables of lib/Settings/register.d/ |
| 11 | + * dependent-field-options.json. |
| 12 | + * |
| 13 | + * The methods run with the merged schema as `this`, so the test reads the |
| 14 | + * library's real narrowing over the app's real tables. |
| 15 | + * |
| 16 | + * @spec openspec/changes/landscape-dependent-field-options/specs/dependent-field-options/spec.md#requirement-req-dfo-003-the-form-offers-only-the-allowed-options |
| 17 | + */ |
| 18 | +import * as fs from 'fs' |
| 19 | +import * as path from 'path' |
| 20 | +import { describe, expect, it } from 'vitest' |
| 21 | + |
| 22 | +const root = path.resolve(__dirname, '../..') |
| 23 | +const read = (file) => JSON.parse(fs.readFileSync(path.join(root, file), 'utf8')) |
| 24 | + |
| 25 | +const monolith = read('lib/Settings/softwarecatalogus_register.json') |
| 26 | +const tables = read('lib/Settings/register.d/dependent-field-options.json') |
| 27 | + |
| 28 | +/** |
| 29 | + * A schema of the monolith with the fragment's dependent-values tables merged in. |
| 30 | + * |
| 31 | + * @param {string} slug The schema slug. |
| 32 | + * @return {object} The merged schema. |
| 33 | + */ |
| 34 | +function mergedSchema(slug) { |
| 35 | + const base = monolith.components.schemas[slug] |
| 36 | + const overlay = tables.components.schemas[slug].properties |
| 37 | + const properties = { ...base.properties } |
| 38 | + for (const [key, value] of Object.entries(overlay)) { |
| 39 | + properties[key] = { ...properties[key], ...value } |
| 40 | + } |
| 41 | + return { ...base, properties } |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * The options the form offers for a field, given the controlling value. |
| 46 | + * |
| 47 | + * @param {object} methods CnFormDialog's methods. |
| 48 | + * @param {object} schema The schema. |
| 49 | + * @param {string} key The dependent field. |
| 50 | + * @param {object} formData The form values. |
| 51 | + * @return {Array<string>} The offered option values. |
| 52 | + */ |
| 53 | +function offered(methods, schema, key, formData) { |
| 54 | + const context = { schema, formData } |
| 55 | + context.dependentValuesOf = methods.dependentValuesOf.bind(context) |
| 56 | + context.dependentAllowed = methods.dependentAllowed.bind(context) |
| 57 | + const property = schema.properties[key] |
| 58 | + const values = property.enum || property.items?.enum || [] |
| 59 | + const options = values.map((value) => ({ id: value, label: value })) |
| 60 | + return methods.narrowToDependentValues.call(context, { key }, options).map((option) => option.id) |
| 61 | +} |
| 62 | + |
| 63 | +const { default: CnFormDialog } = await import('../../node_modules/@conduction/nextcloud-vue/src/components/CnFormDialog/CnFormDialog.vue') |
| 64 | +const { methods } = CnFormDialog |
| 65 | + |
| 66 | +describe('the package the form support comes from', () => { |
| 67 | + it('requires a nextcloud-vue whose CnFormDialog reads the dependent-values tables', () => { |
| 68 | + const pkg = read('package.json') |
| 69 | + expect(pkg.dependencies['@conduction/nextcloud-vue']).toMatch(/^\^2\.(18[1-9]|19\d|[2-9]\d\d)\./) |
| 70 | + expect(typeof methods.narrowToDependentValues).toBe('function') |
| 71 | + expect(typeof methods.dependentAllowed).toBe('function') |
| 72 | + }) |
| 73 | +}) |
| 74 | + |
| 75 | +describe('the application form', () => { |
| 76 | + const schema = mergedSchema('module') |
| 77 | + |
| 78 | + it('offers no licence when the licence type is Closed source', () => { |
| 79 | + expect(offered(methods, schema, 'licence', { licentietype: 'Closed source' })).toEqual([]) |
| 80 | + }) |
| 81 | + |
| 82 | + it('offers the open-source licences when the licence type is Open source', () => { |
| 83 | + const licences = offered(methods, schema, 'licence', { licentietype: 'Open source' }) |
| 84 | + expect(licences).toEqual(tables.components.schemas.module.properties.licence['x-openregister-dependent-values'].allowed['Open source']) |
| 85 | + expect(licences).toContain('European Union Public Licence (EUPL), version 1.2') |
| 86 | + }) |
| 87 | + |
| 88 | + it('leaves the licence open while no licence type is chosen', () => { |
| 89 | + const all = schema.properties.licence.enum || schema.properties.licence.items?.enum |
| 90 | + expect(offered(methods, schema, 'licence', {})).toEqual(all) |
| 91 | + }) |
| 92 | +}) |
| 93 | + |
| 94 | +describe('the organisation form', () => { |
| 95 | + const schema = mergedSchema('organization') |
| 96 | + |
| 97 | + it('offers collaboration types only for a collaboration', () => { |
| 98 | + expect(offered(methods, schema, 'samenwerkingtype', { type: 'Municipality' })).toEqual([]) |
| 99 | + expect(offered(methods, schema, 'samenwerkingtype', { type: 'Collaboration' })).toContain('Shared Service Center') |
| 100 | + }) |
| 101 | +}) |
0 commit comments