Skip to content

Commit

Permalink
feat: get back missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dadiorchen committed Feb 24, 2021
1 parent af408ed commit 346161f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
},
Expand Down
51 changes: 32 additions & 19 deletions server/services/TokenService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,56 +27,64 @@ 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');
TokenRepository.prototype.getById.restore();
});

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,
});
});

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);
Expand All @@ -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);
});
});
Expand Down
13 changes: 8 additions & 5 deletions server/services/TransferService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;
Expand All @@ -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);
Expand Down
9 changes: 6 additions & 3 deletions server/services/TrustService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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,
Expand All @@ -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",
Expand Down
9 changes: 6 additions & 3 deletions server/services/WalletService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 346161f

Please sign in to comment.