Skip to content

Commit

Permalink
feat: migrate AppMedataController to inherit from BaseController V2 (
Browse files Browse the repository at this point in the history
…#28783)

<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

This PR migrate `AppMedataController` to inherit from BaseController V2

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/28783?quickstart=1)

## **Related issues**

Fixes: #28467

## **Manual testing steps**

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
  • Loading branch information
cryptodev-2s authored Nov 27, 2024
1 parent 781267a commit 208245a
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 108 deletions.
197 changes: 125 additions & 72 deletions app/scripts/controllers/app-metadata.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import AppMetadataController from './app-metadata';

const EXPECTED_DEFAULT_STATE = {
currentAppVersion: '',
previousAppVersion: '',
previousMigrationVersion: 0,
currentMigrationVersion: 0,
};
import { ControllerMessenger } from '@metamask/base-controller';
import AppMetadataController, {
getDefaultAppMetadataControllerState,
type AppMetadataControllerOptions,
} from './app-metadata';

describe('AppMetadataController', () => {
describe('constructor', () => {
Expand All @@ -16,86 +13,142 @@ describe('AppMetadataController', () => {
previousMigrationVersion: 1,
currentMigrationVersion: 1,
};
const appMetadataController = new AppMetadataController({
state: initState,
currentMigrationVersion: 1,
currentAppVersion: '1',
});
expect(appMetadataController.store.getState()).toStrictEqual(initState);
withController(
{
state: initState,
currentMigrationVersion: 1,
currentAppVersion: '1',
},
({ controller }) => {
expect(controller.state).toStrictEqual(initState);
},
);
});

it('sets default state and does not modify it', async () => {
const appMetadataController = new AppMetadataController({
state: {},
it('sets default state and does not modify it', () => {
withController({ state: {} }, ({ controller }) => {
expect(controller.state).toStrictEqual(
getDefaultAppMetadataControllerState(),
);
});
expect(appMetadataController.store.getState()).toStrictEqual(
EXPECTED_DEFAULT_STATE,
);
});

it('sets default state and does not modify it if options version parameters match respective default values', async () => {
const appMetadataController = new AppMetadataController({
state: {},
currentMigrationVersion: 0,
currentAppVersion: '',
});
expect(appMetadataController.store.getState()).toStrictEqual(
EXPECTED_DEFAULT_STATE,
it('sets default state and does not modify it if options version parameters match respective default values', () => {
withController(
{
state: {},
currentMigrationVersion: 0,
currentAppVersion: '',
},
({ controller }) => {
expect(controller.state).toStrictEqual(
getDefaultAppMetadataControllerState(),
);
},
);
});

it('updates the currentAppVersion state property if options.currentAppVersion does not match the default value', async () => {
const appMetadataController = new AppMetadataController({
state: {},
currentMigrationVersion: 0,
currentAppVersion: '1',
});
expect(appMetadataController.store.getState()).toStrictEqual({
...EXPECTED_DEFAULT_STATE,
currentAppVersion: '1',
});
it('updates the currentAppVersion state property if options.currentAppVersion does not match the default value', () => {
withController(
{
state: {},
currentMigrationVersion: 0,
currentAppVersion: '1',
},
({ controller }) => {
expect(controller.state).toStrictEqual({
...getDefaultAppMetadataControllerState(),
currentAppVersion: '1',
});
},
);
});

it('updates the currentAppVersion and previousAppVersion state properties if options.currentAppVersion, currentAppVersion and previousAppVersion are all different', async () => {
const appMetadataController = new AppMetadataController({
state: {
currentAppVersion: '2',
previousAppVersion: '1',
it('updates the currentAppVersion and previousAppVersion state properties if options.currentAppVersion, currentAppVersion and previousAppVersion are all different', () => {
withController(
{
state: {
currentAppVersion: '2',
previousAppVersion: '1',
},
currentAppVersion: '3',
currentMigrationVersion: 0,
},
currentAppVersion: '3',
currentMigrationVersion: 0,
});
expect(appMetadataController.store.getState()).toStrictEqual({
...EXPECTED_DEFAULT_STATE,
currentAppVersion: '3',
previousAppVersion: '2',
});
({ controller }) => {
expect(controller.state).toStrictEqual({
...getDefaultAppMetadataControllerState(),
currentAppVersion: '3',
previousAppVersion: '2',
});
},
);
});

it('updates the currentMigrationVersion state property if the currentMigrationVersion param does not match the default value', async () => {
const appMetadataController = new AppMetadataController({
state: {},
currentMigrationVersion: 1,
});
expect(appMetadataController.store.getState()).toStrictEqual({
...EXPECTED_DEFAULT_STATE,
currentMigrationVersion: 1,
});
it('updates the currentMigrationVersion state property if the currentMigrationVersion param does not match the default value', () => {
withController(
{
state: {},
currentMigrationVersion: 1,
},
({ controller }) => {
expect(controller.state).toStrictEqual({
...getDefaultAppMetadataControllerState(),
currentMigrationVersion: 1,
});
},
);
});

it('updates the currentMigrationVersion and previousMigrationVersion state properties if the currentMigrationVersion param, the currentMigrationVersion state property and the previousMigrationVersion state property are all different', async () => {
const appMetadataController = new AppMetadataController({
state: {
currentMigrationVersion: 2,
previousMigrationVersion: 1,
it('updates the currentMigrationVersion and previousMigrationVersion state properties if the currentMigrationVersion param, the currentMigrationVersion state property and the previousMigrationVersion state property are all different', () => {
withController(
{
state: {
currentMigrationVersion: 2,
previousMigrationVersion: 1,
},
currentMigrationVersion: 3,
},
currentMigrationVersion: 3,
});
expect(appMetadataController.store.getState()).toStrictEqual({
...EXPECTED_DEFAULT_STATE,
currentMigrationVersion: 3,
previousMigrationVersion: 2,
});
({ controller }) => {
expect(controller.state).toStrictEqual({
...getDefaultAppMetadataControllerState(),
currentMigrationVersion: 3,
previousMigrationVersion: 2,
});
},
);
});
});
});

type WithControllerOptions = Partial<AppMetadataControllerOptions>;

type WithControllerCallback<ReturnValue> = ({
controller,
}: {
controller: AppMetadataController;
}) => ReturnValue;

type WithControllerArgs<ReturnValue> =
| [WithControllerCallback<ReturnValue>]
| [WithControllerOptions, WithControllerCallback<ReturnValue>];

function withController<ReturnValue>(
...args: WithControllerArgs<ReturnValue>
): ReturnValue {
const [options = {}, fn] = args.length === 2 ? args : [{}, args[0]];

const controllerMessenger = new ControllerMessenger<never, never>();

const messenger = controllerMessenger.getRestricted({
name: 'AppMetadataController',
allowedActions: [],
allowedEvents: [],
});

return fn({
controller: new AppMetadataController({
messenger,
...options,
}),
});
}
Loading

0 comments on commit 208245a

Please sign in to comment.