383 lines
11 KiB
JavaScript
383 lines
11 KiB
JavaScript
import { module, test } from 'qunit';
|
|
import { setupTest } from 'dashtab/tests/helpers';
|
|
import { BITCOIN_PROVIDERS } from 'dashtab/services/bitcoin';
|
|
|
|
const COINGECKO = BITCOIN_PROVIDERS.find(({ id }) => id === 'coingecko');
|
|
const BINANCE = BITCOIN_PROVIDERS.find(({ id }) => id === 'binance');
|
|
const BITSTAMP = BITCOIN_PROVIDERS.find(({ id }) => id === 'bitstamp');
|
|
|
|
module('Unit | Service | bitcoin', function (hooks) {
|
|
setupTest(hooks);
|
|
|
|
let originalFetch;
|
|
let originalConsoleError;
|
|
|
|
hooks.beforeEach(async function () {
|
|
originalFetch = globalThis.fetch;
|
|
originalConsoleError = console.error;
|
|
console.error = () => {};
|
|
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.remove('integrations', 'bitcoin');
|
|
});
|
|
|
|
hooks.afterEach(function () {
|
|
globalThis.fetch = originalFetch;
|
|
console.error = originalConsoleError;
|
|
});
|
|
|
|
test('it exists', function (assert) {
|
|
let service = this.owner.lookup('service:bitcoin');
|
|
assert.ok(service);
|
|
});
|
|
|
|
test('load enables a stored active integration', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'bitcoin', {
|
|
type: 'bitcoin',
|
|
status: 'active',
|
|
interval: 60000,
|
|
});
|
|
|
|
const service = this.owner.lookup('service:bitcoin');
|
|
service.fetchPrice = async () => {};
|
|
await service.ready;
|
|
|
|
assert.true(service.enabled);
|
|
assert.strictEqual(service.interval, 60000);
|
|
assert.strictEqual(service.provider, 'bitstamp');
|
|
assert.ok(service.intervalId);
|
|
|
|
service.stopPolling();
|
|
});
|
|
|
|
test('load restores the stored provider', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'bitcoin', {
|
|
type: 'bitcoin',
|
|
status: 'active',
|
|
interval: 60000,
|
|
provider: 'binance',
|
|
});
|
|
|
|
const service = this.owner.lookup('service:bitcoin');
|
|
service.fetchPrice = async () => {};
|
|
await service.ready;
|
|
|
|
assert.strictEqual(service.provider, 'binance');
|
|
|
|
service.stopPolling();
|
|
});
|
|
|
|
test('load stays disabled for a pending integration', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'bitcoin', {
|
|
type: 'bitcoin',
|
|
status: 'pending',
|
|
interval: 60000,
|
|
});
|
|
|
|
const service = this.owner.lookup('service:bitcoin');
|
|
await service.ready;
|
|
|
|
assert.false(service.enabled);
|
|
assert.strictEqual(service.intervalId, null);
|
|
});
|
|
|
|
test('enable persists an active integration with provider and starts polling', async function (assert) {
|
|
const service = this.owner.lookup('service:bitcoin');
|
|
service.fetchPrice = async () => {};
|
|
|
|
await service.enable(60000, 'binance');
|
|
|
|
const storage = this.owner.lookup('service:storage');
|
|
const integration = await storage.get('integrations', 'bitcoin');
|
|
|
|
assert.true(service.enabled);
|
|
assert.strictEqual(service.interval, 60000);
|
|
assert.strictEqual(service.provider, 'binance');
|
|
assert.strictEqual(integration.status, 'active');
|
|
assert.strictEqual(integration.type, 'bitcoin');
|
|
assert.strictEqual(integration.interval, 60000);
|
|
assert.strictEqual(integration.provider, 'binance');
|
|
assert.ok(service.intervalId);
|
|
|
|
service.stopPolling();
|
|
});
|
|
|
|
test('fetchPrice parses the CoinGecko response', async function (assert) {
|
|
let requestedUrl;
|
|
globalThis.fetch = async (url) => {
|
|
requestedUrl = url;
|
|
return {
|
|
ok: true,
|
|
json: async () => ({
|
|
bitcoin: { usd: 86246.18, usd_24h_change: 3.43 },
|
|
}),
|
|
};
|
|
};
|
|
|
|
const service = this.owner.lookup('service:bitcoin');
|
|
await service.ready;
|
|
await service.fetchPrice();
|
|
|
|
assert.strictEqual(requestedUrl, COINGECKO.url);
|
|
assert.strictEqual(service.price, 86246.18);
|
|
assert.strictEqual(service.changePercent, 3.43);
|
|
assert.strictEqual(service.error, null);
|
|
assert.false(service.loading);
|
|
});
|
|
|
|
test('fetchPrice parses the Binance response', async function (assert) {
|
|
let requestedUrl;
|
|
globalThis.fetch = async (url) => {
|
|
requestedUrl = url;
|
|
return {
|
|
ok: true,
|
|
json: async () => ({
|
|
lastPrice: '86246.18',
|
|
priceChangePercent: '3.43',
|
|
}),
|
|
};
|
|
};
|
|
|
|
const service = this.owner.lookup('service:bitcoin');
|
|
await service.ready;
|
|
service.provider = 'binance';
|
|
|
|
await service.fetchPrice();
|
|
|
|
assert.strictEqual(requestedUrl, BINANCE.url);
|
|
assert.strictEqual(service.price, 86246.18);
|
|
assert.strictEqual(service.changePercent, 3.43);
|
|
});
|
|
|
|
test('fetchPrice parses the Bitstamp response', async function (assert) {
|
|
let requestedUrl;
|
|
globalThis.fetch = async (url) => {
|
|
requestedUrl = url;
|
|
return {
|
|
ok: true,
|
|
json: async () => ({ last: '86246.18', percent_change_24: '3.43' }),
|
|
};
|
|
};
|
|
|
|
const service = this.owner.lookup('service:bitcoin');
|
|
await service.ready;
|
|
service.provider = 'bitstamp';
|
|
|
|
await service.fetchPrice();
|
|
|
|
assert.strictEqual(requestedUrl, BITSTAMP.url);
|
|
assert.strictEqual(service.price, 86246.18);
|
|
assert.strictEqual(service.changePercent, 3.43);
|
|
});
|
|
|
|
test('fetchPrice sets an error and keeps the last price on a failed response', async function (assert) {
|
|
globalThis.fetch = async () => ({
|
|
ok: false,
|
|
status: 500,
|
|
json: async () => ({}),
|
|
});
|
|
|
|
const service = this.owner.lookup('service:bitcoin');
|
|
await service.ready;
|
|
service.price = 100;
|
|
|
|
await service.fetchPrice();
|
|
|
|
assert.strictEqual(service.error, 'Unable to load the bitcoin price');
|
|
assert.strictEqual(service.price, 100);
|
|
assert.false(service.loading);
|
|
});
|
|
|
|
test('fetchPrice sets an error when the request throws', async function (assert) {
|
|
globalThis.fetch = async () => {
|
|
throw new Error('network down');
|
|
};
|
|
|
|
const service = this.owner.lookup('service:bitcoin');
|
|
await service.ready;
|
|
|
|
await service.fetchPrice();
|
|
|
|
assert.strictEqual(service.error, 'Unable to load the bitcoin price');
|
|
assert.false(service.loading);
|
|
});
|
|
|
|
test('fetchPrice keeps the last price when the request is cancelled', async function (assert) {
|
|
globalThis.fetch = async () => {
|
|
const err = new Error('The operation was aborted');
|
|
err.name = 'AbortError';
|
|
throw err;
|
|
};
|
|
|
|
const service = this.owner.lookup('service:bitcoin');
|
|
await service.ready;
|
|
service.price = 100;
|
|
|
|
await service.fetchPrice();
|
|
|
|
assert.strictEqual(service.error, null);
|
|
assert.strictEqual(service.price, 100);
|
|
assert.false(service.loading);
|
|
});
|
|
|
|
test('fetchPrice ignores failures while the page is leaving', async function (assert) {
|
|
globalThis.fetch = async () => {
|
|
throw new TypeError('Failed to fetch');
|
|
};
|
|
|
|
const service = this.owner.lookup('service:bitcoin');
|
|
await service.ready;
|
|
service.pageLifecycle.leaving = true;
|
|
|
|
await service.fetchPrice();
|
|
|
|
assert.strictEqual(service.error, null);
|
|
|
|
service.pageLifecycle.leaving = false;
|
|
});
|
|
|
|
test('handleRestore refetches when enabled', async function (assert) {
|
|
const service = this.owner.lookup('service:bitcoin');
|
|
await service.ready;
|
|
|
|
let calls = 0;
|
|
service.fetchPrice = async () => {
|
|
calls++;
|
|
};
|
|
service.enabled = true;
|
|
|
|
service.handleRestore();
|
|
|
|
assert.strictEqual(calls, 1);
|
|
});
|
|
|
|
test('handleRestore does nothing when disabled', async function (assert) {
|
|
const service = this.owner.lookup('service:bitcoin');
|
|
await service.ready;
|
|
|
|
let calls = 0;
|
|
service.fetchPrice = async () => {
|
|
calls++;
|
|
};
|
|
|
|
service.handleRestore();
|
|
|
|
assert.strictEqual(calls, 0);
|
|
});
|
|
|
|
test('fetchPrice sets an error for an unknown provider', async function (assert) {
|
|
let calls = 0;
|
|
globalThis.fetch = async () => {
|
|
calls++;
|
|
return { ok: true, json: async () => ({}) };
|
|
};
|
|
|
|
const service = this.owner.lookup('service:bitcoin');
|
|
await service.ready;
|
|
service.provider = 'bogus';
|
|
|
|
await service.fetchPrice();
|
|
|
|
assert.strictEqual(service.error, 'Unable to load the bitcoin price');
|
|
assert.strictEqual(calls, 0);
|
|
assert.false(service.fetching);
|
|
});
|
|
|
|
test('fetchPrice ignores overlapping requests', async function (assert) {
|
|
let calls = 0;
|
|
let resolveFetch;
|
|
globalThis.fetch = () => {
|
|
calls++;
|
|
return new Promise((resolve) => {
|
|
resolveFetch = resolve;
|
|
});
|
|
};
|
|
|
|
const service = this.owner.lookup('service:bitcoin');
|
|
await service.ready;
|
|
|
|
const first = service.fetchPrice();
|
|
const second = service.fetchPrice();
|
|
|
|
resolveFetch({
|
|
ok: true,
|
|
json: async () => ({ bitcoin: { usd: 1, usd_24h_change: 0 } }),
|
|
});
|
|
await Promise.all([first, second]);
|
|
|
|
assert.strictEqual(calls, 1);
|
|
});
|
|
|
|
test('stopPolling clears the interval', async function (assert) {
|
|
const service = this.owner.lookup('service:bitcoin');
|
|
await service.ready;
|
|
service.fetchPrice = async () => {};
|
|
|
|
service.startPolling();
|
|
assert.ok(service.intervalId);
|
|
|
|
service.stopPolling();
|
|
assert.strictEqual(service.intervalId, null);
|
|
});
|
|
|
|
test('update changes the stored settings and restarts polling', async function (assert) {
|
|
const service = this.owner.lookup('service:bitcoin');
|
|
service.fetchPrice = async () => {};
|
|
await service.enable(30000, 'coingecko');
|
|
|
|
const storage = this.owner.lookup('service:storage');
|
|
const before = await storage.get('integrations', 'bitcoin');
|
|
|
|
await service.update(60000, 'binance');
|
|
|
|
const after = await storage.get('integrations', 'bitcoin');
|
|
|
|
assert.strictEqual(after.status, 'active');
|
|
assert.strictEqual(after.interval, 60000);
|
|
assert.strictEqual(after.provider, 'binance');
|
|
assert.strictEqual(after.createdAt, before.createdAt);
|
|
assert.strictEqual(service.interval, 60000);
|
|
assert.strictEqual(service.provider, 'binance');
|
|
assert.ok(service.intervalId);
|
|
|
|
service.stopPolling();
|
|
});
|
|
|
|
test('update defaults to the current settings', async function (assert) {
|
|
const service = this.owner.lookup('service:bitcoin');
|
|
service.fetchPrice = async () => {};
|
|
await service.enable(60000, 'binance');
|
|
|
|
await service.update();
|
|
|
|
assert.strictEqual(service.interval, 60000);
|
|
assert.strictEqual(service.provider, 'binance');
|
|
|
|
service.stopPolling();
|
|
});
|
|
|
|
test('disable clears storage and resets state', async function (assert) {
|
|
const service = this.owner.lookup('service:bitcoin');
|
|
service.fetchPrice = async () => {};
|
|
await service.enable(60000, 'binance');
|
|
service.price = 123;
|
|
service.error = 'nope';
|
|
|
|
await service.disable();
|
|
|
|
const storage = this.owner.lookup('service:storage');
|
|
const integration = await storage.get('integrations', 'bitcoin');
|
|
|
|
assert.strictEqual(integration, null);
|
|
assert.false(service.enabled);
|
|
assert.strictEqual(service.intervalId, null);
|
|
assert.strictEqual(service.interval, 30000);
|
|
assert.strictEqual(service.provider, 'coingecko');
|
|
assert.strictEqual(service.price, null);
|
|
assert.strictEqual(service.error, null);
|
|
});
|
|
});
|