1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-28 23:06:37 +01:00
metamask-extension/app/scripts/migrations/092.1.test.ts
Mark Stacey f033a59b17
Remove invalid tokensChainsCache state (#20495)
Migration #77 would set the `TokenListController.tokensChainsCache`
state to `undefined` if it wasn't already set to anything when that
migration was run. This is probably harmless except that it results
in Sentry errors during migrations, and it results in that property
having a value (at least temporarily) that doesn't match its type.

Migration #77 has been updated to prevent this property from being
set to `undefined` going forward. A new migration has been added to
delete this value for any users already affected by this problem. The
new migration was named "92.1" so that it could run after 92 but before
93, to make backporting this to v10.34.x easier (v10.34.x is currently
on migration 92). "92.1" is still a valid number so this should work
just as well as a whole number.
2023-08-17 09:04:30 -02:30

140 lines
3.3 KiB
TypeScript

import { cloneDeep } from 'lodash';
import { migrate, version } from './092.1';
const PREVIOUS_VERSION = 92;
const sentryCaptureExceptionMock = jest.fn();
global.sentry = {
startSession: jest.fn(),
endSession: jest.fn(),
toggleSession: jest.fn(),
captureException: sentryCaptureExceptionMock,
};
describe('migration #92.1', () => {
afterEach(() => {
jest.resetAllMocks();
});
it('should update the version metadata', async () => {
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: {},
};
const newStorage = await migrate(oldStorage);
expect(newStorage.meta).toStrictEqual({
version,
});
});
it('should return state unaltered if there is no TokenListController state', async () => {
const oldData = {
other: 'data',
};
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: oldData,
};
const newStorage = await migrate(cloneDeep(oldStorage));
expect(newStorage.data).toStrictEqual(oldData);
});
it('captures an exception if the TokenListController state is invalid', async () => {
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: { TokenListController: 'this is not valid' },
};
await migrate(oldStorage);
expect(sentryCaptureExceptionMock).toHaveBeenCalledTimes(1);
expect(sentryCaptureExceptionMock).toHaveBeenCalledWith(
new Error(`typeof state.TokenListController is string`),
);
});
it('should return state unaltered if there is no TokenListController tokensChainsCache state', async () => {
const oldData = {
other: 'data',
TokenListController: {
tokenList: {},
},
};
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: oldData,
};
const newStorage = await migrate(cloneDeep(oldStorage));
expect(newStorage.data).toStrictEqual(oldData);
});
it('should return state unaltered if the tokensChainsCache state is an unexpected type', async () => {
const oldData = {
other: 'data',
TokenListController: {
tokensChainsCache: 'unexpected string',
},
};
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: oldData,
};
const newStorage = await migrate(cloneDeep(oldStorage));
expect(newStorage.data).toStrictEqual(oldData);
});
it('should return state unaltered if the tokensChainsCache state is valid', async () => {
const oldData = {
other: 'data',
TokenListController: {
tokensChainsCache: {},
},
};
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: oldData,
};
const newStorage = await migrate(cloneDeep(oldStorage));
expect(newStorage.data).toStrictEqual(oldData);
});
it('should remove undefined tokensChainsCache state', async () => {
const oldData = {
other: 'data',
TokenListController: {
tokensChainsCache: undefined,
},
};
const oldStorage = {
meta: {
version: PREVIOUS_VERSION,
},
data: oldData,
};
const newStorage = await migrate(cloneDeep(oldStorage));
expect(newStorage.data).toStrictEqual({
other: 'data',
TokenListController: {},
});
});
});