Skip to content

Commit

Permalink
Merge pull request #27795 from software-mansion-labs/ts-migration/Mov…
Browse files Browse the repository at this point in the history
…eToIndexedDB

[No QA][TS migration] Migrate 'MoveToIndexedDB.js' lib to TypeScript
  • Loading branch information
bondydaa authored Sep 21, 2023
2 parents ee023d8 + d79af66 commit bb32cc5
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 153 deletions.
11 changes: 1 addition & 10 deletions src/libs/migrateOnyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import _ from 'underscore';
import Log from './Log';
import AddEncryptedAuthToken from './migrations/AddEncryptedAuthToken';
import RenamePriorityModeKey from './migrations/RenamePriorityModeKey';
import MoveToIndexedDB from './migrations/MoveToIndexedDB';
import RenameExpensifyNewsStatus from './migrations/RenameExpensifyNewsStatus';
import AddLastVisibleActionCreated from './migrations/AddLastVisibleActionCreated';
import PersonalDetailsByAccountID from './migrations/PersonalDetailsByAccountID';
Expand All @@ -14,15 +13,7 @@ export default function () {

return new Promise((resolve) => {
// Add all migrations to an array so they are executed in order
const migrationPromises = [
MoveToIndexedDB,
RenamePriorityModeKey,
AddEncryptedAuthToken,
RenameExpensifyNewsStatus,
AddLastVisibleActionCreated,
PersonalDetailsByAccountID,
RenameReceiptFilename,
];
const migrationPromises = [RenamePriorityModeKey, AddEncryptedAuthToken, RenameExpensifyNewsStatus, AddLastVisibleActionCreated, PersonalDetailsByAccountID, RenameReceiptFilename];

// Reduce all promises down to a single promise. All promises run in a linear fashion, waiting for the
// previous promise to finish before moving onto the next one.
Expand Down
75 changes: 0 additions & 75 deletions src/libs/migrations/MoveToIndexedDB.js

This file was deleted.

68 changes: 0 additions & 68 deletions tests/unit/MigrationTest.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import Onyx from 'react-native-onyx';
import _ from 'underscore';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
import CONST from '../../src/CONST';
import Log from '../../src/libs/Log';
import getPlatform from '../../src/libs/getPlatform';
import AddLastVisibleActionCreated from '../../src/libs/migrations/AddLastVisibleActionCreated';
import MoveToIndexedDB from '../../src/libs/migrations/MoveToIndexedDB';
import PersonalDetailsByAccountID from '../../src/libs/migrations/PersonalDetailsByAccountID';
import CheckForPreviousReportActionID from '../../src/libs/migrations/CheckForPreviousReportActionID';
import ONYXKEYS from '../../src/ONYXKEYS';
Expand All @@ -28,71 +25,6 @@ describe('Migrations', () => {
return waitForBatchedUpdates();
});

describe('MoveToIndexedDb', () => {
let mockMultiSet;
beforeEach(() => {
getPlatform.mockImplementation(() => CONST.PLATFORM.WEB);
mockMultiSet = jest.spyOn(Onyx, 'multiSet').mockImplementation(() => Promise.resolve());
localStorage.clear();
});

afterAll(() => {
mockMultiSet.mockRestore(Onyx, 'multiSet');
localStorage.clear();
});

it('Should do nothing for non web/desktop platforms', () => {
// Given the migration is not running on web or desktop
getPlatform.mockImplementation(() => CONST.PLATFORM.ANDROID);

// When the migration runs
return MoveToIndexedDB().then(() => {
// Then we don't expect any storage calls
expect(Onyx.multiSet).not.toHaveBeenCalled();
});
});

it('Should do nothing when there is no old session data', () => {
// Given no session in old storage medium (localStorage)
localStorage.removeItem(ONYXKEYS.SESSION);

// When the migration runs
return MoveToIndexedDB().then(() => {
// Then we don't expect any storage calls
expect(Onyx.multiSet).not.toHaveBeenCalled();
});
});

it('Should migrate Onyx keys in localStorage to (new) Onyx', () => {
// Given some old data exists in storage
const data = {
[ONYXKEYS.SESSION]: {authToken: 'mock-token', loading: false},
[ONYXKEYS.ACCOUNT]: {email: 'test@mock.com'},
[ONYXKEYS.NETWORK]: {isOffline: true},
};

_.forEach(data, (value, key) => localStorage.setItem(key, JSON.stringify(value)));

// When the migration runs
return MoveToIndexedDB().then(() => {
// Then multiset should be called with all the data available in localStorage
expect(Onyx.multiSet).toHaveBeenCalledWith(data);
});
});

it('Should not clear non Onyx keys from localStorage', () => {
// Given some Onyx and non-Onyx data exists in localStorage
localStorage.setItem(ONYXKEYS.SESSION, JSON.stringify({authToken: 'mock-token'}));
localStorage.setItem('non-onyx-item', 'MOCK');

// When the migration runs
return MoveToIndexedDB().then(() => {
// Then non-Onyx data should remain in localStorage
expect(localStorage.getItem('non-onyx-item')).toEqual('MOCK');
});
});
});

describe('AddLastVisibleActionCreated', () => {
it('Should add lastVisibleActionCreated wherever lastActionCreated currently is', () =>
Onyx.multiSet({
Expand Down

0 comments on commit bb32cc5

Please sign in to comment.