|
| 1 | +// imports |
| 2 | +import axios from "axios" |
| 3 | +import { |
| 4 | + CashAccountCheckResult, |
| 5 | + CashAccountLookupResult, |
| 6 | + CashAccountReverseLookupResult |
| 7 | +} from "bitcoin-com-rest" |
| 8 | +import * as chai from "chai" |
| 9 | +import * as util from "util" |
| 10 | +import { BITBOX, resturl } from "../../lib/BITBOX" |
| 11 | +import { CashAccounts } from "../../lib/CashAccounts" |
| 12 | + |
| 13 | +// consts |
| 14 | +const bitbox: BITBOX = new BITBOX() |
| 15 | +const assert: Chai.AssertStatic = chai.assert |
| 16 | + |
| 17 | +// TODO: port from require to import syntax |
| 18 | +const sinon = require("sinon") |
| 19 | +const cashAccountsMock = require("./fixtures/cashaccounts-mock.js") |
| 20 | + |
| 21 | +util.inspect.defaultOptions = { |
| 22 | + showHidden: true, |
| 23 | + colors: true, |
| 24 | + depth: 3 |
| 25 | +} |
| 26 | + |
| 27 | +describe("#CashAccounts", (): void => { |
| 28 | + describe("#CashAccountsConstructor", (): void => { |
| 29 | + it("should create instance of CashAccounts", (): void => { |
| 30 | + const cashAccounts: CashAccounts = new CashAccounts() |
| 31 | + assert.equal(cashAccounts instanceof CashAccounts, true) |
| 32 | + }) |
| 33 | + |
| 34 | + it("should have a restURL property", (): void => { |
| 35 | + const cashAccounts: CashAccounts = new CashAccounts() |
| 36 | + assert.equal(cashAccounts.restURL, resturl) |
| 37 | + }) |
| 38 | + }) |
| 39 | + |
| 40 | + describe("#lookup", () => { |
| 41 | + let sandbox: any |
| 42 | + beforeEach(() => (sandbox = sinon.sandbox.create())) |
| 43 | + afterEach(() => sandbox.restore()) |
| 44 | + |
| 45 | + it(`should lookup CashAccount details by account, number and collision`, async (): Promise< |
| 46 | + any |
| 47 | + > => { |
| 48 | + // Mock out data for unit test, to prevent live network call. |
| 49 | + const data: any = cashAccountsMock.lookup |
| 50 | + const resolved: any = new Promise(r => r({ data: data })) |
| 51 | + sandbox.stub(axios, "get").returns(resolved) |
| 52 | + |
| 53 | + const account: string = "cgcardona" |
| 54 | + const number: number = 122 |
| 55 | + const collision: number = 6383276713 |
| 56 | + |
| 57 | + const result = (await bitbox.CashAccounts.lookup( |
| 58 | + account, |
| 59 | + number, |
| 60 | + collision |
| 61 | + )) as CashAccountLookupResult |
| 62 | + //console.log(`result: ${JSON.stringify(result,null,2)}`) |
| 63 | + |
| 64 | + assert.hasAllKeys(result, ["identifier", "information"]) |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + describe("#check", () => { |
| 69 | + let sandbox: any |
| 70 | + beforeEach(() => (sandbox = sinon.sandbox.create())) |
| 71 | + afterEach(() => sandbox.restore()) |
| 72 | + |
| 73 | + it(`should check CashAccount by account and number`, async (): Promise< |
| 74 | + any |
| 75 | + > => { |
| 76 | + // Mock out data for unit test, to prevent live network call. |
| 77 | + const data: any = cashAccountsMock.check |
| 78 | + const resolved: any = new Promise(r => r({ data: data })) |
| 79 | + sandbox.stub(axios, "get").returns(resolved) |
| 80 | + |
| 81 | + const account: string = "cgcardona" |
| 82 | + const number: number = 122 |
| 83 | + |
| 84 | + const result = (await bitbox.CashAccounts.check( |
| 85 | + account, |
| 86 | + number |
| 87 | + )) as CashAccountCheckResult |
| 88 | + //console.log(`result: ${JSON.stringify(result,null,2)}`) |
| 89 | + |
| 90 | + assert.hasAllKeys(result, ["identifier", "block", "results"]) |
| 91 | + }) |
| 92 | + }) |
| 93 | + |
| 94 | + describe("#reverseLookup", () => { |
| 95 | + let sandbox: any |
| 96 | + beforeEach(() => (sandbox = sinon.sandbox.create())) |
| 97 | + afterEach(() => sandbox.restore()) |
| 98 | + |
| 99 | + it(`should reverse lookup CashAccount details by cash address`, async (): Promise< |
| 100 | + any |
| 101 | + > => { |
| 102 | + // Mock out data for unit test, to prevent live network call. |
| 103 | + const data: any = cashAccountsMock.reverseLookup |
| 104 | + const resolved: any = new Promise(r => r({ data: data })) |
| 105 | + sandbox.stub(axios, "get").returns(resolved) |
| 106 | + |
| 107 | + const cashAddress: string = |
| 108 | + "bitcoincash:qr4aadjrpu73d2wxwkxkcrt6gqxgu6a7usxfm96fst" |
| 109 | + |
| 110 | + const result = (await bitbox.CashAccounts.reverseLookup( |
| 111 | + cashAddress |
| 112 | + )) as CashAccountReverseLookupResult |
| 113 | + //console.log(`result: ${JSON.stringify(result,null,2)}`) |
| 114 | + |
| 115 | + assert.hasAllKeys(result.results[0], [ |
| 116 | + "accountEmoji", |
| 117 | + "nameText", |
| 118 | + "accountNumber", |
| 119 | + "accountHash", |
| 120 | + "accountCollisionLength", |
| 121 | + "payloadType", |
| 122 | + "payloadAddress" |
| 123 | + ]) |
| 124 | + }) |
| 125 | + }) |
| 126 | +}) |
0 commit comments