Skip to content

Commit

Permalink
Merge branch 'develop' into mikesposito/update-ledger
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesposito authored Nov 12, 2024
2 parents fd893e5 + 68ba635 commit ac579f8
Show file tree
Hide file tree
Showing 31 changed files with 2,860 additions and 2,224 deletions.
1,968 changes: 1,968 additions & 0 deletions app/scripts/controllers/metametrics-controller.test.ts

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ControllerMessenger } from '@metamask/base-controller';
import {
AllowedActions,
MetaMetricsDataDeletionController,
type MetaMetricsDataDeletionControllerMessengerActions,
} from './metametrics-data-deletion';
Expand All @@ -10,8 +11,8 @@ describe('MetaMetricsDataDeletionController', () => {
const mockMetaMetricsId = 'mockId';
const mockTaskId = 'mockTaskId';
const { controller, dataDeletionService } = setupController({
metaMetricsId: mockMetaMetricsId,
options: {
getMetaMetricsId: jest.fn().mockReturnValue(mockMetaMetricsId),
dataDeletionService: {
createDataDeletionRegulationTask: jest
.fn()
Expand Down Expand Up @@ -43,8 +44,8 @@ describe('MetaMetricsDataDeletionController', () => {
const mockMetaMetricsId = 'mockId';
const mockTaskId = 'mockTaskId';
const { controller, dataDeletionService } = setupController({
metaMetricsId: mockMetaMetricsId,
options: {
getMetaMetricsId: jest.fn().mockReturnValue(mockMetaMetricsId),
dataDeletionService: {
createDataDeletionRegulationTask: jest
.fn()
Expand Down Expand Up @@ -76,9 +77,7 @@ describe('MetaMetricsDataDeletionController', () => {

it('fails to creates a data deletion task when user has never participating in metrics tracking', async () => {
const { controller } = setupController({
options: {
getMetaMetricsId: jest.fn().mockReturnValue(null),
},
metaMetricsId: null,
});
await expect(
controller.createMetaMetricsDataDeletionTask(),
Expand All @@ -94,8 +93,8 @@ describe('MetaMetricsDataDeletionController', () => {
const mockMetaMetricsId = 'mockId';
const mockTaskId = 'mockTaskId';
const { controller, dataDeletionService } = setupController({
metaMetricsId: mockMetaMetricsId,
options: {
getMetaMetricsId: jest.fn().mockReturnValue(mockMetaMetricsId),
dataDeletionService: {
createDataDeletionRegulationTask: jest
.fn()
Expand Down Expand Up @@ -128,28 +127,35 @@ describe('MetaMetricsDataDeletionController', () => {
*
* @param options - Setup options.
* @param options.options - Controller constructor options.
* @param options.metaMetricsId - The MetaMetrics ID to use.
* @returns The test controller, a messenger instance, and related mocks.
*/
function setupController({
options,
metaMetricsId,
}: {
options?: Partial<
ConstructorParameters<typeof MetaMetricsDataDeletionController>[0]
>;
metaMetricsId?: string | null;
} = {}): {
controller: MetaMetricsDataDeletionController;
dataDeletionService: ConstructorParameters<
typeof MetaMetricsDataDeletionController
>[0]['dataDeletionService'];
messenger: ControllerMessenger<
MetaMetricsDataDeletionControllerMessengerActions,
MetaMetricsDataDeletionControllerMessengerActions | AllowedActions,
never
>;
} {
const messenger = new ControllerMessenger<
MetaMetricsDataDeletionControllerMessengerActions,
const controllerMessenger = new ControllerMessenger<
MetaMetricsDataDeletionControllerMessengerActions | AllowedActions,
never
>();
controllerMessenger.registerActionHandler(
'MetaMetricsController:getState',
jest.fn().mockReturnValue({ metaMetricsId }),
);
const mockCreateDataDeletionRegulationTaskResponse = 'mockRegulateId';
const mockFetchDeletionRegulationStatusResponse = 'UNKNOWN';
const mockDataDeletionService = {
Expand All @@ -164,9 +170,9 @@ function setupController({
const constructorOptions = {
dataDeletionService: mockDataDeletionService,
getMetaMetricsId: jest.fn().mockReturnValue('mockMetaMetricsId'),
messenger: messenger.getRestricted({
messenger: controllerMessenger.getRestricted({
name: 'MetaMetricsDataDeletionController',
allowedActions: [],
allowedActions: ['MetaMetricsController:getState'],
allowedEvents: [],
}),
...options,
Expand All @@ -176,6 +182,6 @@ function setupController({
return {
controller,
dataDeletionService: constructorOptions.dataDeletionService,
messenger,
messenger: controllerMessenger,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
import { PublicInterface } from '@metamask/utils';
import type { DataDeletionService } from '../../services/data-deletion-service';
import { DeleteRegulationStatus } from '../../../../shared/constants/metametrics';
import { MetaMetricsControllerGetStateAction } from '../metametrics-controller';

// Unique name for the controller
const controllerName = 'MetaMetricsDataDeletionController';
Expand Down Expand Up @@ -70,14 +71,24 @@ export type MetaMetricsDataDeletionControllerMessengerActions =
| CreateMetaMetricsDataDeletionTaskAction
| UpdateDataDeletionTaskStatusAction;

/**
* Actions that this controller is allowed to call.
*/
export type AllowedActions = MetaMetricsControllerGetStateAction;

/**
* Events that this controller is allowed to subscribe.
*/
export type AllowedEvents = never;

// Type for the messenger of MetaMetricsDataDeletionController
export type MetaMetricsDataDeletionControllerMessenger =
RestrictedControllerMessenger<
typeof controllerName,
MetaMetricsDataDeletionControllerMessengerActions,
never,
never,
never
MetaMetricsDataDeletionControllerMessengerActions | AllowedActions,
AllowedEvents,
AllowedActions['type'],
AllowedEvents['type']
>;

/**
Expand All @@ -91,27 +102,22 @@ export class MetaMetricsDataDeletionController extends BaseController<
> {
#dataDeletionService: PublicInterface<DataDeletionService>;

#getMetaMetricsId: () => string | null;

/**
* Creates a MetaMetricsDataDeletionController instance.
*
* @param args - The arguments to this function.
* @param args.dataDeletionService - The service used for deleting data.
* @param args.messenger - Messenger used to communicate with BaseV2 controller.
* @param args.state - Initial state to set on this controller.
* @param args.getMetaMetricsId - A function that returns the current MetaMetrics ID.
*/
constructor({
dataDeletionService,
messenger,
state,
getMetaMetricsId,
}: {
dataDeletionService: PublicInterface<DataDeletionService>;
messenger: MetaMetricsDataDeletionControllerMessenger;
state?: Partial<MetaMetricsDataDeletionState>;
getMetaMetricsId: () => string | null;
}) {
// Call the constructor of BaseControllerV2
super({
Expand All @@ -120,7 +126,6 @@ export class MetaMetricsDataDeletionController extends BaseController<
name: controllerName,
state: { ...getDefaultState(), ...state },
});
this.#getMetaMetricsId = getMetaMetricsId;
this.#dataDeletionService = dataDeletionService;
this.#registerMessageHandlers();
}
Expand All @@ -146,7 +151,9 @@ export class MetaMetricsDataDeletionController extends BaseController<
*
*/
async createMetaMetricsDataDeletionTask(): Promise<void> {
const metaMetricsId = this.#getMetaMetricsId();
const { metaMetricsId } = this.messagingSystem.call(
'MetaMetricsController:getState',
);
if (!metaMetricsId) {
throw new Error('MetaMetrics ID not found');
}
Expand Down
Loading

0 comments on commit ac579f8

Please sign in to comment.