consigno-core-tester
Version:
Consigno Core Tester
95 lines (79 loc) • 2.83 kB
JavaScript
const chai = require('chai');
const chaiHttp = require('chai-http');
const forEach = require('lodash/forEach');
chai.use(chaiHttp);
chai.should();
class Http {
constructor(httpServer, optionsDecorator) {
this.server = httpServer;
this.optionsDecorator = optionsDecorator;
this.as = (decorator) => new Http(this.server, decorator);
this.put = (path, body, options) => {
let result = this.request().put(path);
if (body) {
result.send(body);
}
return this.handleRequest(result, options);
};
this.post = (path, body, options) => {
let result = this.request().post(path);
if (body) {
result.send(body);
}
return this.handleRequest(result, options);
};
this.get = (path, options) => this.handleRequest(this.request().get(path), options);
this.delete = (path, options) => this.handleRequest(this.request().delete(path), options);
this.request = () => chai.request(this.server);
this.handleOptions = async (result, options = {}) => {
const resultOptions = await this.decorateOptions(options);
if (resultOptions.headers) {
forEach(resultOptions.headers, (value, key) => result.set(key, value));
}
if (resultOptions.query) {
result.query(resultOptions.query);
}
};
this.handleRequest = async (result, options) => {
await this.handleOptions(result, options);
return new Promise(resolve => result.end((err, res) => resolve(res)));
};
}
decorateOptions(options) {
if (this.optionsDecorator) {
return this.optionsDecorator(options);
}
return this.optionsDecorator ? this.optionsDecorator(options) : options;
}
}
class TestHelper {
constructor(server, config) {
this.database = server.database;
this.config = config;
this.http = new Http(server.server);
this.expect = chai.expect;
this.sync = async () => {
if (this.config.get('DB_DIALECT') === 'mysql') {
const sequelize = this.database.sequelize;
const query = 'SELECT table_name AS name FROM information_schema.tables WHERE table_schema = ?';
const [tables] = await sequelize.query(query, {
replacements: [this.config.get('DB_NAME')],
});
if (tables.length === 0) {
return this.database.sync();
}
let queries = tables.map(table => `DROP TABLE IF EXISTS \`${table.name}\`;`);
queries.unshift('SET foreign_key_checks = 0;');
queries.push('SET foreign_key_checks = 1;');
await sequelize.query(queries.join('\n'));
return this.database.sync();
}
};
this.log = msg => console.log(`☑️ ${msg}`);
this.fatal = msg => {
console.log(`❎ ️ ${msg}`);
throw new Error(msg);
};
}
}
module.exports = TestHelper;