mock browser dep in vitest
This commit is contained in:
@@ -12,12 +12,21 @@ import {
|
||||
within,
|
||||
} from '@testing-library/react';
|
||||
import React from 'react';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const nativeClipboardMock = vi.hoisted(() => ({
|
||||
text: '',
|
||||
}));
|
||||
|
||||
const originalCreateObjectUrl = Object.getOwnPropertyDescriptor(
|
||||
URL,
|
||||
'createObjectURL',
|
||||
);
|
||||
const originalRevokeObjectUrl = Object.getOwnPropertyDescriptor(
|
||||
URL,
|
||||
'revokeObjectURL',
|
||||
);
|
||||
|
||||
vi.mock('@tauri-apps/plugin-clipboard-manager', () => ({
|
||||
readImage: vi.fn(async () => {
|
||||
throw new Error('no native clipboard image');
|
||||
@@ -102,6 +111,19 @@ function mockRoleAgentReply() {
|
||||
return roleAgentMockReply;
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
Object.defineProperties(URL, {
|
||||
createObjectURL: {
|
||||
configurable: true,
|
||||
value: vi.fn(() => 'blob:mock-attachment-preview'),
|
||||
},
|
||||
revokeObjectURL: {
|
||||
configurable: true,
|
||||
value: vi.fn(),
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
nativeClipboardMock.text = '';
|
||||
@@ -109,6 +131,17 @@ afterEach(() => {
|
||||
window.localStorage?.clear();
|
||||
delete window.__TAURI__;
|
||||
vi.restoreAllMocks();
|
||||
|
||||
if (originalCreateObjectUrl) {
|
||||
Object.defineProperty(URL, 'createObjectURL', originalCreateObjectUrl);
|
||||
} else {
|
||||
Reflect.deleteProperty(URL, 'createObjectURL');
|
||||
}
|
||||
if (originalRevokeObjectUrl) {
|
||||
Object.defineProperty(URL, 'revokeObjectURL', originalRevokeObjectUrl);
|
||||
} else {
|
||||
Reflect.deleteProperty(URL, 'revokeObjectURL');
|
||||
}
|
||||
});
|
||||
|
||||
describe('AI 游戏创作 App 界面边界', () => {
|
||||
@@ -1617,6 +1650,9 @@ describe('AI 游戏创作 App 界面边界', () => {
|
||||
});
|
||||
|
||||
expect(await screen.findByText('reference.png')).not.toBeNull();
|
||||
expect(
|
||||
screen.getByRole('img', { name: 'reference.png' }).getAttribute('src'),
|
||||
).toBe('blob:mock-attachment-preview');
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: '开启创作' }));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user