-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: upgrade BridgeController to BaseController V2
- Loading branch information
Showing
5 changed files
with
106 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,42 @@ | ||
import nock from 'nock'; | ||
import { BRIDGE_API_BASE_URL } from '../../../../shared/constants/bridge'; | ||
import BridgeController from './bridge-controller'; | ||
import { BridgeControllerMessenger } from './types'; | ||
import { DEFAULT_BRIDGE_CONTROLLER_STATE } from './constants'; | ||
|
||
const EMPTY_INIT_STATE = { | ||
bridgeState: { | ||
bridgeFeatureFlags: { | ||
extensionSupport: false, | ||
}, | ||
}, | ||
bridgeState: DEFAULT_BRIDGE_CONTROLLER_STATE, | ||
}; | ||
|
||
const messengerMock = { | ||
call: jest.fn(), | ||
registerActionHandler: jest.fn(), | ||
registerInitialEventPayload: jest.fn(), | ||
publish: jest.fn(), | ||
} as unknown as jest.Mocked<BridgeControllerMessenger>; | ||
|
||
describe('BridgeController', function () { | ||
let bridgeController: BridgeController; | ||
|
||
beforeAll(function () { | ||
bridgeController = new BridgeController(); | ||
bridgeController = new BridgeController({ messenger: messengerMock }); | ||
}); | ||
|
||
it('constructor should setup correctly', function () { | ||
expect(bridgeController.store.getState()).toStrictEqual(EMPTY_INIT_STATE); | ||
expect(bridgeController.state).toStrictEqual(EMPTY_INIT_STATE); | ||
}); | ||
|
||
it('setBridgeFeatureFlags should fetch and set the bridge feature flags', async function () { | ||
nock(BRIDGE_API_BASE_URL).get('/getAllFeatureFlags').reply(200, { | ||
'extension-support': true, | ||
}); | ||
expect( | ||
bridgeController.store.getState().bridgeState.bridgeFeatureFlags, | ||
).toStrictEqual({ extensionSupport: false }); | ||
expect(bridgeController.state.bridgeState.bridgeFeatureFlags).toStrictEqual( | ||
{ extensionSupport: false }, | ||
); | ||
|
||
await bridgeController.setBridgeFeatureFlags(); | ||
expect( | ||
bridgeController.store.getState().bridgeState.bridgeFeatureFlags, | ||
).toStrictEqual({ extensionSupport: true }); | ||
expect(bridgeController.state.bridgeState.bridgeFeatureFlags).toStrictEqual( | ||
{ extensionSupport: true }, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,50 @@ | ||
import { ObservableStore } from '@metamask/obs-store'; | ||
import { BaseController, StateMetadata } from '@metamask/base-controller'; | ||
import { fetchBridgeFeatureFlags } from '../../../../ui/pages/bridge/bridge.util'; | ||
import { DEFAULT_BRIDGE_CONTROLLER_STATE } from './constants'; | ||
import { | ||
BRIDGE_CONTROLLER_NAME, | ||
DEFAULT_BRIDGE_CONTROLLER_STATE, | ||
} from './constants'; | ||
import { BridgeControllerState, BridgeControllerMessenger } from './types'; | ||
|
||
export default class BridgeController { | ||
store = new ObservableStore({ bridgeState: DEFAULT_BRIDGE_CONTROLLER_STATE }); | ||
const metadata: StateMetadata<{ bridgeState: BridgeControllerState }> = { | ||
bridgeState: { | ||
persist: false, | ||
anonymous: false, | ||
}, | ||
}; | ||
|
||
export default class BridgeController extends BaseController< | ||
typeof BRIDGE_CONTROLLER_NAME, | ||
{ bridgeState: BridgeControllerState }, | ||
BridgeControllerMessenger | ||
> { | ||
constructor({ messenger }: { messenger: BridgeControllerMessenger }) { | ||
super({ | ||
name: BRIDGE_CONTROLLER_NAME, | ||
metadata, | ||
messenger, | ||
state: { bridgeState: DEFAULT_BRIDGE_CONTROLLER_STATE }, | ||
}); | ||
|
||
this.messagingSystem.registerActionHandler( | ||
`${BRIDGE_CONTROLLER_NAME}:setBridgeFeatureFlags`, | ||
this.setBridgeFeatureFlags.bind(this), | ||
); | ||
} | ||
|
||
resetState = () => { | ||
this.store.updateState({ | ||
bridgeState: { | ||
this.update((_state) => { | ||
_state.bridgeState = { | ||
...DEFAULT_BRIDGE_CONTROLLER_STATE, | ||
}, | ||
}; | ||
}); | ||
}; | ||
|
||
setBridgeFeatureFlags = async () => { | ||
const { bridgeState } = this.store.getState(); | ||
const { bridgeState } = this.state; | ||
const bridgeFeatureFlags = await fetchBridgeFeatureFlags(); | ||
this.store.updateState({ | ||
bridgeState: { ...bridgeState, bridgeFeatureFlags }, | ||
this.update((_state) => { | ||
_state.bridgeState = { ...bridgeState, bridgeFeatureFlags }; | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters