Skip to content

Commit

Permalink
test(base-controller): Add tests for isBaseController{,V1}, raising…
Browse files Browse the repository at this point in the history
… coverage to 100%
  • Loading branch information
MajorLift committed Aug 1, 2024
1 parent 7804ce6 commit b42590f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/base-controller/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = merge(baseConfig, {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 96.29,
branches: 100,
functions: 100,
lines: 100,
statements: 100,
Expand Down
47 changes: 45 additions & 2 deletions packages/base-controller/src/BaseControllerV1.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,61 @@
import * as sinon from 'sinon';

import { JsonRpcEngine } from '../../json-rpc-engine/src';
import type { BaseConfig, BaseState } from './BaseControllerV1';
import { BaseControllerV1 as BaseController } from './BaseControllerV1';
import {
BaseControllerV1 as BaseController,
isBaseControllerV1,
} from './BaseControllerV1';
import type {
CountControllerAction,
CountControllerEvent,
} from './BaseControllerV2.test';
import {
CountController,
countControllerName,
countControllerStateMetadata,
getCountMessenger,
} from './BaseControllerV2.test';
import { ControllerMessenger } from './ControllerMessenger';

const STATE = { name: 'foo' };
const CONFIG = { disabled: true };

class TestController extends BaseController<BaseConfig, BaseState> {
// eslint-disable-next-line jest/no-export
export class TestController extends BaseController<BaseConfig, BaseState> {
constructor(config?: BaseConfig, state?: BaseState) {
super(config, state);
this.initialize();
}
}

describe('isBaseControllerV1', () => {
it('should return false if passed a V1 controller', () => {
const controller = new TestController();
expect(isBaseControllerV1(controller)).toBe(true);
});

it('should return false if passed a V2 controller', () => {
const controllerMessenger = new ControllerMessenger<
CountControllerAction,
CountControllerEvent
>();
const controller = new CountController({
messenger: getCountMessenger(controllerMessenger),
name: countControllerName,
state: { count: 0 },
metadata: countControllerStateMetadata,
});
expect(isBaseControllerV1(controller)).toBe(false);
});

it('should return false if passed a non-controller', () => {
const notController = new JsonRpcEngine();
// @ts-expect-error Intentionally passing invalid input to test runtime behavior
expect(isBaseControllerV1(notController)).toBe(false);
});
});

describe('BaseController', () => {
afterEach(() => {
sinon.restore();
Expand Down
43 changes: 37 additions & 6 deletions packages/base-controller/src/BaseControllerV2.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* eslint-disable jest/no-export */
import type { Draft, Patch } from 'immer';
import * as sinon from 'sinon';

import { JsonRpcEngine } from '../../json-rpc-engine/src';
import { TestController } from './BaseControllerV1.test';
import type {
ControllerGetStateAction,
ControllerStateChangeEvent,
Expand All @@ -9,27 +12,28 @@ import {
BaseController,
getAnonymizedState,
getPersistentState,
isBaseController,
} from './BaseControllerV2';
import { ControllerMessenger } from './ControllerMessenger';
import type { RestrictedControllerMessenger } from './RestrictedControllerMessenger';

const countControllerName = 'CountController';
export const countControllerName = 'CountController';

type CountControllerState = {
count: number;
};

type CountControllerAction = ControllerGetStateAction<
export type CountControllerAction = ControllerGetStateAction<
typeof countControllerName,
CountControllerState
>;

type CountControllerEvent = ControllerStateChangeEvent<
export type CountControllerEvent = ControllerStateChangeEvent<
typeof countControllerName,
CountControllerState
>;

const countControllerStateMetadata = {
export const countControllerStateMetadata = {
count: {
persist: true,
anonymous: true,
Expand All @@ -50,7 +54,7 @@ type CountMessenger = RestrictedControllerMessenger<
* @param controllerMessenger - The controller messenger.
* @returns A restricted controller messenger for the Count controller.
*/
function getCountMessenger(
export function getCountMessenger(
controllerMessenger?: ControllerMessenger<
CountControllerAction,
CountControllerEvent
Expand All @@ -69,7 +73,7 @@ function getCountMessenger(
});
}

class CountController extends BaseController<
export class CountController extends BaseController<
typeof countControllerName,
CountControllerState,
CountMessenger
Expand Down Expand Up @@ -177,6 +181,33 @@ class MessagesController extends BaseController<
}
}

describe('isBaseController', () => {
it('should return true if passed a V2 controller', () => {
const controllerMessenger = new ControllerMessenger<
CountControllerAction,
CountControllerEvent
>();
const controller = new CountController({
messenger: getCountMessenger(controllerMessenger),
name: countControllerName,
state: { count: 0 },
metadata: countControllerStateMetadata,
});
expect(isBaseController(controller)).toBe(true);
});

it('should return false if passed a V1 controller', () => {
const controller = new TestController();
expect(isBaseController(controller)).toBe(false);
});

it('should return false if passed a non-controller', () => {
const notController = new JsonRpcEngine();
// @ts-expect-error Intentionally passing invalid input to test runtime behavior
expect(isBaseController(notController)).toBe(false);
});
});

describe('BaseController', () => {
afterEach(() => {
sinon.restore();
Expand Down

0 comments on commit b42590f

Please sign in to comment.