我正在尝试使用 Jest & Jest 模型为我的 ES6 应用程序编写测试用例。但是测试套件根本没有选择模型。有人可以让我知道正确的测试方法吗
请求.js
class Request{
//
// Set Header for All the requests
//
static get HEADERS() {
return {
"Accept": "application/json, text/plain",
"Content-Type": "application/json"
};
}
//
// GET Request
//
static get(url){
return fetch(url)
.then(response => {
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
})
.catch(err => {
console.log(err);
});
}
}
request.js //开玩笑样机
import configureMockStore from 'redux-mock-store' // mock store
import thunk from 'redux-thunk'
const middlewares = [ thunk ]
const mockStore = configureMockStore(middlewares)
const tasks = {
"1": { "id": '1', "text": "Read description of programming challenge" },
"2": { "id": "2", "text": "Implement awesome web app" },
"3": { "id": "3", "text": "Polish project" }
};
import Request from '../scripts/lib/request';
describe('Request', () => {
it('List all task fetch request', () => {
console.log("11111");
fetch.mockResponses(JSON.stringify(tasks));
const expectedActions = [{ type: 'SET_TASKS', tasks}];
const store = mockStore(tasks);
return store.dispatch(store.get())
.then(() => { // return of async actions
expect(store.getActions()).toEqual(expectedActions)
})
}
}
request.spec.js //单元测试
import Request from '../../scripts/lib/request';
describe('request', () => {
it('Should return all tasks', function() {
var allTasks = Request.get("api/tasks");
expect(allTasks).toEqual({
"1": { "id": '1', "text": "Read description of programming challenge" },
"2": { "id": "2", "text": "Implement awesome web app" },
"3": { "id": "3", "text": "Polish project" }
});
});
});