Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate to MV3 #436

Merged
merged 3 commits into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 7 additions & 36 deletions __tests__/options.test.js
Original file line number Diff line number Diff line change
@@ -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') {
Expand All @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion coverage/badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 4 additions & 5 deletions dist/manifest.json
Original file line number Diff line number Diff line change
@@ -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"],
Expand All @@ -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"
Expand Down
40 changes: 5 additions & 35 deletions src/extension-contexts/options.ts
Original file line number Diff line number Diff line change
@@ -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, () => {
Expand All @@ -14,33 +7,10 @@ export const setOptions = (options: OptionsBool): void => {
};

export const getOptions = (): Promise<OptionsBool> => {
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 });
});
});
};