2021-05-07 21:38:24 +02:00
|
|
|
import { strict as assert } from 'assert';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { ObservableStore } from '@metamask/obs-store';
|
2021-05-20 04:57:51 +02:00
|
|
|
import {
|
|
|
|
BaseController,
|
|
|
|
BaseControllerV2,
|
|
|
|
ControllerMessenger,
|
|
|
|
} from '@metamask/controllers';
|
2021-03-16 22:00:08 +01:00
|
|
|
import ComposableObservableStore from './ComposableObservableStore';
|
2018-04-13 19:13:36 +02:00
|
|
|
|
2021-05-20 04:57:51 +02:00
|
|
|
class OldExampleController extends BaseController {
|
|
|
|
name = 'OldExampleController';
|
|
|
|
|
|
|
|
defaultState = {
|
|
|
|
baz: 'baz',
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateBaz(contents) {
|
|
|
|
this.update({ baz: contents });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class ExampleController extends BaseControllerV2 {
|
|
|
|
static defaultState = {
|
|
|
|
bar: 'bar',
|
|
|
|
};
|
|
|
|
|
|
|
|
static metadata = {
|
|
|
|
bar: { persist: true, anonymous: true },
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor({ messenger }) {
|
|
|
|
super({
|
|
|
|
messenger,
|
|
|
|
name: 'ExampleController',
|
|
|
|
metadata: ExampleController.metadata,
|
|
|
|
state: ExampleController.defaultState,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
updateBar(contents) {
|
|
|
|
this.update(() => {
|
|
|
|
return { bar: contents };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('ComposableObservableStore', function () {
|
|
|
|
it('should register initial state', function () {
|
2021-05-20 04:57:51 +02:00
|
|
|
const controllerMessenger = new ControllerMessenger();
|
|
|
|
const store = new ComposableObservableStore({
|
|
|
|
controllerMessenger,
|
|
|
|
state: 'state',
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
assert.strictEqual(store.getState(), 'state');
|
|
|
|
});
|
2018-04-13 19:13:36 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should register initial structure', function () {
|
2021-05-20 04:57:51 +02:00
|
|
|
const controllerMessenger = new ControllerMessenger();
|
2021-02-04 19:15:23 +01:00
|
|
|
const testStore = new ObservableStore();
|
2021-05-20 04:57:51 +02:00
|
|
|
const store = new ComposableObservableStore({
|
|
|
|
config: { TestStore: testStore },
|
|
|
|
controllerMessenger,
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
testStore.putState('state');
|
|
|
|
assert.deepEqual(store.getState(), { TestStore: 'state' });
|
|
|
|
});
|
2018-04-13 19:13:36 +02:00
|
|
|
|
2021-05-20 04:57:51 +02:00
|
|
|
it('should update structure with observable store', function () {
|
|
|
|
const controllerMessenger = new ControllerMessenger();
|
2021-02-04 19:15:23 +01:00
|
|
|
const testStore = new ObservableStore();
|
2021-05-20 04:57:51 +02:00
|
|
|
const store = new ComposableObservableStore({ controllerMessenger });
|
2021-02-04 19:15:23 +01:00
|
|
|
store.updateStructure({ TestStore: testStore });
|
|
|
|
testStore.putState('state');
|
|
|
|
assert.deepEqual(store.getState(), { TestStore: 'state' });
|
|
|
|
});
|
2018-04-13 19:13:36 +02:00
|
|
|
|
2021-05-20 04:57:51 +02:00
|
|
|
it('should update structure with BaseController-based controller', function () {
|
|
|
|
const controllerMessenger = new ControllerMessenger();
|
|
|
|
const oldExampleController = new OldExampleController();
|
|
|
|
const store = new ComposableObservableStore({ controllerMessenger });
|
|
|
|
store.updateStructure({ OldExample: oldExampleController });
|
|
|
|
oldExampleController.updateBaz('state');
|
|
|
|
assert.deepEqual(store.getState(), { OldExample: { baz: 'state' } });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update structure with BaseControllerV2-based controller', function () {
|
|
|
|
const controllerMessenger = new ControllerMessenger();
|
|
|
|
const exampleController = new ExampleController({
|
|
|
|
messenger: controllerMessenger,
|
|
|
|
});
|
|
|
|
const store = new ComposableObservableStore({ controllerMessenger });
|
|
|
|
store.updateStructure({ Example: exampleController });
|
|
|
|
exampleController.updateBar('state');
|
|
|
|
console.log(exampleController.state);
|
|
|
|
assert.deepEqual(store.getState(), { Example: { bar: 'state' } });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update structure with all three types of stores', function () {
|
|
|
|
const controllerMessenger = new ControllerMessenger();
|
|
|
|
const exampleStore = new ObservableStore();
|
|
|
|
const exampleController = new ExampleController({
|
|
|
|
messenger: controllerMessenger,
|
|
|
|
});
|
|
|
|
const oldExampleController = new OldExampleController();
|
|
|
|
const store = new ComposableObservableStore({ controllerMessenger });
|
|
|
|
store.updateStructure({
|
|
|
|
Example: exampleController,
|
|
|
|
OldExample: oldExampleController,
|
|
|
|
Store: exampleStore,
|
|
|
|
});
|
|
|
|
exampleStore.putState('state');
|
|
|
|
exampleController.updateBar('state');
|
|
|
|
oldExampleController.updateBaz('state');
|
|
|
|
assert.deepEqual(store.getState(), {
|
|
|
|
Example: { bar: 'state' },
|
|
|
|
OldExample: { baz: 'state' },
|
|
|
|
Store: 'state',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should return flattened state', function () {
|
2021-05-20 04:57:51 +02:00
|
|
|
const controllerMessenger = new ControllerMessenger();
|
2021-02-04 19:15:23 +01:00
|
|
|
const fooStore = new ObservableStore({ foo: 'foo' });
|
2021-05-20 04:57:51 +02:00
|
|
|
const barController = new ExampleController({
|
|
|
|
messenger: controllerMessenger,
|
|
|
|
});
|
|
|
|
const bazController = new OldExampleController();
|
|
|
|
const store = new ComposableObservableStore({
|
|
|
|
config: {
|
|
|
|
FooStore: fooStore,
|
|
|
|
BarStore: barController,
|
|
|
|
BazStore: bazController,
|
|
|
|
},
|
|
|
|
controllerMessenger,
|
|
|
|
state: {
|
|
|
|
FooStore: fooStore.getState(),
|
|
|
|
BarStore: barController.state,
|
|
|
|
BazStore: bazController.state,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
assert.deepEqual(store.getFlatState(), {
|
|
|
|
foo: 'foo',
|
|
|
|
bar: 'bar',
|
|
|
|
baz: 'baz',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2021-05-11 18:39:17 +02:00
|
|
|
|
|
|
|
it('should return empty flattened state when not configured', function () {
|
2021-05-20 04:57:51 +02:00
|
|
|
const controllerMessenger = new ControllerMessenger();
|
|
|
|
const store = new ComposableObservableStore({ controllerMessenger });
|
2021-05-11 18:39:17 +02:00
|
|
|
assert.deepEqual(store.getFlatState(), {});
|
|
|
|
});
|
2021-05-20 04:57:51 +02:00
|
|
|
|
|
|
|
it('should throw if the controller messenger is omitted and the config includes a BaseControllerV2 controller', function () {
|
|
|
|
const controllerMessenger = new ControllerMessenger();
|
|
|
|
const exampleController = new ExampleController({
|
|
|
|
messenger: controllerMessenger,
|
|
|
|
});
|
|
|
|
assert.throws(
|
|
|
|
() =>
|
|
|
|
new ComposableObservableStore({
|
|
|
|
config: {
|
|
|
|
Example: exampleController,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw if the controller messenger is omitted and updateStructure called with a BaseControllerV2 controller', function () {
|
|
|
|
const controllerMessenger = new ControllerMessenger();
|
|
|
|
const exampleController = new ExampleController({
|
|
|
|
messenger: controllerMessenger,
|
|
|
|
});
|
|
|
|
const store = new ComposableObservableStore({});
|
|
|
|
assert.throws(() => store.updateStructure({ Example: exampleController }));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw if initialized with undefined config entry', function () {
|
|
|
|
const controllerMessenger = new ControllerMessenger();
|
|
|
|
assert.throws(
|
|
|
|
() =>
|
|
|
|
new ComposableObservableStore({
|
|
|
|
config: {
|
|
|
|
Example: undefined,
|
|
|
|
},
|
|
|
|
controllerMessenger,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|