Add bitcoin ticker widget
This commit is contained in:
1 parent
197542fcfa
commit
753bcccbf6
9 files changed
+668
No files matched your search
@@ -0,0 +1,58 @@
|
||||
import Component from '@glimmer/component';
|
||||
import { service } from '@ember/service';
|
||||
|
||||
export default class BitcoinTickerComponent extends Component {
|
||||
@service bitcoin;
|
||||
|
||||
get formattedPrice() {
|
||||
if (this.bitcoin.price == null) {
|
||||
return '—';
|
||||
}
|
||||
return new Intl.NumberFormat('en-US', {
|
||||
style: 'currency',
|
||||
currency: 'USD',
|
||||
}).format(this.bitcoin.price);
|
||||
}
|
||||
|
||||
get hasChange() {
|
||||
return this.bitcoin.changePercent != null;
|
||||
}
|
||||
|
||||
get isUp() {
|
||||
return this.bitcoin.changePercent >= 0;
|
||||
}
|
||||
|
||||
get changeClass() {
|
||||
return this.isUp ? 'text-green-400' : 'text-red-400';
|
||||
}
|
||||
|
||||
<template>
|
||||
{{#if this.bitcoin.enabled}}
|
||||
<div class="text-center text-white text-shadow-lg">
|
||||
<div class="flex items-center justify-center gap-1.5">
|
||||
<span class="text-lg font-semibold">{{this.formattedPrice}}</span>
|
||||
{{#if this.hasChange}}
|
||||
<svg
|
||||
viewBox="0 0 8 8"
|
||||
fill="currentColor"
|
||||
aria-hidden="true"
|
||||
class="size-2 shrink-0 {{this.changeClass}}"
|
||||
data-direction={{if this.isUp "up" "down"}}
|
||||
>
|
||||
{{#if this.isUp}}
|
||||
<path d="M4 0 8 8H0z" />
|
||||
{{else}}
|
||||
<path d="M4 8 0 0h8z" />
|
||||
{{/if}}
|
||||
</svg>
|
||||
<span class="sr-only">{{if this.isUp "up" "down"}}</span>
|
||||
{{/if}}
|
||||
</div>
|
||||
<div class="text-xs uppercase tracking-wide opacity-80">Bitcoin</div>
|
||||
{{#if this.bitcoin.error}}
|
||||
<div class="text-xs text-red-300">{{this.bitcoin.error}}</div>
|
||||
{{/if}}
|
||||
</div>
|
||||
{{/if}}
|
||||
</template>
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
import Component from '@glimmer/component';
|
||||
import { tracked } from '@glimmer/tracking';
|
||||
import { action } from '@ember/object';
|
||||
import { service } from '@ember/service';
|
||||
import { on } from '@ember/modifier';
|
||||
import {
|
||||
BITCOIN_DEFAULT_INTERVAL,
|
||||
BITCOIN_INTERVAL_OPTIONS,
|
||||
} from '../../../services/bitcoin';
|
||||
|
||||
export default class SettingsAddIntegrationBitcoin extends Component {
|
||||
@service bitcoin;
|
||||
|
||||
@tracked interval = BITCOIN_DEFAULT_INTERVAL;
|
||||
|
||||
get options() {
|
||||
return BITCOIN_INTERVAL_OPTIONS;
|
||||
}
|
||||
|
||||
@action
|
||||
updateInterval(event) {
|
||||
this.interval = Number(event.target.value);
|
||||
}
|
||||
|
||||
@action
|
||||
async handleSubmit(event) {
|
||||
event.preventDefault();
|
||||
await this.bitcoin.enable(this.interval);
|
||||
this.args.closeDialog();
|
||||
}
|
||||
|
||||
<template>
|
||||
<div class="p-6">
|
||||
<h3
|
||||
id="dialog-title"
|
||||
class="text-base font-semibold leading-6 text-gray-900"
|
||||
>Add Bitcoin Price Ticker</h3>
|
||||
<div class="mt-2">
|
||||
<p class="text-sm text-gray-500">
|
||||
Show the current Bitcoin price at the top of your dashboard.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<form {{on "submit" this.handleSubmit}} class="mt-5">
|
||||
<div>
|
||||
<label
|
||||
for="bitcoin-interval"
|
||||
class="block text-sm/6 font-medium text-gray-900"
|
||||
>Update interval</label>
|
||||
<el-select
|
||||
id="bitcoin-interval"
|
||||
name="interval"
|
||||
value="30000"
|
||||
class="mt-2 block"
|
||||
{{on "change" this.updateInterval}}
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
class="grid w-full cursor-default grid-cols-1 rounded-md bg-white py-1.5 pr-2 pl-3 text-left text-gray-900 outline-1 -outline-offset-1 outline-gray-300 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-indigo-600 sm:text-sm/6"
|
||||
>
|
||||
<el-selectedcontent
|
||||
class="col-start-1 row-start-1 truncate pr-6"
|
||||
>Every 30 seconds</el-selectedcontent>
|
||||
<svg
|
||||
viewBox="0 0 16 16"
|
||||
fill="currentColor"
|
||||
data-slot="icon"
|
||||
aria-hidden="true"
|
||||
class="col-start-1 row-start-1 size-5 self-center justify-self-end text-gray-500 sm:size-4"
|
||||
>
|
||||
<path
|
||||
d="M5.22 10.22a.75.75 0 0 1 1.06 0L8 11.94l1.72-1.72a.75.75 0 1 1 1.06 1.06l-2.25 2.25a.75.75 0 0 1-1.06 0l-2.25-2.25a.75.75 0 0 1 0-1.06ZM10.78 5.78a.75.75 0 0 1-1.06 0L8 4.06 6.28 5.78a.75.75 0 0 1-1.06-1.06l2.25-2.25a.75.75 0 0 1 1.06 0l2.25 2.25a.75.75 0 0 1 0 1.06Z"
|
||||
clip-rule="evenodd"
|
||||
fill-rule="evenodd"
|
||||
/>
|
||||
</svg>
|
||||
</button>
|
||||
|
||||
<el-options
|
||||
anchor="bottom start"
|
||||
popover
|
||||
class="max-h-60 w-(--button-width) overflow-auto rounded-md bg-white py-1 text-base shadow-lg outline-1 outline-black/5 [--anchor-gap:--spacing(1)] data-leave:transition data-leave:transition-discrete data-leave:duration-100 data-leave:ease-in data-closed:data-leave:opacity-0 sm:text-sm"
|
||||
>
|
||||
{{#each this.options as |option|}}
|
||||
<el-option
|
||||
value={{option.value}}
|
||||
class="group/option relative block cursor-default py-2 pr-9 pl-3 text-gray-900 select-none focus:bg-indigo-600 focus:text-white focus:outline-hidden"
|
||||
>
|
||||
<span
|
||||
class="block truncate font-normal group-aria-selected/option:font-semibold"
|
||||
>{{option.label}}</span>
|
||||
<span
|
||||
class="absolute inset-y-0 right-0 flex items-center pr-4 text-indigo-600 group-not-aria-selected/option:hidden group-focus/option:text-white in-[el-selectedcontent]:hidden"
|
||||
>
|
||||
<svg
|
||||
viewBox="0 0 20 20"
|
||||
fill="currentColor"
|
||||
data-slot="icon"
|
||||
aria-hidden="true"
|
||||
class="size-5"
|
||||
>
|
||||
<path
|
||||
d="M16.704 4.153a.75.75 0 0 1 .143 1.052l-8 10.5a.75.75 0 0 1-1.127.075l-4.5-4.5a.75.75 0 0 1 1.06-1.06l3.894 3.893 7.48-9.817a.75.75 0 0 1 1.05-.143Z"
|
||||
clip-rule="evenodd"
|
||||
fill-rule="evenodd"
|
||||
/>
|
||||
</svg>
|
||||
</span>
|
||||
</el-option>
|
||||
{{/each}}
|
||||
</el-options>
|
||||
</el-select>
|
||||
</div>
|
||||
|
||||
<div class="mt-5 sm:mt-6 flex gap-3">
|
||||
<button
|
||||
type="submit"
|
||||
class="inline-flex w-full justify-center rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
|
||||
>
|
||||
Enable
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
{{on "click" @closeDialog}}
|
||||
class="inline-flex w-full justify-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</template>
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { on } from '@ember/modifier';
|
||||
import { fn } from '@ember/helper';
|
||||
import Discourse from './discourse.gjs';
|
||||
import Bitcoin from './bitcoin.gjs';
|
||||
|
||||
<template>
|
||||
<div class="block max-h-96 p-4 transform-gpu scroll-py-3 overflow-y-auto">
|
||||
@@ -39,6 +40,37 @@ import Discourse from './discourse.gjs';
|
||||
</p>
|
||||
</div>
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
{{on "click" (fn @switchComponent Bitcoin)}}
|
||||
class="group flex w-full rounded-xl p-3 cursor-pointer select-none focus:outline-hidden hover:bg-gray-100 aria-selected:bg-gray-100"
|
||||
>
|
||||
<div
|
||||
class="flex size-10 flex-none items-center justify-center rounded-lg bg-orange-500"
|
||||
>
|
||||
<svg
|
||||
viewBox="0 0 24 24"
|
||||
fill="currentColor"
|
||||
data-slot="icon"
|
||||
class="size-6 text-white"
|
||||
>
|
||||
<path
|
||||
d="M23.638 14.904c-1.602 6.43-8.113 10.34-14.542 8.736C2.67 22.05-1.244 15.525.362 9.105 1.962 2.67 8.475-1.243 14.9.358c6.43 1.605 10.342 8.115 8.738 14.546zm-6.35-4.613c.24-1.59-.974-2.45-2.64-3.03l.54-2.153-1.315-.328-.525 2.107c-.345-.087-.705-.167-1.064-.25l.526-2.127-1.32-.33-.54 2.165c-.285-.067-.565-.132-.84-.2l-.006-.02-1.815-.45-.35 1.407s.975.225.955.238c.535.136.63.486.615.766l-.862 3.454c-.04.1-.14.25-.365.195.01.014-.95-.237-.95-.237l-.656 1.51 1.72.43c.32.08.634.16.955.25l-.54 2.2 1.32.33.54-2.18c.36.1.71.19 1.05.28l-.53 2.16 1.31.33.54-2.2c2.28.43 3.99.26 4.71-1.8.58-1.65-.03-2.6-1.22-3.22.87-.2 1.53-.78 1.7-1.96z"
|
||||
/>
|
||||
</svg>
|
||||
</div>
|
||||
<div class="ml-4 flex-auto text-left">
|
||||
<p
|
||||
id="item-bitcoin-label"
|
||||
class="text-sm font-medium text-gray-700 group-aria-selected:text-gray-900"
|
||||
>
|
||||
Bitcoin Price Ticker
|
||||
</p>
|
||||
<p class="text-sm text-gray-500 group-aria-selected:text-gray-700">
|
||||
Live BTC price on your dashboard
|
||||
</p>
|
||||
</div>
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
disabled
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
import Service, { service } from '@ember/service';
|
||||
import { tracked } from '@glimmer/tracking';
|
||||
|
||||
export const BITCOIN_TICKER_URL =
|
||||
'https://cors.5apps.com/?uri=https://www.bitstamp.net/api/v2/ticker/btcusd/';
|
||||
|
||||
export const BITCOIN_DEFAULT_INTERVAL = 30000;
|
||||
|
||||
export const BITCOIN_INTERVAL_OPTIONS = [
|
||||
{ value: 30000, label: 'Every 30 seconds' },
|
||||
{ value: 60000, label: 'Every 60 seconds' },
|
||||
{ value: 300000, label: 'Every 5 minutes' },
|
||||
];
|
||||
|
||||
export default class BitcoinService extends Service {
|
||||
@service storage;
|
||||
|
||||
@tracked enabled = false;
|
||||
@tracked interval = BITCOIN_DEFAULT_INTERVAL;
|
||||
@tracked price = null;
|
||||
@tracked changePercent = null;
|
||||
@tracked loading = false;
|
||||
@tracked error = null;
|
||||
|
||||
intervalId = null;
|
||||
fetching = false;
|
||||
ready = null;
|
||||
|
||||
constructor() {
|
||||
super(...arguments);
|
||||
this.ready = this.load();
|
||||
}
|
||||
|
||||
async load() {
|
||||
try {
|
||||
const integration = await this.storage.get('integrations', 'bitcoin');
|
||||
if (integration?.status === 'active') {
|
||||
this.enabled = true;
|
||||
this.interval = integration.interval ?? BITCOIN_DEFAULT_INTERVAL;
|
||||
this.startPolling();
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error loading bitcoin integration:', err);
|
||||
}
|
||||
}
|
||||
|
||||
async enable(interval = BITCOIN_DEFAULT_INTERVAL) {
|
||||
await this.ready;
|
||||
|
||||
this.interval = interval;
|
||||
this.enabled = true;
|
||||
|
||||
await this.storage.set('integrations', 'bitcoin', {
|
||||
type: 'bitcoin',
|
||||
status: 'active',
|
||||
interval,
|
||||
currency: 'usd',
|
||||
createdAt: Date.now(),
|
||||
});
|
||||
|
||||
this.startPolling();
|
||||
}
|
||||
|
||||
async fetchPrice() {
|
||||
if (this.fetching) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.fetching = true;
|
||||
this.loading = true;
|
||||
|
||||
try {
|
||||
const response = await fetch(BITCOIN_TICKER_URL);
|
||||
if (!response.ok) {
|
||||
throw new Error(`Request failed with status ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
this.price = Number.parseFloat(data.last);
|
||||
this.changePercent = Number.parseFloat(data.percent_change_24);
|
||||
this.error = null;
|
||||
} catch (err) {
|
||||
console.error('Error fetching bitcoin price:', err);
|
||||
this.error = 'Unable to load the bitcoin price';
|
||||
} finally {
|
||||
this.fetching = false;
|
||||
this.loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
startPolling() {
|
||||
this.stopPolling();
|
||||
this.fetchPrice();
|
||||
this.intervalId = setInterval(() => {
|
||||
this.fetchPrice();
|
||||
}, this.interval);
|
||||
}
|
||||
|
||||
stopPolling() {
|
||||
if (this.intervalId) {
|
||||
clearInterval(this.intervalId);
|
||||
this.intervalId = null;
|
||||
}
|
||||
}
|
||||
|
||||
willDestroy() {
|
||||
this.stopPolling();
|
||||
super.willDestroy();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { pageTitle } from 'ember-page-title';
|
||||
import { on } from '@ember/modifier';
|
||||
import DefaultClock from '../components/default-clock.gjs';
|
||||
import BitcoinTicker from '../components/bitcoin-ticker.gjs';
|
||||
import ModalDialog from '../components/modal-dialog.gjs';
|
||||
import ModalStepOne from '../components/modal-step-one.gjs';
|
||||
import { default as SettingsAddIntegrationList } from '../components/settings/add-integration/list.gjs';
|
||||
@@ -24,6 +25,10 @@ import { default as SettingsAddIntegrationList } from '../components/settings/ad
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="col-start-2 row-start-1 flex items-center justify-center">
|
||||
<BitcoinTicker />
|
||||
</div>
|
||||
|
||||
<div class="col-start-2 row-start-2 flex items-center justify-center">
|
||||
<DefaultClock />
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupRenderingTest } from 'dashtab/tests/helpers';
|
||||
import { render } from '@ember/test-helpers';
|
||||
import { tracked } from '@glimmer/tracking';
|
||||
import BitcoinTicker from 'dashtab/components/bitcoin-ticker';
|
||||
|
||||
class FakeBitcoinService {
|
||||
@tracked enabled = false;
|
||||
@tracked price = null;
|
||||
@tracked changePercent = null;
|
||||
@tracked error = null;
|
||||
}
|
||||
|
||||
module('Integration | Component | bitcoin-ticker', function (hooks) {
|
||||
setupRenderingTest(hooks);
|
||||
|
||||
test('it renders nothing when disabled', async function (assert) {
|
||||
const bitcoin = new FakeBitcoinService();
|
||||
this.owner.register('service:bitcoin', bitcoin, { instantiate: false });
|
||||
|
||||
await render(<template><BitcoinTicker /></template>);
|
||||
|
||||
assert.dom().doesNotContainText('Bitcoin');
|
||||
});
|
||||
|
||||
test('it renders the price and an up triangle when enabled', async function (assert) {
|
||||
const bitcoin = new FakeBitcoinService();
|
||||
this.owner.register('service:bitcoin', bitcoin, { instantiate: false });
|
||||
|
||||
bitcoin.enabled = true;
|
||||
bitcoin.price = 86246.18;
|
||||
bitcoin.changePercent = 3.43;
|
||||
|
||||
await render(<template><BitcoinTicker /></template>);
|
||||
|
||||
assert.dom().containsText('$86,246.18');
|
||||
assert.dom('[data-direction="up"]').exists();
|
||||
assert.dom('.text-green-400').exists();
|
||||
assert.dom().containsText('Bitcoin');
|
||||
assert.dom().doesNotContainText('%');
|
||||
});
|
||||
|
||||
test('it shows an error message when the fetch fails', async function (assert) {
|
||||
const bitcoin = new FakeBitcoinService();
|
||||
this.owner.register('service:bitcoin', bitcoin, { instantiate: false });
|
||||
|
||||
bitcoin.enabled = true;
|
||||
bitcoin.error = 'Unable to load the bitcoin price';
|
||||
|
||||
await render(<template><BitcoinTicker /></template>);
|
||||
|
||||
assert.dom().containsText('Unable to load the bitcoin price');
|
||||
});
|
||||
|
||||
test('it renders a down triangle for a negative change', async function (assert) {
|
||||
const bitcoin = new FakeBitcoinService();
|
||||
this.owner.register('service:bitcoin', bitcoin, { instantiate: false });
|
||||
|
||||
bitcoin.enabled = true;
|
||||
bitcoin.price = 86246.18;
|
||||
bitcoin.changePercent = -3.43;
|
||||
|
||||
await render(<template><BitcoinTicker /></template>);
|
||||
|
||||
assert.dom('[data-direction="down"]').exists();
|
||||
assert.dom('.text-red-400').exists();
|
||||
assert.dom('.text-green-400').doesNotExist();
|
||||
});
|
||||
|
||||
test('it renders a placeholder before the first price arrives', async function (assert) {
|
||||
const bitcoin = new FakeBitcoinService();
|
||||
this.owner.register('service:bitcoin', bitcoin, { instantiate: false });
|
||||
|
||||
bitcoin.enabled = true;
|
||||
|
||||
await render(<template><BitcoinTicker /></template>);
|
||||
|
||||
assert.dom().containsText('—');
|
||||
});
|
||||
|
||||
test('it treats a zero change as positive', async function (assert) {
|
||||
const bitcoin = new FakeBitcoinService();
|
||||
this.owner.register('service:bitcoin', bitcoin, { instantiate: false });
|
||||
|
||||
bitcoin.enabled = true;
|
||||
bitcoin.price = 100;
|
||||
bitcoin.changePercent = 0;
|
||||
|
||||
await render(<template><BitcoinTicker /></template>);
|
||||
|
||||
assert.dom('[data-direction="up"]').exists();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,63 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupRenderingTest } from 'dashtab/tests/helpers';
|
||||
import { render, triggerEvent } from '@ember/test-helpers';
|
||||
import Bitcoin from 'dashtab/components/settings/add-integration/bitcoin';
|
||||
|
||||
module(
|
||||
'Integration | Component | settings/add-integration/bitcoin',
|
||||
function (hooks) {
|
||||
setupRenderingTest(hooks);
|
||||
|
||||
test('it renders', async function (assert) {
|
||||
const noop = () => {};
|
||||
|
||||
await render(<template><Bitcoin @closeDialog={{noop}} /></template>);
|
||||
|
||||
assert.dom().containsText('Add Bitcoin Price Ticker');
|
||||
assert.dom().containsText('Update interval');
|
||||
assert.dom('form').exists();
|
||||
assert.dom('el-select').exists();
|
||||
assert.dom('el-option').exists({ count: 3 });
|
||||
assert.dom().containsText('Every 30 seconds');
|
||||
assert.dom().containsText('Every 60 seconds');
|
||||
assert.dom().containsText('Every 5 minutes');
|
||||
});
|
||||
|
||||
test('submitting enables the integration with the default interval and closes the dialog', async function (assert) {
|
||||
const calls = [];
|
||||
const bitcoin = { enable: async (interval) => calls.push(interval) };
|
||||
this.owner.register('service:bitcoin', bitcoin, { instantiate: false });
|
||||
|
||||
let closed = false;
|
||||
const closeDialog = () => {
|
||||
closed = true;
|
||||
};
|
||||
|
||||
await render(
|
||||
<template><Bitcoin @closeDialog={{closeDialog}} /></template>
|
||||
);
|
||||
await triggerEvent('form', 'submit');
|
||||
|
||||
assert.deepEqual(calls, [30000]);
|
||||
assert.true(closed);
|
||||
});
|
||||
|
||||
test('submitting uses the selected interval', async function (assert) {
|
||||
const calls = [];
|
||||
const bitcoin = { enable: async (interval) => calls.push(interval) };
|
||||
this.owner.register('service:bitcoin', bitcoin, { instantiate: false });
|
||||
|
||||
const noop = () => {};
|
||||
await render(<template><Bitcoin @closeDialog={{noop}} /></template>);
|
||||
|
||||
const select = document.querySelector('el-select');
|
||||
const event = new Event('change', { bubbles: true });
|
||||
Object.defineProperty(event, 'target', { value: { value: '60000' } });
|
||||
select.dispatchEvent(event);
|
||||
|
||||
await triggerEvent('form', 'submit');
|
||||
|
||||
assert.deepEqual(calls, [60000]);
|
||||
});
|
||||
}
|
||||
);
|
||||
@@ -20,6 +20,8 @@ module(
|
||||
|
||||
assert.dom().containsText('Discourse');
|
||||
assert.dom().containsText('Notifications from your discussion forums');
|
||||
assert.dom().containsText('Bitcoin Price Ticker');
|
||||
assert.dom().containsText('Live BTC price on your dashboard');
|
||||
assert.dom().containsText('Gitea');
|
||||
assert.dom().containsText('Notifications from your Git hosting servers');
|
||||
});
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupTest } from 'dashtab/tests/helpers';
|
||||
import { BITCOIN_TICKER_URL } from 'dashtab/services/bitcoin';
|
||||
|
||||
module('Unit | Service | bitcoin', function (hooks) {
|
||||
setupTest(hooks);
|
||||
|
||||
let originalFetch;
|
||||
let originalConsoleError;
|
||||
|
||||
hooks.beforeEach(function () {
|
||||
originalFetch = globalThis.fetch;
|
||||
originalConsoleError = console.error;
|
||||
console.error = () => {};
|
||||
});
|
||||
|
||||
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.ok(service.intervalId);
|
||||
|
||||
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 and starts polling', async function (assert) {
|
||||
const service = this.owner.lookup('service:bitcoin');
|
||||
service.fetchPrice = async () => {};
|
||||
|
||||
await service.enable(60000);
|
||||
|
||||
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(integration.status, 'active');
|
||||
assert.strictEqual(integration.type, 'bitcoin');
|
||||
assert.strictEqual(integration.interval, 60000);
|
||||
assert.ok(service.intervalId);
|
||||
|
||||
service.stopPolling();
|
||||
});
|
||||
|
||||
test('fetchPrice parses the ticker 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;
|
||||
await service.fetchPrice();
|
||||
|
||||
assert.strictEqual(requestedUrl, BITCOIN_TICKER_URL);
|
||||
assert.strictEqual(service.price, 86246.18);
|
||||
assert.strictEqual(service.changePercent, 3.43);
|
||||
assert.strictEqual(service.error, null);
|
||||
assert.false(service.loading);
|
||||
});
|
||||
|
||||
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 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 () => ({ last: '1', percent_change_24: '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);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user