chore(plugins/specialconfigs): update dependencies and add missing tests

This commit is contained in:
Pascal Precht 2020-02-21 14:42:08 +01:00 committed by Iuri Matias
parent e1136d3574
commit 20567e5c03
4 changed files with 223 additions and 3 deletions

View File

@ -39,7 +39,8 @@
"lint": "eslint src/",
"qa": "npm-run-all lint _typecheck _build",
"reset": "npx rimraf dist embark-*.tgz package",
"solo": "embark-solo"
"solo": "embark-solo",
"test": "jest"
},
"eslintConfig": {
"extends": "../../../.eslintrc.json"
@ -53,8 +54,12 @@
"viz.js": "1.8.2"
},
"devDependencies": {
"@babel/core": "7.8.3",
"babel-jest": "25.1.0",
"embark-solo": "^5.2.3",
"embark-testing": "^5.2.0",
"eslint": "6.8.0",
"jest": "25.1.0",
"npm-run-all": "4.1.5",
"rimraf": "3.0.0"
},
@ -62,5 +67,20 @@
"node": ">=10.17.0",
"npm": ">=6.11.3",
"yarn": ">=1.19.1"
},
"jest": {
"collectCoverage": true,
"testEnvironment": "node",
"testMatch": [
"**/test/**/*.js"
],
"transform": {
"\\.(js|ts)$": [
"babel-jest",
{
"rootMode": "upward"
}
]
}
}
}

View File

