From 8c8e5f762713f5d20d731c33cd7e33b3aa90c14b Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Thu, 20 Feb 2020 14:14:31 +0100 Subject: [PATCH] test(stack/namesystem): Add missing tests and update dependencies --- packages/stack/namesystem/package.json | 22 ++- .../stack/namesystem/test/namesystem.spec.js | 173 ++++++++++++++++++ packages/stack/namesystem/tsconfig.json | 3 + 3 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 packages/stack/namesystem/test/namesystem.spec.js diff --git a/packages/stack/namesystem/package.json b/packages/stack/namesystem/package.json index 3bad49a22..422a5528e 100644 --- a/packages/stack/namesystem/package.json +++ b/packages/stack/namesystem/package.json @@ -40,7 +40,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" @@ -51,14 +52,33 @@ "embark-i18n": "^5.2.3" }, "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", "npm-run-all": "4.1.5", + "jest": "25.1.0", "rimraf": "3.0.0" }, "engines": { "node": ">=10.17.0", "npm": ">=6.11.3", "yarn": ">=1.19.1" + }, + "jest": { + "collectCoverage": true, + "testEnvironment": "node", + "testMatch": [ + "**/test/**/*.js" + ], + "transform": { + "\\.js$": [ + "babel-jest", + { + "rootMode": "upward" + } + ] + } } } diff --git a/packages/stack/namesystem/test/namesystem.spec.js b/packages/stack/namesystem/test/namesystem.spec.js new file mode 100644 index 000000000..faf5dc89a --- /dev/null +++ b/packages/stack/namesystem/test/namesystem.spec.js @@ -0,0 +1,173 @@ +import sinon from 'sinon'; +import assert from 'assert'; +import { fakeEmbark, Plugins } from 'embark-testing'; +import Namesystem from '../src'; + +describe('stack/namesystem', () => { + + let namesystem, embark; + + beforeEach(() => { + const testBed = fakeEmbark({ + namesystemConfig: { + provider: 'testNode', + enabled: true + } + }); + + embark = testBed.embark; + namesystem = new Namesystem(embark); + }); + + afterEach(() => { + embark.teardown(); + sinon.restore(); + }); + + describe('instantiation', () => { + + it('should register namesystem:node:register command handler', () => { + namesystem.events.assert.commandHandlerRegistered('namesystem:node:register'); + }); + + it('should register namesystem:node:start command handler', () => { + namesystem.events.assert.commandHandlerRegistered('namesystem:node:start'); + }); + + it('should register namesystem:resolve command handler', () => { + namesystem.events.assert.commandHandlerRegistered('namesystem:resolve'); + }); + + it('should register namesystem:lookup command handler', () => { + namesystem.events.assert.commandHandlerRegistered('namesystem:lookup'); + }); + + it('should register namesystem:registerSubdomain command handler', () => { + namesystem.events.assert.commandHandlerRegistered('namesystem:registerSubdomain'); + }); + + it('should register module:namesystem:reset command handler', () => { + namesystem.events.assert.commandHandlerRegistered('module:namesystem:reset'); + }); + }); + + + it('should register node', () => { + + const startFunction = sinon.fake(); + const executeCommand = sinon.fake(); + + embark.events.request('namesystem:node:register', 'testNode', startFunction, executeCommand); + assert(namesystem.namesystemNodes['testNode']); + assert.equal(namesystem.namesystemNodes['testNode'].started, false); + }); + + it('should start registered node', done => { + + const startFunction = sinon.spy(cb => cb()); + const executeCommand = sinon.fake(); + + const namesystemConfig = { + provider: 'testNode', + enabled: true + }; + + embark.events.request('namesystem:node:register', 'testNode', startFunction, executeCommand); + embark.events.request('namesystem:node:start', namesystemConfig, err => { + assert(startFunction.calledOnce); + assert.equal(namesystem.namesystemNodes['testNode'].started, true); + done(); + }); + }); + + it('should not start node if namesystem is disabled', done => { + + const startFunction = sinon.fake(); + const executeCommand = sinon.fake(); + + const namesystemConfig = { + provider: 'testNode', + enabled: false + }; + + embark.events.request('namesystem:node:register', 'testNode', startFunction, executeCommand); + embark.events.request('namesystem:node:start', namesystemConfig, err => { + assert(!startFunction.calledOnce); + done(); + }); + }); + + it('should resolve name using registered node', async () => { + + const startFunction = sinon.spy(cb => cb()); + const executeCommand = sinon.spy((method, args, cb) => cb()); + + const namesystemConfig = { + provider: 'testNode', + enabled: true + }; + + embark.events.request('namesystem:node:register', 'testNode', startFunction, executeCommand); + await embark.events.request2('namesystem:node:start', namesystemConfig); + + await embark.events.request2('namesystem:resolve', 'someName'); + assert(executeCommand.calledOnce); + assert(executeCommand.calledWith('resolve', ['someName'])); + }); + + it('should lookup address using registered node', async () => { + + const startFunction = sinon.spy(cb => cb()); + const executeCommand = sinon.spy((method, args, cb) => cb()); + + const namesystemConfig = { + provider: 'testNode', + enabled: true + }; + + embark.events.request('namesystem:node:register', 'testNode', startFunction, executeCommand); + await embark.events.request2('namesystem:node:start', namesystemConfig); + + await embark.events.request2('namesystem:lookup', '0x000'); + assert(executeCommand.calledOnce); + assert(executeCommand.calledWith('lookup', ['0x000'])); + }); + + it('should register subdomain using registered node', async () => { + + const startFunction = sinon.spy(cb => cb()); + const executeCommand = sinon.spy((method, args, cb) => cb()); + + const namesystemConfig = { + provider: 'testNode', + enabled: true + }; + + embark.events.request('namesystem:node:register', 'testNode', startFunction, executeCommand); + await embark.events.request2('namesystem:node:start', namesystemConfig); + + await embark.events.request2('namesystem:registerSubdomain', 'someName', '0x000'); + assert(executeCommand.calledOnce); + assert(executeCommand.calledWith('registerSubdomain', ['someName', '0x000'])); + }); + + it('should reset namesystem', async () => { + + const startFunction = sinon.spy(cb => cb()); + const executeCommand = sinon.spy((method, args, cb) => cb()); + + const namesystemConfig = { + provider: 'testNode', + enabled: true + }; + + embark.events.request('namesystem:node:register', 'testNode', startFunction, executeCommand); + await embark.events.request2('namesystem:node:start', namesystemConfig); + + await embark.events.request2('module:namesystem:reset'); + assert(executeCommand.calledOnce); + assert(executeCommand.calledWith('reset', [])); + assert(startFunction.calledTwice); + }); +}); + diff --git a/packages/stack/namesystem/tsconfig.json b/packages/stack/namesystem/tsconfig.json index a38dc1d0e..64fe00a5d 100644 --- a/packages/stack/namesystem/tsconfig.json +++ b/packages/stack/namesystem/tsconfig.json @@ -12,6 +12,9 @@ "references": [ { "path": "../../core/i18n" + }, + { + "path": "../../utils/testing" } ] }