1. jest.setup.js: - 添加 Request/Response/Headers 全局对象 mock - 解决 'Request is not defined' 错误 2. .eslintrc.json: - 将 jest.setup.js 添加到忽略列表 3. shared-mocks.tsx: - 添加 ArrowUp 图标 mock 4. back-to-top.test.tsx: - 重写测试使用 import 语法 - 使用 fireEvent.scroll 触发滚动事件 - 修复组件渲染测试
This commit is contained in:
@@ -4,6 +4,51 @@ const { TextEncoder, TextDecoder } = require('util');
|
||||
global.TextEncoder = TextEncoder;
|
||||
global.TextDecoder = TextDecoder;
|
||||
|
||||
if (typeof global.Request === 'undefined') {
|
||||
global.Request = class Request {
|
||||
constructor(input, init = {}) {
|
||||
this.url = typeof input === 'string' ? input : input.url;
|
||||
this.method = init.method || 'GET';
|
||||
this.headers = new Map(Object.entries(init.headers || {}));
|
||||
this.body = init.body;
|
||||
}
|
||||
async json() {
|
||||
return typeof this.body === 'string' ? JSON.parse(this.body) : this.body;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
if (typeof global.Response === 'undefined') {
|
||||
global.Response = class Response {
|
||||
constructor(body, init = {}) {
|
||||
this.body = body;
|
||||
this.status = init.status || 200;
|
||||
this.statusText = init.statusText || 'OK';
|
||||
this.headers = new Map(Object.entries(init.headers || {}));
|
||||
}
|
||||
async json() {
|
||||
return typeof this.body === 'string' ? JSON.parse(this.body) : this.body;
|
||||
}
|
||||
async text() {
|
||||
return typeof this.body === 'string' ? this.body : JSON.stringify(this.body);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
if (typeof global.Headers === 'undefined') {
|
||||
global.Headers = class Headers {
|
||||
constructor(init = {}) {
|
||||
this._headers = new Map(Object.entries(init));
|
||||
}
|
||||
get(name) {
|
||||
return this._headers.get(name);
|
||||
}
|
||||
set(name, value) {
|
||||
this._headers.set(name, value);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const { setupAllMocks } = require('./src/__mocks__/shared-mocks');
|
||||
|
||||
setupAllMocks();
|
||||
|
||||
Reference in New Issue
Block a user