rob-revisited/ide/ui/js/test-wizard-model.js

81 lines
4.5 KiB
JavaScript

// The test wizard's model (no DOM, tested in test-wizard.test.mjs): the kinds of test, the name and the assertions a new test starts with, the
// assertion palette filtered by kind, and the model sent to the PHP side (ide/src/TestWizard).
export const KINDS = ['unit', 'kernel', 'functional', 'api', 'panther'];
export const KIND_LABELS = {
unit: 'Unit test of a class', kernel: 'Service test (KernelTestCase)', functional: 'Functional test of a route (WebTestCase)',
api: 'API Platform test (ApiTestCase)', panther: 'Browser test (Panther)',
};
const upper = (s) => (s ? s[0].toUpperCase() + s.slice(1) : s);
const camel = (s) => String(s || '').split(/[^A-Za-z0-9]+/).filter(Boolean).map(upper).join('');
export const short = (cls) => String(cls || '').split('\\').pop();
/** the kinds this project can offer (API Platform tests need API Platform) */
export function kindsFor(info) {
return KINDS.filter((k) => k !== 'api' || info.apiPlatform);
}
/** the palette of a kind: unit and kernel tests assert on `$result`, the others on the response / the page */
export function paletteFor(kind, info) {
return (info.assertions || []).filter((a) => a.kinds.includes(kind));
}
/** the code of an assertion as the PHP side writes it ({1} {2}... replaced, a blank argument taking its default) */
export function assertionCode(a, info) {
const spec = (info.assertions || []).find((x) => x.id === a.id);
if (!spec) return '';
const code = spec.code || '';
return code.replace(/\{(\d)\}/g, (_, n) => String((a.args?.[n - 1] ?? '') !== '' ? a.args[n - 1] : spec.args[n - 1]?.default ?? ''));
}
/** a new assertion of the palette with its default arguments */
export function newAssertion(id, info) {
const spec = (info.assertions || []).find((x) => x.id === id);
return { id, args: (spec?.args || []).map((a) => a.default) };
}
/** the name a new test starts with */
export function testName(kind, ctx) {
switch (kind) {
case 'unit': case 'kernel': return `test${upper(ctx.method || 'Works')}`;
case 'functional': case 'panther': return `test${camel(ctx.route?.name || ctx.route?.path || 'Page')}${ctx.route?.method && ctx.route.method !== 'GET' ? upper(ctx.route.method.toLowerCase()) : ''}`;
case 'api': return `test${upper(String(ctx.operation?.method || 'get').toLowerCase())}${camel(ctx.operation?.path || '')}`;
default: return 'testSomething';
}
}
/** a new test of a kind: its assertions start from a useful one */
export function newTest(kind, ctx, info) {
const t = { name: testName(kind, ctx), on: true, asserts: [] };
if (kind === 'unit' || kernel(kind)) Object.assign(t, { method: ctx.method, provider: false });
if (kind === 'functional' || kind === 'panther') Object.assign(t, { route: { name: ctx.route.name, path: ctx.route.path, method: ctx.route.method || 'GET' }, protected: !!ctx.route.protected });
if (kind === 'functional') t.asserts = [newAssertion('isSuccessful', info)];
if (kind === 'panther') t.asserts = [newAssertion('selectorExists', info)];
if (kind === 'api') {
t.operation = { method: ctx.operation.method, path: ctx.operation.path };
t.asserts = [newAssertion(ctx.operation.method === 'POST' ? 'statusCodeSame' : 'isSuccessful', info)];
if (ctx.operation.method === 'POST') t.asserts[0].args = ['201'];
}
if (kind === 'unit' || kind === 'kernel') t.asserts = ctx.returnsVoid ? [] : [newAssertion('notNull', info)];
return t;
}
function kernel(kind) { return kind === 'kernel'; }
/** the model sent to the PHP side: only the ticked tests, the login of the protected routes */
export function cleanModel(state) {
const tests = state.tests.filter((t) => t.on).map(({ on, ...t }) => ({ ...t, asserts: t.asserts.map((a) => ({ id: a.id, args: a.args })), ...(t.protected ? { login: state.login } : {}) }));
return { kind: state.kind, class: state.class, target: state.target || '', tests };
}
/** the default login of the tests of protected routes: the project's user class when it has one, else an in-memory user */
export function defaultLogin(info) {
return { mode: info.userClass ? 'entity' : 'memory', roles: ['ROLE_USER'], identifier: 'tester', userClass: info.userClass || '', property: 'email', value: '[email protected]' };
}
/** the routes the list shows: not the framework's own, filtered by what is typed */
export function filterRoutes(routes, query, { api = false } = {}) {
const q = String(query || '').trim().toLowerCase();
return routes.filter((r) => (api ? r.name.startsWith('_api_') || r.path.startsWith('/api') : !r.name.startsWith('_api_')) && (!q || `${r.name} ${r.path} ${r.controller || ''}`.toLowerCase().includes(q)));
}