Skip to content

Commit

Permalink
added reset factory default to developer settings
Browse files Browse the repository at this point in the history
  • Loading branch information
eric2788 committed Oct 24, 2024
1 parent 6b992a0 commit c394541
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 13 deletions.
39 changes: 29 additions & 10 deletions src/options/fragments/developer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { toast } from 'sonner/dist';
import type { ExposeHandler, StateProxy } from "~hooks/binding";
import type { Leaves } from "~types/common";
import { sendMessager } from '~utils/messaging';
import { setSettingStorage } from '~utils/storage';
import { removeSettingStorage, setSettingStorage } from '~utils/storage';

export type SettingSchema = {
elements: {
Expand Down Expand Up @@ -233,22 +233,41 @@ function DeveloperSettings({ state, useHandler }: StateProxy<SettingSchema>): JS
success: '已成功获取最新版本,请重新加载网页。',
error: (err) => '获取最新版本失败: ' + err.message
})
await fetching
}

const resetDefault = async () => {
if (!window.confirm('这将覆盖开发者相关至插件默认设定。')) return
const removing = removeSettingStorage('settings.developer')
toast.promise(removing, {
loading: '正在重置开发者相关设定...',
success: '已成功重置至默认设定,请重新加载网页。',
error: (err) => '重置设定失败: ' + err.message
})
}

return (
<div className="col-span-2 container grid grid-cols-1 gap-5 w-full">
<Alert
className="bg-[#f8d7da] text-[#721c24]"
className="bg-[#f8d7da] text-[#721c24] items-center"
icon={alertIcon}
action={
<Button
onClick={fetchDeveloper}
size="sm"
className="!absolute top-3 right-3 text-white bg-red-500"
>
获取最新版本
</Button>}
<div className="flex gap-1 flex-grow justify-end">
<Button
onClick={fetchDeveloper}
size="sm"
className=" text-white bg-red-500"
>
获取最新版本
</Button>
<Button
onClick={resetDefault}
size="sm"
className=" text-white bg-green-500"
>
重置设定
</Button>
</div>
}
>
若你本身并不熟悉网页开发,请尽量别碰这里的设定
</Alert>
Expand Down
10 changes: 10 additions & 0 deletions src/utils/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ export async function getFullSettingStroage(): Promise<Settings> {
return Object.assign({}, ...settings)
}

/**
* Removes a setting from the storage.
*
* @param key - The key of the setting to be removed. It should be one of the keys of `SettingFragments`.
* @returns A promise that resolves when the setting is removed.
*/
export async function removeSettingStorage(key: keyof SettingFragments): Promise<void> {
return storage.remove(key)
}

/**
* Executes a callback function while setting a processing flag in the session storage.
* @param callback - The callback function to be executed.
Expand Down
44 changes: 41 additions & 3 deletions tests/pages/options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,12 @@ test('測試從遠端獲取開發者設定', async ({ page }) => {
await page.getByText('开发者相关').click()

const inputUpperButtonArea = page.getByTestId('elements.upperButtonArea')
await expect(inputUpperButtonArea).toHaveValue('.lower-row .left-ctnr')
await inputUpperButtonArea.fill('inputUpperButtonArea changed')

const liveTitle = page.getByTestId('elements.liveTitle')
await expect(liveTitle).toHaveValue('.live-skin-main-text.small-title')
await liveTitle.fill('liveTitle changed')

const liveFullScreenClass = page.getByTestId('classes.screenFull')
await expect(liveFullScreenClass).toHaveValue('fullscreen-fix')
await liveFullScreenClass.fill('liveFullScreenClass changed')

await page.getByText('保存设定').click()
Expand Down Expand Up @@ -289,6 +286,47 @@ test('測試從遠端獲取開發者設定', async ({ page }) => {
expect(storageStr).toBe(JSON.stringify(remote))
})

test('測試恢復開發者設定至默認值', async ({ page }) => {

logger.info('正在修改开发者相关....')

await page.getByText('开发者相关').click()

const inputUpperButtonArea = page.getByTestId('elements.upperButtonArea')
await expect(inputUpperButtonArea).toHaveValue('.lower-row .left-ctnr')
await inputUpperButtonArea.fill('inputUpperButtonArea changed')

const liveTitle = page.getByTestId('elements.liveTitle')
await expect(liveTitle).toHaveValue('.live-skin-main-text.small-title')
await liveTitle.fill('liveTitle changed')

const liveFullScreenClass = page.getByTestId('classes.screenFull')
await expect(liveFullScreenClass).toHaveValue('fullscreen-fix')
await liveFullScreenClass.fill('liveFullScreenClass changed')

await page.getByText('保存设定').click()
await page.reload({ waitUntil: 'domcontentloaded' })

logger.info('正在验证开发者相关....')
await page.getByText('开发者相关').click()
await expect(inputUpperButtonArea).toHaveValue('inputUpperButtonArea changed')
await expect(liveTitle).toHaveValue('liveTitle changed')
await expect(liveFullScreenClass).toHaveValue('liveFullScreenClass changed')

logger.info('正在恢复开发者相关至默认值....')
page.once('dialog', dialog => dialog.accept())
await page.getByText('重置设定').click()

await page.getByText('已成功重置至默认设定').waitFor({ state: 'visible' })
await page.reload({ waitUntil: 'domcontentloaded' })

logger.info('正在验证开发者相关有否被重置....')
await page.getByText('开发者相关').click()
await expect(inputUpperButtonArea).not.toHaveValue('inputUpperButtonArea changed')
await expect(liveTitle).not.toHaveValue('liveTitle changed')
await expect(liveFullScreenClass).not.toHaveValue('liveFullScreenClass changed')
})

test('測試設定數據從MV2遷移', async ({ serviceWorker, page }) => {

logger.info('正在測試寫入 MV2 設定....')
Expand Down

0 comments on commit c394541

Please sign in to comment.