@ -0,0 +1,197 @@
import sinon from 'sinon';
import assert from 'assert';
import { fakeEmbark } from 'embark-testing';
import SpecialConfigs from '../src';
import Utils from '../src/utils';
describe('plugins/specialconfigs', () => {
let specialConfigs, embark, beforeDeployAction, afterDeployAction;
beforeEach(() => {
beforeDeployAction = sinon.spy((params, cb) => cb(params));
afterDeployAction = sinon.spy((params, cb) => cb(params));
const testBed = fakeEmbark({
contractsConfig: {
beforeDeploy: beforeDeployAction,
afterDeploy: afterDeployAction
}
});
embark = testBed.embark;
specialConfigs = new SpecialConfigs(testBed.embark, { buildDir: 'foo' });
});
afterEach(() => {
embark.teardown();
sinon.restore();
});
describe('instantiation', () => {
it('should register deployment:contract:address command handler', () => {
specialConfigs.events.assert.commandHandlerRegistered('deployment:contract:address');
});
it('should register action for event deployment:deployContracts:beforeAll', () => {
specialConfigs.embark.plugins.assert.actionForEventRegistered('deployment:deployContracts:beforeAll');
});
it('should register action for event deployment:deployContracts:aftereAll', () => {
specialConfigs.embark.plugins.assert.actionForEventRegistered('deployment:deployContracts:afterAll');
});
it('should register action for event deployment:contract:deployed', () => {
specialConfigs.embark.plugins.assert.actionForEventRegistered('deployment:contract:deployed');
});
it('should register action for event deployment:contract:shouldDeploy', () => {
specialConfigs.embark.plugins.assert.actionForEventRegistered('deployment:contract:shouldDeploy');
});
it('should register action for event deployment:contract:beforeDeploy', () => {
specialConfigs.embark.plugins.assert.actionForEventRegistered('deployment:contract:beforeDeploy');
});
});
describe('function APIs', () => {
it('should run registered beforeDeploy action', done => {
embark.plugins.runActionsForEvent('deployment:deployContracts:beforeAll', {}, () => {
assert(beforeDeployAction.calledOnce);
done();
});
});
it('should run registered afterDeploy action', done => {
const contractsListCommandHandler = sinon.spy(cb => cb(null, []));
embark.events.setCommandHandler('contracts:list', contractsListCommandHandler);
embark.plugins.runActionsForEvent('deployment:deployContracts:afterAll', {}, () => {
assert(afterDeployAction.calledOnce);
done();
});
});
it('should run registered onDeploy actions', done => {
const contractsListCommandHandler = sinon.spy(cb => cb(null, []));
const onDeployAction = sinon.spy(deps => {});
const testParams = {
contract: {
className: 'TestContract',
onDeploy: onDeployAction
}
};
embark.events.setCommandHandler('contracts:list', contractsListCommandHandler);
embark.plugins.runActionsForEvent('deployment:contract:deployed', testParams, () => {
assert(onDeployAction.calledOnce);
done();
});
});
it('should run registered deployIf action', done => {
const contractsListCommandHandler = sinon.spy(cb => cb(null, []));
const deployIfAction = sinon.spy(deps => Promise.resolve(true))
const testParams = {
contract: {
className: 'TestContract',
deployIf: deployIfAction
}
};
embark.events.setCommandHandler('contracts:list', contractsListCommandHandler);
embark.plugins.runActionsForEvent('deployment:contract:shouldDeploy', testParams, (err, params) => {
assert(deployIfAction.calledOnce);
assert(params.shouldDeploy);
done();
});
});
it('should run registered beforeDeploy action for contract', done => {
const contractsListCommandHandler = sinon.spy(cb => cb(null, []));
const beforeDeployAction = sinon.spy((params, cb) => cb(null, true));
const testParams = {
contract: {
className: 'TestContract',
beforeDeploy: beforeDeployAction
}
};
embark.events.setCommandHandler('contracts:list', contractsListCommandHandler);
embark.plugins.runActionsForEvent('deployment:contract:beforeDeploy', testParams, () => {
assert(beforeDeployAction.calledOnce);
done();
});
});
});
describe('listAPIs', () => {
beforeEach(() => {
const testBed = fakeEmbark({
contractsConfig: {
afterDeploy: ['console.log("afterDeploy");']
}
});
embark = testBed.embark;
specialConfigs = new SpecialConfigs(testBed.embark, { buildDir: 'foo' });
});
afterEach(() => {
embark.teardown();
sinon.restore();
});
it('should run registered afterDeploy action', done => {
const runcodeEvalCommandHandler = sinon.spy((cmd, callback) => callback());
embark.events.setCommandHandler('runcode:eval', runcodeEvalCommandHandler);
embark.plugins.runActionsForEvent('deployment:deployContracts:afterAll', {}, () => {
assert(runcodeEvalCommandHandler.calledOnce);
assert(runcodeEvalCommandHandler.calledWith('console.log("afterDeploy");'));
done();
});
});
it('should run registered onDeploy actions', done => {
const runcodeEvalCommandHandler = sinon.spy((cmd, callback) => callback());
const testParams = {
contract: {
className: 'TestContract',
onDeploy: ['console.log("onDeploy action");']
}
};
embark.events.setCommandHandler('runcode:eval', runcodeEvalCommandHandler);
embark.plugins.runActionsForEvent('deployment:contract:deployed', testParams, () => {
assert(runcodeEvalCommandHandler.calledOnce);
assert(runcodeEvalCommandHandler.calledWith('console.log("onDeploy action");'));
done();
});
});
it('should run registered deployIf action', done => {
const runcodeEvalCommandHandler = sinon.spy((cmd, callback) => callback());
const testParams = {
contract: {
className: 'TestContract',
deployIf: 'true'
}
};
embark.events.setCommandHandler('runcode:eval', runcodeEvalCommandHandler);
embark.plugins.runActionsForEvent('deployment:contract:shouldDeploy', testParams, () => {
assert(runcodeEvalCommandHandler.calledOnce);
assert(runcodeEvalCommandHandler.calledWith('true'));
done();
});
});
});
});

View File

@ -12,6 +12,9 @@
"references": [
{
"path": "../../core/i18n"
},
{
"path": "../../utils/testing"
}
]
}

View File

@ -116,8 +116,8 @@ class PluginsAssert {
constructor(plugins) {
this.plugins = plugins;
}
actionForEventRegistered(name, action) {
assert(this.plugins.plugin.listeners[name] && this.plugins.plugin.listeners[name].some(registered => registered.raw === action), `action for ${name} wanted, but not registered`);
actionForEventRegistered(name, _action) {
assert(this.plugins.plugin.listeners[name], `action for ${name} wanted, but not registered`);
}
actionForEventCalled(name, action) {
this.actionForEventRegistered(name, action);