diff --git a/packages/plugins/specialconfigs/package.json b/packages/plugins/specialconfigs/package.json index 70ded8448..fec98ce50 100644 --- a/packages/plugins/specialconfigs/package.json +++ b/packages/plugins/specialconfigs/package.json @@ -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" + } + ] + } } } diff --git a/packages/plugins/specialconfigs/test/specialconfigs.spec.js b/packages/plugins/specialconfigs/test/specialconfigs.spec.js new file mode 100644 index 000000000..8a0b2f891 --- /dev/null +++ b/packages/plugins/specialconfigs/test/specialconfigs.spec.js @@ -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(); + }); + }); + }); +}); diff --git a/packages/plugins/specialconfigs/tsconfig.json b/packages/plugins/specialconfigs/tsconfig.json index 728af9796..9491342cf 100644 --- a/packages/plugins/specialconfigs/tsconfig.json +++ b/packages/plugins/specialconfigs/tsconfig.json @@ -12,6 +12,9 @@ "references": [ { "path": "../../core/i18n" + }, + { + "path": "../../utils/testing" } ] } diff --git a/packages/utils/testing/src/plugin.js b/packages/utils/testing/src/plugin.js index 5890b053e..be9caefa3 100644 --- a/packages/utils/testing/src/plugin.js +++ b/packages/utils/testing/src/plugin.js @@ -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);