|
| 1 | +import { ControllerMessenger } from '@metamask/base-controller'; |
| 2 | +import { GasPricesController } from '@metamask/example-controllers'; |
| 3 | +import type { GasPricesControllerMessenger } from '@metamask/example-controllers'; |
| 4 | + |
| 5 | +import type { |
| 6 | + ExtractAvailableAction, |
| 7 | + ExtractAvailableEvent, |
| 8 | +} from '../../../packages/base-controller/tests/helpers'; |
| 9 | +import type { AbstractGasPricesService } from './gas-prices-service/abstract-gas-prices-service'; |
| 10 | +import { |
| 11 | + getDefaultNetworkControllerState, |
| 12 | + type NetworkControllerGetStateAction, |
| 13 | +} from './network-controller-types'; |
| 14 | + |
| 15 | +describe('GasPricesController', () => { |
| 16 | + describe('constructor', () => { |
| 17 | + it('uses all of the given state properties to initialize state', () => { |
| 18 | + const gasPricesService = buildGasPricesService(); |
| 19 | + const givenState = { |
| 20 | + gasPricesByChainId: { |
| 21 | + '0x1': { |
| 22 | + low: 10, |
| 23 | + average: 15, |
| 24 | + high: 20, |
| 25 | + fetchedDate: '2024-01-01', |
| 26 | + }, |
| 27 | + }, |
| 28 | + }; |
| 29 | + const controller = new GasPricesController({ |
| 30 | + messenger: getControllerMessenger(), |
| 31 | + state: givenState, |
| 32 | + gasPricesService, |
| 33 | + }); |
| 34 | + |
| 35 | + expect(controller.state).toStrictEqual(givenState); |
| 36 | + }); |
| 37 | + |
| 38 | + it('fills in missing state properties with default values', () => { |
| 39 | + const gasPricesService = buildGasPricesService(); |
| 40 | + const controller = new GasPricesController({ |
| 41 | + messenger: getControllerMessenger(), |
| 42 | + gasPricesService, |
| 43 | + }); |
| 44 | + |
| 45 | + expect(controller.state).toMatchInlineSnapshot(` |
| 46 | + Object { |
| 47 | + "gasPricesByChainId": Object {}, |
| 48 | + } |
| 49 | + `); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('updateGasPrices', () => { |
| 54 | + beforeEach(() => { |
| 55 | + jest.useFakeTimers().setSystemTime(new Date('2024-01-02')); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + jest.useRealTimers(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('fetches gas prices for the current chain through the service object and updates state accordingly', async () => { |
| 63 | + const gasPricesService = buildGasPricesService(); |
| 64 | + jest.spyOn(gasPricesService, 'fetchGasPrices').mockResolvedValue({ |
| 65 | + low: 5, |
| 66 | + average: 10, |
| 67 | + high: 15, |
| 68 | + }); |
| 69 | + const rootMessenger = getRootControllerMessenger({ |
| 70 | + networkControllerGetStateActionHandler: () => ({ |
| 71 | + ...getDefaultNetworkControllerState(), |
| 72 | + chainId: '0x42', |
| 73 | + }), |
| 74 | + }); |
| 75 | + const controller = new GasPricesController({ |
| 76 | + messenger: getControllerMessenger(rootMessenger), |
| 77 | + gasPricesService, |
| 78 | + }); |
| 79 | + |
| 80 | + await controller.updateGasPrices(); |
| 81 | + |
| 82 | + expect(controller.state).toStrictEqual({ |
| 83 | + gasPricesByChainId: { |
| 84 | + '0x42': { |
| 85 | + low: 5, |
| 86 | + average: 10, |
| 87 | + high: 15, |
| 88 | + fetchedDate: '2024-01-02T00:00:00.000Z', |
| 89 | + }, |
| 90 | + }, |
| 91 | + }); |
| 92 | + }); |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +/** |
| 97 | + * The union of actions that the root messenger allows. |
| 98 | + */ |
| 99 | +type RootAction = ExtractAvailableAction<GasPricesControllerMessenger>; |
| 100 | + |
| 101 | +/** |
| 102 | + * The union of events that the root messenger allows. |
| 103 | + */ |
| 104 | +type RootEvent = ExtractAvailableEvent<GasPricesControllerMessenger>; |
| 105 | + |
| 106 | +/** |
| 107 | + * Constructs the unrestricted messenger. This can be used to call actions and |
| 108 | + * publish events within the tests for this controller. |
| 109 | + * |
| 110 | + * @param args - The arguments to this function. |
| 111 | + * @param args.networkControllerGetStateActionHandler - Used to mock the |
| 112 | + * `NetworkController:getState` action on the messenger. |
| 113 | + * @returns The unrestricted messenger suited for GasPricesController. |
| 114 | + */ |
| 115 | +function getRootControllerMessenger({ |
| 116 | + networkControllerGetStateActionHandler = jest |
| 117 | + .fn< |
| 118 | + ReturnType<NetworkControllerGetStateAction['handler']>, |
| 119 | + Parameters<NetworkControllerGetStateAction['handler']> |
| 120 | + >() |
| 121 | + .mockReturnValue(getDefaultNetworkControllerState()), |
| 122 | +}: { |
| 123 | + networkControllerGetStateActionHandler?: NetworkControllerGetStateAction['handler']; |
| 124 | +} = {}): ControllerMessenger<RootAction, RootEvent> { |
| 125 | + const rootMessenger = new ControllerMessenger<RootAction, RootEvent>(); |
| 126 | + rootMessenger.registerActionHandler( |
| 127 | + 'NetworkController:getState', |
| 128 | + networkControllerGetStateActionHandler, |
| 129 | + ); |
| 130 | + return rootMessenger; |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * Constructs the messenger which is restricted to relevant GasPricesController |
| 135 | + * actions and events. |
| 136 | + * |
| 137 | + * @param rootMessenger - The root messenger to restrict. |
| 138 | + * @returns The restricted messenger. |
| 139 | + */ |
| 140 | +function getControllerMessenger( |
| 141 | + rootMessenger = getRootControllerMessenger(), |
| 142 | +): GasPricesControllerMessenger { |
| 143 | + return rootMessenger.getRestricted({ |
| 144 | + name: 'GasPricesController', |
| 145 | + allowedActions: ['NetworkController:getState'], |
| 146 | + allowedEvents: [], |
| 147 | + }); |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * Constructs a mock GasPricesService object for use in testing. |
| 152 | + * |
| 153 | + * @returns The mock GasPricesService object. |
| 154 | + */ |
| 155 | +function buildGasPricesService(): AbstractGasPricesService { |
| 156 | + return { |
| 157 | + fetchGasPrices: jest.fn(), |
| 158 | + }; |
| 159 | +} |
0 commit comments