Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/assets/CurrencyRateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export class CurrencyRateController extends BaseController<CurrencyRateConfig, C
private mutex = new Mutex();
private handle?: NodeJS.Timer;

private getCurrentCurrencyFromState(state?: Partial<CurrencyRateState>) {
return (state && state.currentCurrency) ? state.currentCurrency : 'usd';
}

private getPricingURL(currentCurrency: string, nativeCurrency: string) {
return (
`https://min-api.cryptocompare.com/data/price?fsym=` +
Expand All @@ -65,7 +69,7 @@ export class CurrencyRateController extends BaseController<CurrencyRateConfig, C
constructor(config?: Partial<CurrencyRateConfig>, state?: Partial<CurrencyRateState>) {
super(config, state);
this.defaultConfig = {
currentCurrency: 'usd',
currentCurrency: this.getCurrentCurrencyFromState(state),
disabled: true,
interval: 180000,
nativeCurrency: 'ETH'
Expand Down
11 changes: 11 additions & 0 deletions tests/CurrencyRateController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ describe('CurrencyRateController', () => {
});
});

it('should initialize with the currency in state', () => {
const existingState = { currentCurrency: 'rep' };
const controller = new CurrencyRateController({}, existingState);
expect(controller.config).toEqual({
currentCurrency: 'rep',
disabled: false,
interval: 180000,
nativeCurrency: 'ETH'
});
});

it('should poll and update rate in the right interval', () => {
return new Promise((resolve) => {
const controller = new CurrencyRateController({ interval: 100 });
Expand Down