react-native-fetch-mock 项目简介
在使用 React Native 的过程中,没有找到一款合适自己的 fetch-mock 组件。因此,决定自己发布一个 react-native-fetch-mock module。Roadmap1. 结合Mock.js(已实现)2. 配合线上rap系统Usage__ mocks__/index.jsexport default {
'/api/path': (options) => {
const all = Mock.mock({
'list|2': [{
'id|+1': 1,
'name': '@first @last',
'age|18-54': 1,
}]
}).list;
return Promise.resolve({
data: all,
});
}
}index.jsimport FetchMock from 'react-native-fetch-mock';
if (__dev__) {
global.fetch = new FetchMock(require('path/to/mocks/directory')).fetch;
}
// if __dev__ is true, it will back the data you defined in mock directory
fetch('/api/path', options);
'/api/path': (options) => {
const all = Mock.mock({
'list|2': [{
'id|+1': 1,
'name': '@first @last',
'age|18-54': 1,
}]
}).list;
return Promise.resolve({
data: all,
});
}
}index.jsimport FetchMock from 'react-native-fetch-mock';
if (__dev__) {
global.fetch = new FetchMock(require('path/to/mocks/directory')).fetch;
}
// if __dev__ is true, it will back the data you defined in mock directory
fetch('/api/path', options);