|
1 | 1 | import 'isomorphic-fetch';
|
2 | 2 | import { stub } from 'sinon';
|
3 |
| -import * as nock from 'nock'; |
4 | 3 | import CurrencyRateController from './CurrencyRateController';
|
5 | 4 |
|
6 | 5 | describe('CurrencyRateController', () => {
|
7 |
| - beforeEach(() => { |
8 |
| - nock(/.+/u) |
9 |
| - .get(/XYZ,USD/u) |
10 |
| - .reply(200, { XYZ: 123, USD: 456 }) |
11 |
| - .get(/DEF,USD/u) |
12 |
| - .reply(200, { DEF: 123 }) |
13 |
| - .get(/.+/u) |
14 |
| - .reply(200, { USD: 1337 }) |
15 |
| - .persist(); |
16 |
| - }); |
17 |
| - |
18 |
| - afterEach(() => { |
19 |
| - nock.cleanAll(); |
20 |
| - }); |
21 |
| - |
22 | 6 | it('should set default state', () => {
|
23 |
| - const controller = new CurrencyRateController(); |
| 7 | + const fetchExchangeRateStub = stub(); |
| 8 | + const controller = new CurrencyRateController({}, {}, fetchExchangeRateStub); |
24 | 9 | expect(controller.state).toEqual({
|
25 | 10 | conversionDate: 0,
|
26 | 11 | conversionRate: 0,
|
27 | 12 | currentCurrency: 'usd',
|
28 | 13 | nativeCurrency: 'ETH',
|
29 | 14 | usdConversionRate: 0,
|
30 | 15 | });
|
| 16 | + |
| 17 | + controller.disabled = true; |
31 | 18 | });
|
32 | 19 |
|
33 | 20 | it('should initialize with the default config', () => {
|
34 |
| - const controller = new CurrencyRateController(); |
| 21 | + const fetchExchangeRateStub = stub(); |
| 22 | + const controller = new CurrencyRateController({}, {}, fetchExchangeRateStub); |
35 | 23 | expect(controller.config).toEqual({
|
36 | 24 | currentCurrency: 'usd',
|
37 | 25 | disabled: false,
|
38 | 26 | interval: 180000,
|
39 | 27 | nativeCurrency: 'ETH',
|
40 | 28 | includeUSDRate: false,
|
41 | 29 | });
|
| 30 | + |
| 31 | + controller.disabled = true; |
42 | 32 | });
|
43 | 33 |
|
44 | 34 | it('should initialize with the currency in state', () => {
|
| 35 | + const fetchExchangeRateStub = stub(); |
45 | 36 | const existingState = { currentCurrency: 'rep' };
|
46 |
| - const controller = new CurrencyRateController({}, existingState); |
| 37 | + const controller = new CurrencyRateController({}, existingState, fetchExchangeRateStub); |
47 | 38 | expect(controller.config).toEqual({
|
48 | 39 | currentCurrency: 'rep',
|
49 | 40 | disabled: false,
|
50 | 41 | interval: 180000,
|
51 | 42 | nativeCurrency: 'ETH',
|
52 | 43 | includeUSDRate: false,
|
53 | 44 | });
|
| 45 | + |
| 46 | + controller.disabled = true; |
54 | 47 | });
|
55 | 48 |
|
56 |
| - it('should poll and update rate in the right interval', () => { |
57 |
| - return new Promise((resolve) => { |
58 |
| - const controller = new CurrencyRateController({ interval: 100 }); |
59 |
| - const mock = stub(controller, 'fetchExchangeRate'); |
60 |
| - setTimeout(() => { |
61 |
| - expect(mock.called).toBe(true); |
62 |
| - expect(mock.calledTwice).toBe(false); |
63 |
| - }, 1); |
64 |
| - setTimeout(() => { |
65 |
| - expect(mock.calledTwice).toBe(true); |
66 |
| - mock.restore(); |
67 |
| - resolve(); |
68 |
| - }, 150); |
69 |
| - }); |
| 49 | + it('should poll and update rate in the right interval', async () => { |
| 50 | + const fetchExchangeRateStub = stub(); |
| 51 | + const controller = new CurrencyRateController({ interval: 100 }, {}, fetchExchangeRateStub); |
| 52 | + |
| 53 | + await new Promise((resolve) => setTimeout(() => resolve(), 1)); |
| 54 | + expect(fetchExchangeRateStub.called).toBe(true); |
| 55 | + expect(fetchExchangeRateStub.calledTwice).toBe(false); |
| 56 | + await new Promise((resolve) => setTimeout(() => resolve(), 150)); |
| 57 | + expect(fetchExchangeRateStub.calledTwice).toBe(true); |
| 58 | + |
| 59 | + controller.disabled = true; |
70 | 60 | });
|
71 | 61 |
|
72 | 62 | it('should not update rates if disabled', async () => {
|
73 |
| - const controller = new CurrencyRateController({ |
74 |
| - interval: 10, |
75 |
| - }); |
76 |
| - controller.fetchExchangeRate = stub().resolves({}); |
| 63 | + const fetchExchangeRateStub = stub().resolves({}); |
| 64 | + const controller = new CurrencyRateController({ interval: 10 }, {}, fetchExchangeRateStub); |
77 | 65 | controller.disabled = true;
|
| 66 | + |
78 | 67 | await controller.updateExchangeRate();
|
79 |
| - expect((controller.fetchExchangeRate as any).called).toBe(false); |
| 68 | + expect(fetchExchangeRateStub.called).toBe(false); |
80 | 69 | });
|
81 | 70 |
|
82 | 71 | it('should clear previous interval', () => {
|
| 72 | + const fetchExchangeRateStub = stub(); |
83 | 73 | const mock = stub(global, 'clearTimeout');
|
84 |
| - const controller = new CurrencyRateController({ interval: 1337 }); |
| 74 | + const controller = new CurrencyRateController({ interval: 1337 }, {}, fetchExchangeRateStub); |
85 | 75 | return new Promise((resolve) => {
|
86 | 76 | setTimeout(() => {
|
87 | 77 | controller.poll(1338);
|
88 | 78 | expect(mock.called).toBe(true);
|
89 | 79 | mock.restore();
|
| 80 | + |
| 81 | + controller.disabled = true; |
90 | 82 | resolve();
|
91 | 83 | }, 100);
|
92 | 84 | });
|
93 | 85 | });
|
94 | 86 |
|
95 | 87 | it('should update currency', async () => {
|
96 |
| - const controller = new CurrencyRateController({ interval: 10 }); |
| 88 | + const fetchExchangeRateStub = stub().resolves({ conversionRate: 10 }); |
| 89 | + const controller = new CurrencyRateController({ interval: 10 }, {}, fetchExchangeRateStub); |
97 | 90 | expect(controller.state.conversionRate).toEqual(0);
|
98 | 91 | await controller.updateExchangeRate();
|
99 |
| - expect(controller.state.conversionRate).toBeGreaterThan(0); |
100 |
| - }); |
101 |
| - |
102 |
| - it('should add usd rate to state when includeUSDRate is configured true', async () => { |
103 |
| - const controller = new CurrencyRateController({ includeUSDRate: true, currentCurrency: 'xyz' }); |
104 |
| - expect(controller.state.usdConversionRate).toEqual(0); |
105 |
| - await controller.updateExchangeRate(); |
106 |
| - expect(controller.state.usdConversionRate).toEqual(456); |
107 |
| - }); |
| 92 | + expect(controller.state.conversionRate).toEqual(10); |
108 | 93 |
|
109 |
| - it('should add usd rate to state fetches when configured', async () => { |
110 |
| - const controller = new CurrencyRateController({ includeUSDRate: true }); |
111 |
| - const result = await controller.fetchExchangeRate('xyz', 'FOO', true); |
112 |
| - expect(result.usdConversionRate).toEqual(456); |
113 |
| - expect(result.conversionRate).toEqual(123); |
| 94 | + controller.disabled = true; |
114 | 95 | });
|
115 | 96 |
|
116 |
| - it('should throw correctly when configured to return usd but receives an invalid response for currentCurrency rate', async () => { |
117 |
| - const controller = new CurrencyRateController({ includeUSDRate: true }); |
118 |
| - await expect(controller.fetchExchangeRate('abc', 'FOO', true)).rejects.toThrow( |
119 |
| - 'Invalid response for ABC: undefined', |
| 97 | + it('should add usd rate to state when includeUSDRate is configured true', async () => { |
| 98 | + const fetchExchangeRateStub = stub().resolves({}); |
| 99 | + const controller = new CurrencyRateController( |
| 100 | + { includeUSDRate: true, currentCurrency: 'xyz' }, |
| 101 | + {}, |
| 102 | + fetchExchangeRateStub, |
120 | 103 | );
|
121 |
| - }); |
122 | 104 |
|
123 |
| - it('should throw correctly when configured to return usd but receives an invalid response for usdConversionRate', async () => { |
124 |
| - const controller = new CurrencyRateController({ includeUSDRate: true }); |
125 |
| - await expect(controller.fetchExchangeRate('def', 'FOO', true)).rejects.toThrow( |
126 |
| - 'Invalid response for usdConversionRate: undefined', |
127 |
| - ); |
128 |
| - }); |
129 |
| - |
130 |
| - describe('#fetchExchangeRate', () => { |
131 |
| - it('should handle a valid symbol in the API response', async () => { |
132 |
| - const controller = new CurrencyRateController({ nativeCurrency: 'usd' }); |
133 |
| - const response = await controller.fetchExchangeRate('usd'); |
134 |
| - expect(response.conversionRate).toEqual(1337); |
135 |
| - }); |
| 105 | + await controller.updateExchangeRate(); |
136 | 106 |
|
137 |
| - it('should handle a missing symbol in the API response', async () => { |
138 |
| - const controller = new CurrencyRateController({ nativeCurrency: 'usd' }); |
139 |
| - await expect(controller.fetchExchangeRate('foo')).rejects.toThrow('Invalid response for FOO: undefined'); |
140 |
| - }); |
| 107 | + expect(fetchExchangeRateStub.alwaysCalledWithExactly('xyz', 'ETH', true)).toBe(true); |
141 | 108 | });
|
142 | 109 | });
|
0 commit comments