Skip to content

Commit bc5238e

Browse files
authored
Changed updatePluginState to use proper key pluginStates (#48)
* Changed updatePluginState to use proper key `pluginStates` * Added test for PluginController updatePluginState and getPluginState * Update packages/controllers/src/plugins/PluginController.test.ts * Changed plugin code and assert plugin public state * Update packages/controllers/src/plugins/PluginController.test.ts Fix state
1 parent 9d33266 commit bc5238e

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

packages/controllers/src/plugins/PluginController.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,65 @@ describe('PluginController Controller', () => {
4040
name: 'PluginController',
4141
}),
4242
});
43+
4344
expect(pluginController).toBeDefined();
4445
});
46+
it('can create a worker and plugin controller and add a plugin and update its state', async () => {
47+
const workerExecutionEnvironment = new WebWorkerExecutionEnvironmentService(
48+
{
49+
setupPluginProvider: jest.fn(),
50+
workerUrl: new URL(URL.createObjectURL(new Blob([workerCode]))),
51+
},
52+
);
53+
const pluginController = new PluginController({
54+
terminateAllPlugins: workerExecutionEnvironment.terminateAllPlugins.bind(
55+
workerExecutionEnvironment,
56+
),
57+
terminatePlugin: workerExecutionEnvironment.terminatePlugin.bind(
58+
workerExecutionEnvironment,
59+
),
60+
executePlugin: workerExecutionEnvironment.executePlugin.bind(
61+
workerExecutionEnvironment,
62+
),
63+
getRpcMessageHandler:
64+
workerExecutionEnvironment.getRpcMessageHandler.bind(
65+
workerExecutionEnvironment,
66+
),
67+
removeAllPermissionsFor: jest.fn(),
68+
getPermissions: jest.fn(),
69+
hasPermission: jest.fn(),
70+
requestPermissions: jest.fn(),
71+
closeAllConnections: jest.fn(),
72+
messenger: new ControllerMessenger<any, any>().getRestricted({
73+
name: 'PluginController',
74+
}),
75+
});
76+
const plugin = await pluginController.add({
77+
name: 'TestPlugin',
78+
sourceCode: `
79+
wallet.registerRpcMessageHandler(async (origin, request) => {
80+
const {method, params, id} = request;
81+
return method + id;
82+
});
83+
`,
84+
manifest: {
85+
web3Wallet: {
86+
initialPermissions: {},
87+
},
88+
version: '0.0.0-development',
89+
},
90+
});
91+
92+
await pluginController.startPlugin(plugin.name);
93+
await pluginController.updatePluginState(plugin.name, { hello: 'world' });
94+
const pluginState = await pluginController.getPluginState(plugin.name);
95+
expect(pluginState).toEqual({ hello: 'world' });
96+
expect(pluginController.state.pluginStates).toEqual({
97+
TestPlugin: {
98+
hello: 'world',
99+
},
100+
});
101+
});
45102

46103
it('can add a plugin and use its JSON-RPC api with a WebWorkerExecutionEnvironmentService', async () => {
47104
const webWorkerExecutionEnvironment =

packages/controllers/src/plugins/PluginController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ export class PluginController extends BaseController<
342342
newPluginState: Json,
343343
): Promise<void> {
344344
this.update((state: any) => {
345-
state[pluginName] = newPluginState;
345+
state.pluginStates[pluginName] = newPluginState;
346346
});
347347
}
348348

0 commit comments

Comments
 (0)