stackpress
Version:
Incept is a content management framework.
233 lines (222 loc) • 9.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = generate;
const helpers_js_1 = require("../../helpers.js");
const samples = [
{
slug: 'foo-bar',
string: 'foobar',
number: 123,
boolean: true,
date: new Date(),
array: ['foo', 'bar'],
object: { foo: 'bar' }
},
{
slug: 'bar-foo',
string: 'barfoo',
number: 321,
boolean: false,
date: new Date(),
array: ['bar', 'foo'],
object: { bar: 'foo' }
},
{
slug: 'foo-zoo',
string: 'foozoo',
number: 123,
boolean: true,
date: new Date(),
array: ['foo', 'zoo'],
object: { foo: 'zoo' }
},
{
slug: 'bar-zoo',
string: 'barzoo',
number: 321,
boolean: false,
date: new Date(),
array: ['bar', 'zoo'],
object: { bar: 'zoo' }
}
];
function generate(directory, registry) {
const models = Array.from(registry.model.values());
const order = (0, helpers_js_1.sequence)(models);
for (const model of order.reverse()) {
const dependents = model.relations
.filter(column => column.model)
.map(column => {
var _a, _b;
return ({
foreign: (_a = column.relation) === null || _a === void 0 ? void 0 : _a.parent.key,
local: (_b = column.relation) === null || _b === void 0 ? void 0 : _b.child.key,
model: column.model
});
});
const inputs = samples.map(sample => {
const input = {};
model.fields.forEach(column => {
const type = column.multiple
? 'array'
: column.type === 'Json'
? 'object'
: column.type === 'Hash'
? 'object'
: column.type === 'Object'
? 'object'
: column.type === 'Number'
? 'number'
: column.type === 'Boolean'
? 'boolean'
: column.type === 'Date'
? 'date'
: 'string';
input[column.name] = sample[type];
});
return input;
});
const source = directory.createSourceFile(`${model.name}/tests/actions.ts`, '', { overwrite: true });
source.addImportDeclaration({
moduleSpecifier: 'mocha',
namedImports: ['describe', 'it']
});
source.addImportDeclaration({
moduleSpecifier: 'chai',
namedImports: ['expect']
});
source.addImportDeclaration({
isTypeOnly: true,
moduleSpecifier: '@stackpress/inquire/Engine',
defaultImport: 'Engine'
});
for (const dependent of dependents) {
source.addImportDeclaration({
moduleSpecifier: `../../${dependent.model.name}/actions/index.js`,
namespaceImport: `${dependent.model.camel}Actions`
});
}
source.addImportDeclaration({
moduleSpecifier: '../actions/index.js',
namedImports: [
'batch',
'create',
'detail',
'get',
'remove',
'restore',
'search',
'update',
'upsert'
]
});
source.addFunction({
isDefaultExport: true,
name: `${model.title}ActionTests`,
parameters: [{ name: 'engine', type: 'Engine' }],
statements: (`
describe('${model.title} Actions', async () => {
${dependents.length > 0 ? (`
const dependents: Record<string, any[]> = {};
before(async () => {
${dependents.map(dependent => {
const name = dependent.local.name;
const actions = `${dependent.model.camel}Actions`;
return `dependents.${name} = ((await ${actions}.search(engine)).results) || [];`;
}).join('\n')}
});
`) : ''}
it('should create ${model.title}', async () => {
${dependents.length > 0
? (`
const ids: Record<string, any> = {};
${dependents.map(dependent => {
const local = dependent.local.name;
const foreign = dependent.foreign.name;
return `ids.${local} = dependents.${local}[0].${foreign};`;
}).join('\n')}
const input = Object.assign({}, ${JSON.stringify(inputs[0])}, ids);
`)
: `const input = ${JSON.stringify(inputs[0])};`}
const actual = await create(engine, input);
expect(actual.code).to.equal(200);
expect(actual.status).to.equal('OK');
});
${dependents.length < 2 ? (`
it('should batch ${model.title}', async () => {
const rows = ${JSON.stringify(inputs.slice(2))};
${dependents.map(dependent => {
const local = dependent.local.name;
const foreign = dependent.foreign.name;
return `
rows[0].${local} = dependents.${local}[2].${foreign};
rows[1].${local} = dependents.${local}[3].${foreign};
`;
}).join('\n')}
const actual = await batch(engine, rows);
expect(actual.code).to.equal(200);
expect(actual.status).to.equal('OK');
});
`) : ''}
it('should search ${model.title}', async () => {
const actual = await search(engine);
expect(actual.code).to.equal(200);
expect(actual.status).to.equal('OK');
expect(Array.isArray(actual.results)).to.be.true;
});
${model.ids.length ? (`
it('should get ${model.title}', async () => {
const response = await search(engine);
const row = response.results?.[0];
const key = '${model.ids[0].name}';
const value = row?.[key] as string;
const actual = await get(engine, key, value);
expect(actual.code).to.equal(200);
expect(actual.status).to.equal('OK');
});
it('should get ${model.title} with ids', async () => {
const response = await search(engine);
const row = response.results?.[0];
const ids: Record<string, string|number> = { ${model.ids.map(column => `${column.name}: row?.${column.name} as string|number`).join(', ')} };
const actual = await detail(engine, ids);
expect(actual.code).to.equal(200);
expect(actual.status).to.equal('OK');
});
it('should update ${model.title}', async () => {
const response = await search(engine);
const row = response.results?.[0];
const ids: Record<string, string|number> = { ${model.ids.map(column => `${column.name}: row?.${column.name} as string|number`).join(', ')} };
const input: Partial<Record<string, any>> = ${JSON.stringify(inputs[1])};
Object.keys(ids).forEach(key => {
delete input[key];
});
${dependents.map(dependent => `delete input.${dependent.local.name};`).join('\n')}
const actual = await update(engine, ids, input);
expect(actual.code).to.equal(200);
expect(actual.status).to.equal('OK');
});
it('should remove ${model.title}', async () => {
const response = await search(engine);
const row = response.results?.[0];
const ids: Record<string, string|number> = { ${model.ids.map(column => `${column.name}: row?.${column.name} as string|number`).join(', ')} };
const actual = await remove(engine, ids);
expect(actual.code).to.equal(200);
expect(actual.status).to.equal('OK');
});
${model.active ? (`
it('should restore ${model.title}', async () => {
const response = await search(engine, { filter: { ${model.active.name}: -1 } });
const row = response.results?.[0];
const ids: Record<string, string|number> = { ${model.ids.map(column => `${column.name}: row?.${column.name} as string|number`).join(', ')} };
const actual = await restore(engine, ids);
expect(actual.code).to.equal(200);
expect(actual.status).to.equal('OK');
});
`) : ''}
`) : ''}
});
`)
});
}
}
;