From 6aee9930cad0ec4079e52ddff8060e266fbfe8f5 Mon Sep 17 00:00:00 2001 From: Ankit Ahuja Date: Sun, 26 Jul 2020 02:15:15 -0700 Subject: [PATCH] fix edge case in migration; update manifest to version 3 --- .../__tests__/migrate-old-format.test.js | 22 ++++++++++++++++- src/background/migrate/migrate-old-format.ts | 24 +++++++++---------- .../header/TheDeleteStyleDialog.vue | 1 + src/extension/manifest.json | 4 ++-- 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/background/migrate/__tests__/migrate-old-format.test.js b/src/background/migrate/__tests__/migrate-old-format.test.js index 93c7cc73..05ff7405 100644 --- a/src/background/migrate/__tests__/migrate-old-format.test.js +++ b/src/background/migrate/__tests__/migrate-old-format.test.js @@ -31,7 +31,7 @@ describe('MigrateOldFormat', () => { expect(mockSet).toHaveBeenCalledTimes(0); }); - it('returns styles as is when they are already in the new format', async () => { + it('returns styles as is when they are in the new format', async () => { const mockSet = jest.fn((_, callback) => callback()); const mockGet = (_, callback) => { callback({ styles: newFormat }); @@ -43,6 +43,26 @@ describe('MigrateOldFormat', () => { expect(mockSet).toHaveBeenCalledTimes(0); }); + it('returns styles as is when they are in the new format (with empty css)', async () => { + const newFormatWithEmptyCss = { + 'www.google.com': { + css: '', + enabled: false, + readability: false, + }, + }; + + const mockSet = jest.fn((_, callback) => callback()); + const mockGet = (_, callback) => { + callback({ styles: newFormatWithEmptyCss }); + }; + mockChromeAPI(mockGet, mockSet); + + const styles = await getMigratedStyles(); + expect(styles).toEqual(newFormatWithEmptyCss); + expect(mockSet).toHaveBeenCalledTimes(0); + }); + it('backs up v2 styles to "backup_v2_styles"', async () => { const mockSet = jest.fn((_, callback) => callback()); const mockGet = (_, callback) => { diff --git a/src/background/migrate/migrate-old-format.ts b/src/background/migrate/migrate-old-format.ts index f6aae72f..7b1b116b 100644 --- a/src/background/migrate/migrate-old-format.ts +++ b/src/background/migrate/migrate-old-format.ts @@ -5,7 +5,7 @@ import { Style, StyleMap, StylebotOptions } from '@stylebot/types'; /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ const isNewFormat = (styles: any) => { const urls: Array = Object.keys(styles); - return !!urls.find(url => !!styles[url].css); + return !!urls.find(url => styles[url].css !== undefined); }; /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ @@ -20,28 +20,28 @@ const backupV2Styles = (styles: any) => { export const getMigratedStyles = (): Promise => { return new Promise(resolve => { chrome.storage.local.get('styles', async items => { - const oldStyles = items['styles']; + const styles = items['styles']; - if (!oldStyles) { + if (!styles) { resolve({}); return; } - // test to ensure styles are not already in the new format - if (isNewFormat(oldStyles)) { - resolve(oldStyles); + // check if styles are in the new format + if (isNewFormat(styles)) { + resolve(styles); return; } // backup old styles, in case we run into a bug - await backupV2Styles(oldStyles); + await backupV2Styles(styles); const formatter = new LegacyCssFormatter(); - const urls: Array = Object.keys(oldStyles); + const urls: Array = Object.keys(styles); const results: Array> = urls.map( async (url): Promise