Skip to content

Commit

Permalink
stash
Browse files Browse the repository at this point in the history
  • Loading branch information
MajorLift committed Jul 5, 2024
1 parent a3a060c commit 7359c4b
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 80 deletions.
144 changes: 122 additions & 22 deletions packages/composable-controller/src/ComposableController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import { JsonRpcEngine } from '@metamask/json-rpc-engine';
import type { Patch } from 'immer';
import * as sinon from 'sinon';

import type { ComposableControllerEvents } from './ComposableController';
import type {
ChildControllerStateChangeEvents,
ComposableControllerEvents,
} from './ComposableController';
import { ComposableController } from './ComposableController';

// Mock BaseController classes
Expand Down Expand Up @@ -130,6 +133,18 @@ class BarController extends BaseControllerV1<never, BarControllerState> {
type BazControllerState = BaseState & {
baz: string;
};
type BazControllerEvent = {
type: `BazController:stateChange`;
payload: [BazControllerState, Patch[]];
};

type BazMessenger = RestrictedControllerMessenger<
'BazController',
never,
BazControllerEvent,
never,
never
>;

class BazController extends BaseControllerV1<never, BazControllerState> {
defaultState = {
Expand All @@ -138,12 +153,60 @@ class BazController extends BaseControllerV1<never, BazControllerState> {

override name = 'BazController' as const;

constructor() {
protected messagingSystem: BazMessenger;

constructor({ messenger }: { messenger: BazMessenger }) {
super();
this.initialize();
this.messagingSystem = messenger;
}
}

// Mock non-controller class

type NonControllerMessenger = RestrictedControllerMessenger<
'NonController',
never,
never,
never,
never
>;

class NonController {
name: string;

messagingSystem: NonControllerMessenger;

constructor({
name,
messenger,
}: {
name: string;
messenger: NonControllerMessenger;
}) {
this.name = name;
this.messagingSystem = messenger;
}
}

type ControllersMap = {
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/naming-convention
FooController: FooController;
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/naming-convention
QuzController: QuzController;
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/naming-convention
BarController: BarController;
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/naming-convention
BazController: BazController;
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/naming-convention
NonController: NonController;
};

describe('ComposableController', () => {
afterEach(() => {
sinon.restore();
Expand All @@ -159,16 +222,30 @@ describe('ComposableController', () => {
// eslint-disable-next-line @typescript-eslint/naming-convention
BazController: BazControllerState;
};

const composableMessenger = new ControllerMessenger<
never,
ComposableControllerEvents<ComposableControllerState>
| ComposableControllerEvents<ComposableControllerState>
| ChildControllerStateChangeEvents<ComposableControllerState>
>().getRestricted({
name: 'ComposableController',
allowedActions: [],
allowedEvents: [],
allowedEvents: ['BazController:stateChange'],
});
const controller = new ComposableController({
controllers: [new BarController(), new BazController()],
const controller = new ComposableController<
ComposableControllerState,
ControllersMap[keyof ComposableControllerState]
>({
controllers: [
new BarController(),
new BazController({
messenger: new ControllerMessenger<never, never>().getRestricted({
name: 'BazController',
allowedActions: [],
allowedEvents: [],
}),
}),
],
messenger: composableMessenger,
});

Expand All @@ -194,7 +271,10 @@ describe('ComposableController', () => {
allowedEvents: [],
});
const barController = new BarController();
new ComposableController({
new ComposableController<
ComposableControllerState,
ControllersMap[keyof ComposableControllerState]
>({
controllers: [barController],
messenger: composableMessenger,
});
Expand Down Expand Up @@ -255,11 +335,13 @@ describe('ComposableController', () => {
'QuzController:stateChange',
],
});
const composableController =
new ComposableController<ComposableControllerState>({
controllers: [fooController, quzController],
messenger: composableControllerMessenger,
});
const composableController = new ComposableController<
ComposableControllerState,
ControllersMap[keyof ComposableControllerState]
>({
controllers: [fooController, quzController],
messenger: composableControllerMessenger,
});
expect(composableController.state).toStrictEqual({
FooController: { foo: 'foo' },
QuzController: { quz: 'quz' },
Expand Down Expand Up @@ -288,7 +370,10 @@ describe('ComposableController', () => {
allowedActions: [],
allowedEvents: ['FooController:stateChange'],
});
new ComposableController<ComposableControllerState>({
new ComposableController<
ComposableControllerState,
ControllersMap[keyof ComposableControllerState]
>({
controllers: [fooController],
messenger: composableControllerMessenger,
});
Expand Down Expand Up @@ -318,6 +403,9 @@ describe('ComposableController', () => {
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/naming-convention
FooController: FooControllerState;
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/naming-convention
NonController: never;
};
const barController = new BarController();
const controllerMessenger = new ControllerMessenger<
Expand All @@ -336,14 +424,17 @@ describe('ComposableController', () => {
allowedActions: [],
allowedEvents: ['FooController:stateChange'],
});
const composableController =
new ComposableController<ComposableControllerState>({
controllers: [barController, fooController],
messenger: composableControllerMessenger,
});
const composableController = new ComposableController<
ComposableControllerState,
ControllersMap[keyof ComposableControllerState]
>({
controllers: [barController, fooController],
messenger: composableControllerMessenger,
});
expect(composableController.state).toStrictEqual({
BarController: { bar: 'bar' },
FooController: { foo: 'foo' },
NonController: {},
});
});

Expand Down Expand Up @@ -373,7 +464,10 @@ describe('ComposableController', () => {
allowedActions: [],
allowedEvents: ['FooController:stateChange'],
});
new ComposableController<ComposableControllerState>({
new ComposableController<
ComposableControllerState,
ControllersMap[keyof ComposableControllerState]
>({
controllers: [barController, fooController],
messenger: composableControllerMessenger,
});
Expand Down Expand Up @@ -421,7 +515,10 @@ describe('ComposableController', () => {
allowedActions: [],
allowedEvents: ['FooController:stateChange'],
});
new ComposableController<ComposableControllerState>({
new ComposableController<
ComposableControllerState,
ControllersMap[keyof ComposableControllerState]
>({
controllers: [barController, fooController],
messenger: composableControllerMessenger,
});
Expand Down Expand Up @@ -490,13 +587,16 @@ describe('ComposableController', () => {
});
expect(
() =>
new ComposableController({
new ComposableController<
ComposableControllerState,
ControllersMap[keyof ComposableControllerState]
>({
// @ts-expect-error - Suppressing type error to test for runtime error handling
controllers: [notController, fooController],
messenger: composableControllerMessenger,
}),
).toThrow(
'Invalid controller: controller must extend from BaseController or BaseControllerV1',
'Invalid component: component must be a MessengerConsumer or a controller inheriting from BaseControllerV1.',
);
});
});
Expand Down
Loading

0 comments on commit 7359c4b

Please sign in to comment.