flosight-ui/app/src/providers/currency/currency.spec.ts
2017-08-10 14:39:35 -04:00

79 lines
2.5 KiB
TypeScript

/* tslint:disable:no-unused-variable */
import { TestBed, ComponentFixture, inject } from '@angular/core/testing';
import { HttpModule } from '@angular/http';
import { CurrencyProvider } from './currency';
describe('CurrencyProvider', () => {
let currency;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpModule
],
providers: [
CurrencyProvider
]
});
});
beforeEach(inject([CurrencyProvider], _cp => {
currency = _cp;
}));
it('initialises', () => {
expect(currency).not.toBeNull();
expect(currency.defaultCurrency).toBe('BTC');
expect(currency.currencySymbol).toBe('BTC');
expect(currency.factor).toBe(1);
});
it('sets currency by updating the symbol and changing the multiplication factor', () => {
currency.setCurrency('USD');
expect(currency.currencySymbol).toBe('USD');
expect(currency.factor).toEqual(1);
currency.setCurrency('BTC');
expect(currency.currencySymbol).toBe('BTC');
expect(currency.factor).toEqual(1);
currency.setCurrency('mBTC');
expect(currency.currencySymbol).toBe('mBTC');
expect(currency.factor).toEqual(1000);
currency.setCurrency('bits');
expect(currency.currencySymbol).toBe('bits');
expect(currency.factor).toEqual(1000000);
});
it('rounds float using specified number of decimal places', () => {
let aFloat = 4.32943;
expect(currency.roundFloat(aFloat, 2)).toBe(4.33);
expect(currency.roundFloat(aFloat, 3)).toBe(4.329);
expect(currency.roundFloat(aFloat, 4)).toBe(4.3294);
expect(currency.roundFloat(aFloat, 5)).toBe(4.32943);
aFloat = 1234567890.09876543;
expect(currency.roundFloat(aFloat, 2)).toBe(1234567890.10);
expect(currency.roundFloat(aFloat, 3)).toBe(1234567890.099);
expect(currency.roundFloat(aFloat, 4)).toBe(1234567890.0988);
expect(currency.roundFloat(aFloat, 5)).toBe(1234567890.09877);
expect(currency.roundFloat(aFloat, 6)).toBe(1234567890.098765);
expect(currency.roundFloat(aFloat, 7)).toBe(1234567890.0987654);
expect(currency.roundFloat(aFloat, 8)).toBe(1234567890.09876543);
});
it('gets proper conversion after changing currency', () => {
let aFloat = 12345.09876543;
expect(currency.getConversion(aFloat)).toBe('12345.09876543 BTC');
currency.setCurrency('mBTC');
expect(currency.getConversion(aFloat)).toBe('12345098.76543 mBTC');
currency.setCurrency('bits');
expect(currency.getConversion(aFloat)).toBe('12345098765.43 bits');
});
});