rob-revisited/ide/ui/js/api-platform-model.test.mjs

60 lines
3.9 KiB
JavaScript

// The forms of the API Platform window: how the arguments of an attribute become form values and back (api-platform-model.js).
// Run: docker run --rm -v $PWD:/w -w /w node:22-alpine node --test ide/ui/js/api-platform-model.test.mjs
import test from 'node:test';
import assert from 'node:assert/strict';
import { readField, writeField, modelOf, changed, FIELD, OP_FIELDS } from './api-platform-model.js';
test('a context is read as its groups, the other keys stay in the code', () => {
const args = { normalizationContext: { groups: ['book:read'], enable_max_depth: true }, denormalizationContext: { groups: ['book:write'] } };
assert.deepEqual(readField(args, 'normalizationGroups'), { value: ['book:read'] });
assert.deepEqual(readField(args, 'denormalizationGroups'), { value: ['book:write'] });
assert.deepEqual(readField(args, 'validationGroups'), { value: [] });
});
test('an expression is locked: the form shows it and never writes it', () => {
const args = { security: { '@expr': 'self::RULE' }, normalizationContext: { groups: [{ '@expr': 'Book::READ' }] }, paginationItemsPerPage: { '@expr': '5 * 2' } };
assert.deepEqual(readField(args, 'security'), { locked: 'self::RULE' });
assert.equal(readField(args, 'normalizationGroups').locked, '[{"@expr":"Book::READ"}]');
const model = modelOf(args, ['security', 'normalizationGroups', 'paginationItemsPerPage', 'shortName']);
assert.deepEqual(Object.keys(model.locked), ['security', 'normalizationGroups', 'paginationItemsPerPage']);
model.m.shortName = 'Livre';
assert.deepEqual(changed(model, ['security', 'normalizationGroups', 'paginationItemsPerPage', 'shortName']), { shortName: 'Livre' });
});
test('values of every kind go back to the arguments they came from', () => {
const args = { paginationEnabled: false, paginationItemsPerPage: 25, input: false, processor: 'App\\State\\P', formats: ['jsonld', 'json'], order: { title: 'ASC' },
uriVariables: { authorId: { '@class': 'ApiPlatform\\Metadata\\Link', fromClass: 'App\\Entity\\Author', toProperty: 'author' } } };
const keys = ['paginationEnabled', 'paginationItemsPerPage', 'input', 'processor', 'formats', 'order', 'uriVariables'];
const model = modelOf(args, keys);
assert.equal(model.m.paginationEnabled, 'false');
assert.equal(model.m.input, 'false');
assert.deepEqual(model.m.order, [['title', 'ASC']]);
assert.deepEqual(model.m.uriVariables, [['authorId', 'App\\Entity\\Author', '', 'author']]);
assert.deepEqual(changed(model, keys), {}, 'nothing touched, nothing to write');
assert.equal(writeField('paginationEnabled', 'false'), false);
assert.equal(writeField('paginationEnabled', ''), null);
assert.equal(writeField('paginationItemsPerPage', '30'), 30);
assert.equal(writeField('paginationItemsPerPage', ''), null);
assert.equal(writeField('input', 'false'), false);
assert.equal(writeField('processor', ''), null);
assert.deepEqual(writeField('formats', []), null);
assert.deepEqual(writeField('order', [['title', 'DESC'], ['', 'ASC']]), { title: 'DESC' });
assert.deepEqual(writeField('uriVariables', [['bookId', 'App\\Entity\\Book', 'author', ''], ['', '', '', '']]), { bookId: { fromClass: 'App\\Entity\\Book', fromProperty: 'author' } });
assert.deepEqual(writeField('normalizationGroups', ['a']), ['a']);
});
test('only what changed is sent, and a new operation sends every filled field', () => {
const model = modelOf({ security: "is_granted('ROLE_ADMIN')" }, OP_FIELDS);
model.m.security = '';
model.m.name = 'publish';
assert.deepEqual(changed(model, OP_FIELDS), { security: null, name: 'publish' });
const fresh = modelOf({}, OP_FIELDS);
fresh.m.processor = 'App\\State\\P';
assert.deepEqual(changed(fresh, OP_FIELDS, true), { processor: 'App\\State\\P' });
});
test('every field of the forms has a label and a kind', () => {
for (const [k, [label, kind]] of Object.entries(FIELD)) {
assert.ok(label && ['text', 'int', 'tri', 'class', 'groups', 'formats', 'order', 'links'].includes(kind), k);
}
});