6bac88fbc4
- Detect and block chrome://, chrome-extension://, about://, edge://, brave:// pages - Show 'Unsupported' badge with warning icon and disabled button - Add 'unsupported' and 'unsupportedDesc' i18n keys (zh/en) - Update useRightClickRestorer hook with isUnsupported state - Add unit tests for unsupported page behavior - Update page UI to render conditional unsupported layout
93 lines
3.1 KiB
TypeScript
93 lines
3.1 KiB
TypeScript
import { renderHook, act, waitFor } from '@testing-library/react';
|
|
import { useRightClickRestorer } from '../useRightClickRestorer';
|
|
import { sendMessageToContent } from '@/utils/messages';
|
|
|
|
const mockTabsQuery = vi.fn();
|
|
|
|
vi.mock('@/utils/messages', () => ({
|
|
MessageAction: {
|
|
RESTORE_RIGHT_CLICK: 'restoreRightClick',
|
|
QUERY_RIGHT_CLICK_STATUS: 'queryRightClickStatus',
|
|
},
|
|
sendMessageToContent: vi.fn(),
|
|
}));
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mockTabsQuery.mockResolvedValue([{ url: 'https://example.com/path' }]);
|
|
chrome.tabs.query = mockTabsQuery;
|
|
vi.mocked(sendMessageToContent).mockResolvedValue({ success: true, restored: false });
|
|
});
|
|
|
|
describe('useRightClickRestorer', () => {
|
|
it('should load domain and query status', async () => {
|
|
const { result } = renderHook(() => useRightClickRestorer());
|
|
|
|
await waitFor(() => expect(result.current.isLoading).toBe(false));
|
|
|
|
expect(result.current.domain).toBe('example.com');
|
|
expect(result.current.isUnlocked).toBe(false);
|
|
expect(result.current.isUnsupported).toBe(false);
|
|
expect(sendMessageToContent).toHaveBeenCalledWith('queryRightClickStatus');
|
|
});
|
|
|
|
it('should mark internal pages as unsupported', async () => {
|
|
mockTabsQuery.mockResolvedValue([{ url: 'chrome://newtab/' }]);
|
|
chrome.tabs.query = mockTabsQuery;
|
|
|
|
const { result } = renderHook(() => useRightClickRestorer());
|
|
|
|
await waitFor(() => expect(result.current.isLoading).toBe(false));
|
|
|
|
expect(result.current.isUnsupported).toBe(true);
|
|
expect(result.current.isUnlocked).toBe(false);
|
|
expect(sendMessageToContent).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should unlock right click', async () => {
|
|
vi.mocked(sendMessageToContent).mockResolvedValueOnce({ success: true, restored: false });
|
|
vi.mocked(sendMessageToContent).mockResolvedValueOnce({ success: true, restored: true });
|
|
|
|
const { result } = renderHook(() => useRightClickRestorer());
|
|
|
|
await waitFor(() => expect(result.current.isLoading).toBe(false));
|
|
|
|
await act(async () => {
|
|
await result.current.unlock();
|
|
});
|
|
|
|
expect(result.current.isUnlocked).toBe(true);
|
|
expect(sendMessageToContent).toHaveBeenLastCalledWith('restoreRightClick');
|
|
});
|
|
|
|
it('should not unlock unsupported pages', async () => {
|
|
mockTabsQuery.mockResolvedValue([{ url: 'chrome://settings/' }]);
|
|
chrome.tabs.query = mockTabsQuery;
|
|
|
|
const { result } = renderHook(() => useRightClickRestorer());
|
|
|
|
await waitFor(() => expect(result.current.isLoading).toBe(false));
|
|
|
|
await act(async () => {
|
|
await result.current.unlock();
|
|
});
|
|
|
|
expect(result.current.isUnlocked).toBe(false);
|
|
expect(sendMessageToContent).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should handle sendMessage failure gracefully', async () => {
|
|
vi.mocked(sendMessageToContent).mockRejectedValue(new Error('Connection failed'));
|
|
|
|
const { result } = renderHook(() => useRightClickRestorer());
|
|
|
|
await waitFor(() => expect(result.current.isLoading).toBe(false));
|
|
|
|
await act(async () => {
|
|
await result.current.unlock();
|
|
});
|
|
|
|
expect(result.current.isUnlocked).toBe(false);
|
|
});
|
|
});
|