Changeset View
Changeset View
Standalone View
Standalone View
apps/ecash-herald/test/utilsTests.js
// Copyright (c) 2023 The Bitcoin developers | // Copyright (c) 2023 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
'use strict'; | 'use strict'; | ||||
const assert = require('assert'); | const assert = require('assert'); | ||||
const axios = require('axios'); | const axios = require('axios'); | ||||
const MockAdapter = require('axios-mock-adapter'); | const MockAdapter = require('axios-mock-adapter'); | ||||
const config = require('../config'); | const config = require('../config'); | ||||
const { | const { | ||||
returnAddressPreview, | returnAddressPreview, | ||||
getCoingeckoPrices, | getCoingeckoPrices, | ||||
formatPrice, | formatPrice, | ||||
jsonReplacer, | |||||
jsonReviver, | |||||
} = require('../src/utils'); | } = require('../src/utils'); | ||||
const { addressPreviews } = require('./mocks/templates'); | const { addressPreviews } = require('./mocks/templates'); | ||||
describe('ecash-telegram-bot utils.js functions', function () { | describe('ecash-telegram-bot utils.js functions', function () { | ||||
it('returnAddressPreview converts a valid ecash: address into an abbreviated preview at various slice sizes', function () { | it('returnAddressPreview converts a valid ecash: address into an abbreviated preview at various slice sizes', function () { | ||||
for (let i = 0; i < addressPreviews.length; i += 1) { | for (let i = 0; i < addressPreviews.length; i += 1) { | ||||
const { address, preview, sliceSize } = addressPreviews[i]; | const { address, preview, sliceSize } = addressPreviews[i]; | ||||
assert.strictEqual( | assert.strictEqual( | ||||
▲ Show 20 Lines • Show All 138 Lines • ▼ Show 20 Lines | it('formatPrice correctly formats a GBP price greater than 100', async function () { | ||||
assert.strictEqual(formatPrice(1523.134239, 'gbp'), `£1,523`); | assert.strictEqual(formatPrice(1523.134239, 'gbp'), `£1,523`); | ||||
}); | }); | ||||
it('formatPrice correctly formats a JPY price greater than ¥100', async function () { | it('formatPrice correctly formats a JPY price greater than ¥100', async function () { | ||||
assert.strictEqual(formatPrice(100000.999923422, 'jpy'), `¥100,001`); | assert.strictEqual(formatPrice(100000.999923422, 'jpy'), `¥100,001`); | ||||
}); | }); | ||||
it('formatPrice omits a currency symbol if it cannot find it', async function () { | it('formatPrice omits a currency symbol if it cannot find it', async function () { | ||||
assert.strictEqual(formatPrice(100000.999923422, 'cad'), `100,001`); | assert.strictEqual(formatPrice(100000.999923422, 'cad'), `100,001`); | ||||
}); | }); | ||||
it('jsonReplacer and jsonReviver can encode and decode a Map to and from JSON', async function () { | |||||
const map = new Map([ | |||||
[1, 'one'], | |||||
[2, 'two'], | |||||
[3, 'three'], | |||||
]); | |||||
const jsonText = JSON.stringify(map, jsonReplacer); | |||||
const roundTrip = JSON.parse(jsonText, jsonReviver); | |||||
assert.deepEqual(map, roundTrip); | |||||
}); | |||||
it('jsonReplacer and jsonReviver can encode and decode a Set to and from JSON', async function () { | |||||
const set = new Set(['one', 'two', 'three']); | |||||
const jsonText = JSON.stringify(set, jsonReplacer); | |||||
const roundTrip = JSON.parse(jsonText, jsonReviver); | |||||
assert.deepEqual(set, roundTrip); | |||||
}); | |||||
it('jsonReplacer and jsonReviver can encode and decode an object including a Set and a Map to and from JSON', async function () { | |||||
const map = new Map([ | |||||
[1, 'one'], | |||||
[2, 'two'], | |||||
[3, 'three'], | |||||
]); | |||||
const set = new Set(['one', 'two', 'three']); | |||||
const jsonText = JSON.stringify({ map, set }, jsonReplacer); | |||||
const roundTrip = JSON.parse(jsonText, jsonReviver); | |||||
assert.deepEqual({ map, set }, roundTrip); | |||||
}); | |||||
}); | }); |