diff --git a/__tests__/options.test.js b/__tests__/options.test.js index 6f3320fa..81b18f05 100644 --- a/__tests__/options.test.js +++ b/__tests__/options.test.js @@ -1,19 +1,12 @@ import { chrome } from 'jest-chrome'; -import { - initialOptions, - initialOptionsBool, - isFalse, - MIGRATED_TO_CHROME_STORAGE, - SHOW_ON_TWEETDECK_TIMELINE, - SHOW_ON_TIMELINE, - SHOW_ON_TWEETDECK_TWEET_DETAIL, -} from '../src/constants'; +import { initialOptionsBool, SHOW_ON_TIMELINE, SHOW_ON_TWEETDECK_TWEET_DETAIL } from '../src/constants'; import { getOptions, setOptions } from '../src/extension-contexts/options'; let chromeStorage = {}; -chrome.storage.sync.set.mockImplementation((items) => { +chrome.storage.sync.set.mockImplementation((items, callback) => { chromeStorage = { ...chromeStorage, ...items }; + callback(); }); chrome.storage.sync.get.mockImplementation((keys, callback) => { if (typeof keys === 'string') { @@ -24,44 +17,22 @@ chrome.storage.sync.get.mockImplementation((keys, callback) => { }); beforeEach(() => { chromeStorage = {}; - localStorage.clear(); }); describe('options', () => { describe('getOptions', () => { - it('何もない状態で呼んだら, 初期値が返って, 初期値が保存されて, 移行済みになる', () => { + it('chrome.storageが空の状態で呼んだら, 初期値が返る (chrome.storageは空のまま)', () => { expect(getOptions()).resolves.toMatchObject(initialOptionsBool); - expect(chromeStorage).toMatchObject({ - ...initialOptionsBool, - [MIGRATED_TO_CHROME_STORAGE]: true, - }); - }); - it('未移行で, localStorageに設定があったら, localStorageの内容が移行されつつ返って, 移行済みになる', () => { - Object.entries({ ...initialOptions, [SHOW_ON_TWEETDECK_TIMELINE]: isFalse }).map(([k, v]) => { - localStorage.setItem(k, v); - }); - const expected = { ...initialOptionsBool, [SHOW_ON_TWEETDECK_TIMELINE]: false }; - expect(getOptions()).resolves.toMatchObject(expected); - expect(chromeStorage).toMatchObject({ - ...expected, - [MIGRATED_TO_CHROME_STORAGE]: true, - }); + expect(chromeStorage).toMatchObject({}); }); - it('移行済みなら, 保存された設定が返る', () => { - Object.entries({ ...initialOptions, [SHOW_ON_TWEETDECK_TIMELINE]: isFalse }).map(([k, v]) => { - localStorage.setItem(k, v); - }); + it('chrome.storageに保存された設定があるなら, それが返る', () => { chromeStorage = { ...initialOptionsBool, [SHOW_ON_TIMELINE]: false, - [MIGRATED_TO_CHROME_STORAGE]: true, }; const expected = { ...initialOptionsBool, [SHOW_ON_TIMELINE]: false }; expect(getOptions()).resolves.toMatchObject(expected); - expect(chromeStorage).toMatchObject({ - ...expected, - [MIGRATED_TO_CHROME_STORAGE]: true, - }); + expect(chromeStorage).toMatchObject(expected); }); }); describe('setOptions', () => { diff --git a/coverage/badge.svg b/coverage/badge.svg index fdcac574..561215b8 100644 --- a/coverage/badge.svg +++ b/coverage/badge.svg @@ -1 +1 @@ -Coverage: 82.58%Coverage82.58% \ No newline at end of file +Coverage: 82.76%Coverage82.76% \ No newline at end of file diff --git a/dist/manifest.json b/dist/manifest.json index d6b2c848..d08ba163 100644 --- a/dist/manifest.json +++ b/dist/manifest.json @@ -1,7 +1,7 @@ { - "manifest_version": 2, + "manifest_version": 3, "name": "twitter画像原寸ボタン", - "version": "4.2.0", + "version": "5.0.0", "description": "twitterの画像ツイートにボタンを追加する拡張機能。追加されたボタンを押すとツイートの画像を原寸で新しいタブに表示する。連絡先: @hogextend", "author": "hogashi", "permissions": ["tabs", "storage"], @@ -22,10 +22,9 @@ } ], "background": { - "scripts": ["js/background.bundle.js"], - "persistent": false + "service_worker": "js/background.bundle.js" }, - "browser_action": { + "action": { "default_icon": "icons/icon.png", "default_title": "twitter画像原寸ボタン", "default_popup": "html/popup.html" diff --git a/src/extension-contexts/options.ts b/src/extension-contexts/options.ts index 6a967228..446ef7a0 100644 --- a/src/extension-contexts/options.ts +++ b/src/extension-contexts/options.ts @@ -1,11 +1,4 @@ -import { - OptionsBool, - initialOptions, - initialOptionsBool, - OPTION_KEYS, - isTrue, - MIGRATED_TO_CHROME_STORAGE, -} from '../constants'; +import { OptionsBool, initialOptionsBool, OPTION_KEYS } from '../constants'; export const setOptions = (options: OptionsBool): void => { chrome.storage.sync.set(options, () => { @@ -14,33 +7,10 @@ export const setOptions = (options: OptionsBool): void => { }; export const getOptions = (): Promise => { - return new Promise((resolve) => { - // chrome.storageから取ってきつつ, - // まだ移行してないときはlocalStorageあるいは初期値を移行 - chrome.storage.sync.get(MIGRATED_TO_CHROME_STORAGE, (isMigrated) => { - if (!isMigrated[MIGRATED_TO_CHROME_STORAGE]) { - const newOptions = { ...initialOptions, ...localStorage }; - // 真偽値にして移行する - const newOptionsBool = { ...initialOptionsBool }; - OPTION_KEYS.forEach((key) => { - newOptionsBool[key] = newOptions[key] === isTrue; - }); - chrome.storage.sync.set( - { - ...newOptionsBool, - [MIGRATED_TO_CHROME_STORAGE]: true, - }, - () => { - // 移行できたら新しい値を返す - resolve(newOptionsBool); - }, - ); - } else { - chrome.storage.sync.get(OPTION_KEYS, (got) => { - // 初期値をフォールバックとしておく - resolve({ ...initialOptionsBool, ...got }); - }); - } + return new Promise(resolve => { + chrome.storage.sync.get(OPTION_KEYS, got => { + // 初期値をフォールバックとしておく + resolve({ ...initialOptionsBool, ...got }); }); }); };