From 6f90099d47ce39603dfc975ff63102fd0d26f520 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Fri, 11 Oct 2019 22:42:16 -0400 Subject: [PATCH] Add tests for ENS (#1933) * add basic test to ens * add more tests * test arguments for test --- packages/plugins/ens/package.json | 29 +++++++++++-- packages/plugins/ens/test/index.js | 70 ++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 packages/plugins/ens/test/index.js diff --git a/packages/plugins/ens/package.json b/packages/plugins/ens/package.json index 0ad3e8f3c..fe25ccaf5 100644 --- a/packages/plugins/ens/package.json +++ b/packages/plugins/ens/package.json @@ -36,10 +36,8 @@ "lint": "eslint src/", "qa": "npm-run-all lint _build", "reset": "npx rimraf dist embark-*.tgz package", - "solo": "embark-solo" - }, - "eslintConfig": { - "extends": "../../../.eslintrc.json" + "solo": "embark-solo", + "test": "jest" }, "dependencies": { "@babel/runtime-corejs2": "7.6.2", @@ -54,10 +52,33 @@ }, "devDependencies": { "embark-solo": "^4.1.1", + "babel-eslint": "10.0.3", + "babel-jest": "24.9.0", "eslint": "5.7.0", + "eslint-plugin-jest": "22.5.1", + "jest": "24.9.0", "npm-run-all": "4.1.5", "rimraf": "3.0.0" }, + "eslintConfig": { + "env": { + "es6": true, + "node": true + }, + "extends": [ + "eslint:recommended", + "plugin:jest/recommended", + "plugin:jest/style" + ], + "parser": "babel-eslint" + }, + "jest": { + "collectCoverage": true, + "testEnvironment": "node", + "testMatch": [ + "**/test/**/*.js" + ] + }, "engines": { "node": ">=8.12.0 <12.0.0", "npm": ">=6.4.1", diff --git a/packages/plugins/ens/test/index.js b/packages/plugins/ens/test/index.js new file mode 100644 index 000000000..98046a506 --- /dev/null +++ b/packages/plugins/ens/test/index.js @@ -0,0 +1,70 @@ +const ENS = require("../dist/index"); +const { fakeEmbark } = require('embark-testing'); +const {Utils} = require('embarkjs'); +const secureSend = Utils.secureSend; + +describe('embark-ens', () => { + let ens, doneCb; + + const { embark } = fakeEmbark(); + + + beforeEach(() => { + embark.events.setCommandHandler('namesystem:node:register', () => {}); + ens = new ENS(embark); + ens.config = { + embarkConfig: { + generationDir: 'test-dir' + }, + namesystemConfig: { + register: { + rootDomain: 'root.eth' + } + } + }; + }); + + afterEach(() => { + embark.teardown(); + }); + + it("should register the right artifact", (done) => { + const pipelineRegisterHandler = jest.fn((args, cb) => { + expect(args).toEqual({ + path: ['test-dir', 'config'], + file: 'namesystem.json', + format: 'json', + content: Object.assign({}, embark.config.namesystemConfig, ens.getEnsConfig()) + }); + cb(); + done(); + }); + ens.getEnsConfig = jest.fn(); + + embark.events.setCommandHandler('pipeline:register', pipelineRegisterHandler); + + ens.addArtifactFile({}, () => {}); + }); + + describe('safeRegisterSubDomain', () => { + it('should register if the name is not registered', (done) => { + ens.ensResolve = jest.fn((name, cb) => {cb(null, null)}); + ens.registerSubDomain = jest.fn((defaultAccount, subDomainName, reverseNode, address, secureSend, callback) => callback()); + + ens.safeRegisterSubDomain('test.eth', '0x0123', '0x4321', () => { + expect(ens.registerSubDomain).toHaveBeenCalledWith('0x4321', 'test.eth', '0xd523d7aaff8eefa323a17f2c79662ff1a8d952f6fa9cf53986347e99ada8098c', '0x0123', secureSend, expect.any(Function)); + done(); + }); + }); + + it('should not register if the name is already registered', (done) => { + ens.ensResolve = jest.fn((name, cb) => {cb(null, '0x0123')}); + ens.registerSubDomain = jest.fn((defaultAccount, subDomainName, reverseNode, address, secureSend, callback) => callback()); + + ens.safeRegisterSubDomain('test.eth', '0x0123', '0x4321', () => { + expect(ens.registerSubDomain).not.toHaveBeenCalled(); + done(); + }); + }); + }); +});