From 346161f0bbf31a1cab85c26efd02b2edad62e6be Mon Sep 17 00:00:00 2001 From: dadiorchen Date: Wed, 24 Feb 2021 06:32:41 +0000 Subject: [PATCH] feat: get back missing tests --- package-lock.json | 2 +- package.json | 4 +- server/services/TokenService.spec.js | 51 ++++++++++++++++--------- server/services/TransferService.spec.js | 13 ++++--- server/services/TrustService.spec.js | 9 +++-- server/services/WalletService.spec.js | 9 +++-- 6 files changed, 55 insertions(+), 33 deletions(-) diff --git a/package-lock.json b/package-lock.json index 81438704..d13f4503 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "treetracker", - "version": "1.6.1", + "version": "1.8.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 41c86650..458ebf90 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "eslint:fix": "npm run eslint -- --fix", "test": "npm run test-unit; npm run test-integration;npm run test-repository", "test-unit": "NODE_ENV=test DOTENV_CONFIG_PATH=.env.test mocha -r dotenv/config --exit --require co-mocha ./server/models/**/*.spec.js ./server/routes/**/*.spec.js", - "test-unit-ci": "NODE_ENV=test DOTENV_CONFIG_PATH=.env.ci mocha -r dotenv/config --exit --require co-mocha ./server/models/**/*.spec.js ./server/routes/**/*.spec.js", + "test-unit-ci": "NODE_ENV=test DOTENV_CONFIG_PATH=.env.ci mocha -r dotenv/config --exit --require co-mocha --ignore './server/repositories/**/*.spec.js' './server/setup.js' './server/**/*.spec.js'", "test-repository": "NODE_ENV=test DOTENV_CONFIG_PATH=.env.test mocha -r dotenv/config --exit --require co-mocha ./server/repositories/**/*.spec.js", "server-test": "DEBUG=loopback:*,express:* NODE_LOG_LEVEL=debug nodemon server/serverTest.js", "server": "nodemon server/server.js", @@ -20,7 +20,7 @@ "test-integration": "NODE_ENV=test DOTENV_CONFIG_PATH=.env.test mocha -r dotenv/config --exit --timeout 20000 --require co-mocha './__tests__/*.spec.js'", "test-integration-ci": "NODE_ENV=test DOTENV_CONFIG_PATH=.env.ci mocha -r dotenv/config --exit --timeout 20000 --require co-mocha './__tests__/*.spec.js'", "test-watch": "NODE_ENV=test NODE_LOG_LEVEL=info mocha -r dotenv/config dotenv_config_path=.env.test --timeout 10000 --require co-mocha -w -b --ignore './server/repositories/**/*.spec.js' './server/setup.js' './server/**/*.spec.js' './__tests__/seed.spec.js' './__tests__/supertest.js'", - "test-watch-debug": "NODE_ENV=test NODE_LOG_LEVEL=debug DOTENV_CONFIG_PATH=.env.test mocha -r dotenv/config --timeout 10000 --require co-mocha -w -b --ignore './server/repositories/**/*.spec.js' './server/setup.js' './server/**/*.spec.js' './__tests__/seed.spec.js' './__tests__/supertest.js'", + "test-watch-debug": "NODE_ENV=test NODE_LOG_LEVEL=debug DOTENV_CONFIG_PATH=.env.test mocha -r dotenv/config --timeout 10000 --require co-mocha -w -b --ignore './server/repositories/**/*.spec.js' './server/setup.js' './server/**/*.spec.js' './__tests__/seed.spec.js' './__tests__/*.spec.js'", "prettier-fix": "prettier ./ --write", "db-migrate-ci": "cd database; db-migrate up" }, diff --git a/server/services/TokenService.spec.js b/server/services/TokenService.spec.js index bd1f28fb..90eb405f 100644 --- a/server/services/TokenService.spec.js +++ b/server/services/TokenService.spec.js @@ -12,6 +12,7 @@ const Token = require("../models/Token"); const WalletService = require("../services/WalletService"); const Session = require("../models/Session"); const TransactionRepository = require("../repositories/TransactionRepository"); +const uuid = require('uuid'); describe("Token", () => { let tokenService; @@ -26,7 +27,7 @@ describe("Token", () => { }); it("getById() with id which doesn't exist, should throw 404", async () => { - dsinon.stub(TokenRepository.prototype, "getById").rejects(new HttpError(404, "not found")); + sinon.stub(TokenRepository.prototype, "getById").rejects(new HttpError(404, "not found")); await jestExpect(async () => { await tokenService.getById("testUuid"); }).rejects.toThrow('not found'); @@ -34,17 +35,19 @@ describe("Token", () => { }); it("getTokensByBundle", async () => { - const wallet = new Wallet(1, session); + const walletId1 = uuid.v4(); + const tokenId1 = uuid.v4(); + const wallet = new Wallet(walletId1, session); const fn = sinon.stub(TokenRepository.prototype, "getByFilter").resolves([ { - id: 1, + id: tokenId1, } ], session); const result = await tokenService.getTokensByBundle(wallet, 1); expect(result).a("array").lengthOf(1); expect(result[0]).instanceOf(Token); expect(fn).calledWith({ - wallet_id: 1, + wallet_id: walletId1, transfer_pending: false, },{ limit: 1, @@ -52,30 +55,36 @@ describe("Token", () => { }); it("countTokenByWallet", async () => { - const wallet = new Wallet(1, session); + const walletId1 = uuid.v4(); + const wallet = new Wallet(walletId1, session); const fn = sinon.stub(TokenRepository.prototype, "countByFilter").resolves(1); const result = await tokenService.countTokenByWallet(wallet); expect(result).eq(1); expect(fn).calledWith({ - wallet_id: 1, + wallet_id: walletId1, }); fn.restore(); }); it("convertToResponse", async () => { + const transactionId1 = uuid.v4(); + const tokenId1 = uuid.v4(); + const walletId1 = uuid.v4(); + const walletId2 = uuid.v4(); + const captureId1 = uuid.v4(); const transactionObject = { - id: 1, - token_id: 1, - source_wallet_id: 1, - destination_wallet_id: 1, + id: transactionId1, + token_id: tokenId1, + source_wallet_id: walletId1, + destination_wallet_id: walletId2, } sinon.stub(TokenService.prototype, "getById").resolves(new Token({ - id: 1, + id: tokenId1, uuid: "xxx", - capture_id: 1, + capture_id: captureId1, })); sinon.stub(WalletService.prototype, "getById").resolves(new Wallet({ - id: 1, + id: walletId1, name: "testName", })); const result = await tokenService.convertToResponse(transactionObject); @@ -87,18 +96,22 @@ describe("Token", () => { describe("getTokensByTransferId", () => { it("Successfuly", async () => { - const token = new Token({id:2}); + const tokenId1 = uuid.v4(); + const tokenId2 = uuid.v4(); + const transferId1 = uuid.v4(); + const transactionId1 = uuid.v4(); + const token = new Token({id:tokenId2}); const transaction = { - id: 1, - token_id: 2, + id: transactionId1, + token_id: tokenId2, }; const fn = sinon.stub(TransactionRepository.prototype, "getByFilter").resolves([transaction]); const fn2 = sinon.stub(TokenService.prototype, "getById").resolves(token); - const tokens = await tokenService.getTokensByTransferId(1); + const tokens = await tokenService.getTokensByTransferId(transferId1); expect(fn).calledWith({ - transfer_id: 1, + transfer_id: transferId1, }) - expect(fn2).calledWith(2); + expect(fn2).calledWith(tokenId2); expect(tokens).lengthOf(1); }); }); diff --git a/server/services/TransferService.spec.js b/server/services/TransferService.spec.js index 6d568b91..2a1e16f9 100644 --- a/server/services/TransferService.spec.js +++ b/server/services/TransferService.spec.js @@ -8,6 +8,7 @@ const sinonChai = require("sinon-chai"); chai.use(sinonChai); const {expect} = chai; const Session = require("../models/Session"); +const uuid = require('uuid'); describe("TransferService", () => { let transferService ; @@ -22,14 +23,16 @@ describe("TransferService", () => { }); it("convertToResponse", async () => { + const transferId1 = uuid.v4(); + const walletId1 = uuid.v4(); const transferObject = { - id: 1, - originator_wallet_id: 1, - source_wallet_id: 1, - destination_wallet_id: 1, + id: transferId1, + originator_wallet_id: walletId1, + source_wallet_id: walletId1, + destination_wallet_id: walletId1, } sinon.stub(WalletService.prototype, "getById").resolves(new Wallet({ - id: 1, + id: walletId1, name: "testName", })); const result = await transferService.convertToResponse(transferObject); diff --git a/server/services/TrustService.spec.js b/server/services/TrustService.spec.js index de6a61e6..bae5dda5 100644 --- a/server/services/TrustService.spec.js +++ b/server/services/TrustService.spec.js @@ -7,6 +7,7 @@ const chai = require("chai"); const sinonChai = require("sinon-chai"); chai.use(sinonChai); const {expect} = chai; +const uuid = require('uuid'); describe("TrustService", () => { let trustService = new TrustService(); @@ -33,6 +34,8 @@ describe("TrustService", () => { } */ it("convertToResponse", async () => { + const trustId1 = uuid.v4(); + const walletId1 = uuid.v4(); const trustObject = { "actor_wallet_id": 10, "target_wallet_id": 11, @@ -43,15 +46,15 @@ describe("TrustService", () => { "created_at": "2020-10-16T07:36:21.955Z", "updated_at": "2020-10-16T07:36:21.955Z", "active": null, - "id": 1, + "id": trustId1, } sinon.stub(WalletService.prototype, "getById").resolves(new Wallet({ - id: 1, + id: walletId1, name: "testName", })); const result = await trustService.convertToResponse(trustObject); expect(result).deep.eq({ - "id": 1, + "id": trustId1, "actor_wallet": "testName", "target_wallet": "testName", "originating_wallet": "testName", diff --git a/server/services/WalletService.spec.js b/server/services/WalletService.spec.js index c1488d73..408499c0 100644 --- a/server/services/WalletService.spec.js +++ b/server/services/WalletService.spec.js @@ -4,6 +4,7 @@ const sinon = require("sinon"); const {expect} = require("chai"); const Wallet = require("../models/Wallet"); const Session = require("../models/Session"); +const uuid = require('uuid'); describe("WalletService", () => { let walletService; @@ -14,15 +15,17 @@ describe("WalletService", () => { }) it("getById", async () => { - sinon.stub(WalletRepository.prototype, "getById").resolves({id:1}); + const walletId1 = uuid.v4(); + sinon.stub(WalletRepository.prototype, "getById").resolves({id:walletId1}); expect(walletService).instanceOf(WalletService); - const wallet = await walletService.getById(1); + const wallet = await walletService.getById(walletId1); expect(wallet).instanceOf(Wallet); WalletRepository.prototype.getById.restore(); }); it("getByName", async () => { - sinon.stub(WalletRepository.prototype, "getByName").resolves({id:1}); + const walletId1 = uuid.v4(); + sinon.stub(WalletRepository.prototype, "getByName").resolves({id:walletId1}); expect(walletService).instanceOf(WalletService); const wallet = await walletService.getByName("test"); expect(wallet).instanceOf(Wallet);