From e97a8e7cf500fbec863fe53c376cda8b772be0ea Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sun, 6 Dec 2020 15:41:39 -0800 Subject: [PATCH] fix(slp utils): refactoring unit tests --- package-lock.json | 118 +++++++++++++++++++++++------------------ package.json | 1 + src/slp/utils.js | 1 + test/unit/address.js | 3 +- test/unit/slp-utils.js | 108 ++++++++++++++----------------------- 5 files changed, 111 insertions(+), 120 deletions(-) diff --git a/package-lock.json b/package-lock.json index 48e2189..75bd26c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@psf/bch-js", - "version": "3.4.0", + "version": "4.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -4445,6 +4445,12 @@ "integrity": "sha1-+CbJtOKoUR2E46yinbBeGk87cqk=", "dev": true }, + "lodash.clonedeep": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz", + "integrity": "sha1-4j8/nE+Pvd6HJSnBBxhXoIblzO8=", + "dev": true + }, "lodash.escaperegexp": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/lodash.escaperegexp/-/lodash.escaperegexp-4.1.2.tgz", @@ -5253,9 +5259,9 @@ } }, "node-fetch": { - "version": "2.6.0", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", - "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==", + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", + "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==", "dev": true }, "node-mocks-http": { @@ -5301,9 +5307,9 @@ "dev": true }, "npm": { - "version": "6.14.7", - "resolved": "https://registry.npmjs.org/npm/-/npm-6.14.7.tgz", - "integrity": "sha512-swhsdpNpyXg4GbM6LpOQ6qaloQuIKizZ+Zh6JPXJQc59ka49100Js0WvZx594iaKSoFgkFq2s8uXFHS3/Xy2WQ==", + "version": "6.14.9", + "resolved": "https://registry.npmjs.org/npm/-/npm-6.14.9.tgz", + "integrity": "sha512-yHi1+i9LyAZF1gAmgyYtVk+HdABlLy94PMIDoK1TRKWvmFQAt5z3bodqVwKvzY0s6dLqQPVsRLiwhJfNtiHeCg==", "dev": true, "requires": { "JSONStream": "^1.3.5", @@ -5370,7 +5376,7 @@ "lodash.uniq": "~4.5.0", "lodash.without": "~4.4.0", "lru-cache": "^5.1.1", - "meant": "~1.0.1", + "meant": "^1.0.2", "mississippi": "^3.0.0", "mkdirp": "^0.5.5", "move-concurrently": "^1.0.1", @@ -5385,8 +5391,8 @@ "npm-packlist": "^1.4.8", "npm-pick-manifest": "^3.0.2", "npm-profile": "^4.0.4", - "npm-registry-fetch": "^4.0.5", - "npm-user-validate": "~1.0.0", + "npm-registry-fetch": "^4.0.7", + "npm-user-validate": "^1.0.1", "npmlog": "~4.1.2", "once": "~1.4.0", "opener": "^1.5.1", @@ -5461,17 +5467,6 @@ "humanize-ms": "^1.2.1" } }, - "ajv": { - "version": "5.5.2", - "bundled": true, - "dev": true, - "requires": { - "co": "^4.6.0", - "fast-deep-equal": "^1.0.0", - "fast-json-stable-stringify": "^2.0.0", - "json-schema-traverse": "^0.3.0" - } - }, "ansi-align": { "version": "2.0.0", "bundled": true, @@ -5802,11 +5797,6 @@ "mkdirp": "~0.5.0" } }, - "co": { - "version": "4.6.0", - "bundled": true, - "dev": true - }, "code-point-at": { "version": "1.1.0", "bundled": true, @@ -5898,11 +5888,11 @@ } }, "configstore": { - "version": "3.1.2", + "version": "3.1.5", "bundled": true, "dev": true, "requires": { - "dot-prop": "^4.1.0", + "dot-prop": "^4.2.1", "graceful-fs": "^4.1.2", "make-dir": "^1.0.0", "unique-string": "^1.0.0", @@ -6078,7 +6068,7 @@ } }, "dot-prop": { - "version": "4.2.0", + "version": "4.2.1", "bundled": true, "dev": true, "requires": { @@ -6255,11 +6245,6 @@ "bundled": true, "dev": true }, - "fast-deep-equal": { - "version": "1.1.0", - "bundled": true, - "dev": true - }, "fast-json-stable-stringify": { "version": "2.0.0", "bundled": true, @@ -6580,12 +6565,35 @@ "dev": true }, "har-validator": { - "version": "5.1.0", + "version": "5.1.5", "bundled": true, "dev": true, "requires": { - "ajv": "^5.3.0", + "ajv": "^6.12.3", "har-schema": "^2.0.0" + }, + "dependencies": { + "ajv": { + "version": "6.12.6", + "bundled": true, + "dev": true, + "requires": { + "fast-deep-equal": "^3.1.1", + "fast-json-stable-stringify": "^2.0.0", + "json-schema-traverse": "^0.4.1", + "uri-js": "^4.2.2" + } + }, + "fast-deep-equal": { + "version": "3.1.3", + "bundled": true, + "dev": true + }, + "json-schema-traverse": { + "version": "0.4.1", + "bundled": true, + "dev": true + } } }, "has": { @@ -6872,11 +6880,6 @@ "bundled": true, "dev": true }, - "json-schema-traverse": { - "version": "0.3.1", - "bundled": true, - "dev": true - }, "json-stringify-safe": { "version": "5.0.1", "bundled": true, @@ -7224,7 +7227,7 @@ } }, "meant": { - "version": "1.0.1", + "version": "1.0.2", "bundled": true, "dev": true }, @@ -7249,6 +7252,11 @@ "brace-expansion": "^1.1.7" } }, + "minimist": { + "version": "1.2.5", + "bundled": true, + "dev": true + }, "minizlib": { "version": "1.3.3", "bundled": true, @@ -7485,7 +7493,7 @@ } }, "npm-registry-fetch": { - "version": "4.0.5", + "version": "4.0.7", "bundled": true, "dev": true, "requires": { @@ -7514,7 +7522,7 @@ } }, "npm-user-validate": { - "version": "1.0.0", + "version": "1.0.1", "bundled": true, "dev": true }, @@ -7859,13 +7867,6 @@ "ini": "~1.3.0", "minimist": "^1.2.0", "strip-json-comments": "~2.0.1" - }, - "dependencies": { - "minimist": { - "version": "1.2.5", - "bundled": true, - "dev": true - } } }, "read": { @@ -8525,6 +8526,21 @@ "xdg-basedir": "^3.0.0" } }, + "uri-js": { + "version": "4.4.0", + "bundled": true, + "dev": true, + "requires": { + "punycode": "^2.1.0" + }, + "dependencies": { + "punycode": { + "version": "2.1.1", + "bundled": true, + "dev": true + } + } + }, "url-parse-lax": { "version": "1.0.0", "bundled": true, diff --git a/package.json b/package.json index e0d0142..de0f794 100644 --- a/package.json +++ b/package.json @@ -85,6 +85,7 @@ "eslint-config-prettier": "^6.0.0", "eslint-plugin-node": "^9.1.0", "eslint-plugin-prettier": "^3.1.0", + "lodash.clonedeep": "^4.5.0", "mocha": "^6.1.4", "nock": "^10.0.6", "node-mocks-http": "^1.7.0", diff --git a/src/slp/utils.js b/src/slp/utils.js index 3f50ddc..7688c56 100644 --- a/src/slp/utils.js +++ b/src/slp/utils.js @@ -196,6 +196,7 @@ class Utils { async list(id) { let path let method + if (!id) { method = "get" path = `${this.restURL}slp/list` diff --git a/test/unit/address.js b/test/unit/address.js index 57beb97..1f61c29 100644 --- a/test/unit/address.js +++ b/test/unit/address.js @@ -338,7 +338,7 @@ describe("#address.js", () => { }) }) }) - /* + describe("address format detection", () => { describe("#isLegacyAddress", () => { describe("is legacy", () => { @@ -932,5 +932,4 @@ describe("#address.js", () => { }) }) }) - */ }) diff --git a/test/unit/slp-utils.js b/test/unit/slp-utils.js index 3f5dd4c..2baf628 100644 --- a/test/unit/slp-utils.js +++ b/test/unit/slp-utils.js @@ -1,9 +1,7 @@ // Public npm libraries -const assert = require("assert") -const assert2 = require("chai").assert -// const nock = require("nock") // http call mocking +const assert = require("chai").assert const sinon = require("sinon") -// const axios = require("axios") +const cloneDeep = require("lodash.clonedeep") // Unit under test const SLP = require("../../src/slp/slp") @@ -15,7 +13,8 @@ const REST_URL = process.env.RESTURL : "https://bchn.fullstack.cash/v4/" // Mock data used for unit tests -const mockData = require("./fixtures/slp/mock-utils") +const mockDataLib = require("./fixtures/slp/mock-utils") +let mockData // Default to unit tests unless some other value for TEST is passed. if (!process.env.TEST) process.env.TEST = "unit" @@ -27,6 +26,8 @@ describe("#SLP Utils", () => { // Activate nock if it's inactive. // if (!nock.isActive()) nock.activate() + mockData = cloneDeep(mockDataLib) + sandbox = sinon.createSandbox() uut = new SLP({ @@ -45,69 +46,42 @@ describe("#SLP Utils", () => { }) describe("#list", () => { - it("should do something", () => { - console.log("hello world") - }) - // - // it(`should list single SLP token by id`, async () => { - // // Mock the call to the REST API - // // if (process.env.TEST === "unit") { - // // nock(SERVER) - // // .get(uri => uri.includes("/")) - // // .reply(200, mockData.mockToken) - // // } - // - // const tokenId = - // "4276533bb702e7f8c9afd8aa61ebf016e95011dc3d54e55faa847ac1dd461e84" - // - // console.log(`uut.restURL: ${uut.restURL}`) - // - // const list = await uut.Utils.list(tokenId) - // console.log(`list: ${JSON.stringify(list, null, 2)}`) - // - // // assert.equal(list.id, tokenId) - // }) + it(`should list single SLP token by id`, async () => { + sandbox + .stub(uut.Utils.axios, "get") + .resolves({ data: mockData.mockToken }) - // it(`should list multiple SLP tokens by array of ids`, async () => { - // // Mock the call to the REST API - // if (process.env.TEST === "unit") { - // nock(SERVER) - // .post(uri => uri.includes("/")) - // .reply(200, mockData.mockTokens) - // } - // - // const tokenIds = [ - // "4276533bb702e7f8c9afd8aa61ebf016e95011dc3d54e55faa847ac1dd461e84", - // "b3f4f132dc3b9c8c96316346993a8d54d729715147b7b11aa6c8cd909e955313" - // ] - // - // const list = await uut.Utils.list(tokenIds) - // // console.log(`list: ${JSON.stringify(list, null, 2)}`) - // - // assert2.hasAllKeys(list[0], [ - // "blockCreated", - // "blockLastActiveMint", - // "blockLastActiveSend", - // "circulatingSupply", - // "containsBaton", - // "decimals", - // "documentHash", - // "documentUri", - // "id", - // "initialTokenQty", - // "mintingBatonStatus", - // "name", - // "symbol", - // "timestamp", - // "timestampUnix", - // "totalBurned", - // "totalMinted", - // "txnsSinceGenesis", - // "validAddresses", - // "versionType" - // ]) - // assert.equal(list[0].id, tokenIds[0]) - // }) + const tokenId = + "4276533bb702e7f8c9afd8aa61ebf016e95011dc3d54e55faa847ac1dd461e84" + + const list = await uut.Utils.list(tokenId) + // console.log(`list: ${JSON.stringify(list, null, 2)}`) + + assert.equal(list.id, tokenId) + assert.property(list, "decimals") + assert.property(list, "symbol") + assert.property(list, "documentUri") + assert.property(list, "name") + }) + + it(`should list multiple SLP tokens by array of ids`, async () => { + // Mock the call to the REST API + sandbox + .stub(uut.Utils.axios, "post") + .resolves({ data: mockData.mockList }) + + const tokenIds = [ + "4276533bb702e7f8c9afd8aa61ebf016e95011dc3d54e55faa847ac1dd461e84", + "8fc284dcbc922f7bb7e2a443dc3af792f52923bba403fcf67ca028c88e89da0e" + ] + + const list = await uut.Utils.list(tokenIds) + // console.log(`list: ${JSON.stringify(list, null, 2)}`) + + assert.isArray(list) + assert.property(list[0], "symbol") + assert.property(list[1], "symbol") + }) }) /* describe("#balancesForAddress", () => {