Skip to content

Commit

Permalink
Merge pull request #1 from funkyLover/feat_keep_checked_status
Browse files Browse the repository at this point in the history
[feat]record checked status;
  • Loading branch information
funkyLover authored Mar 16, 2019
2 parents 14c1f2d + 0ee4651 commit 154fb56
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 25 deletions.
6 changes: 6 additions & 0 deletions example/with checked data/_checked.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
'mockChecked': {
'api.mock.com/api-bin/api.fcgi': 1
},
'setChecked': 1
};
1 change: 1 addition & 0 deletions lib/server/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ router
};
return;
}
id = id === '-1' ? null : id;
setStatus('setChecked', id);

ctx.body = {
Expand Down
49 changes: 49 additions & 0 deletions lib/server/status.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const path = require('path');
const fs = require('fs-extra');

let defaultStatus = {
dir: '',
server: null,
Expand All @@ -24,12 +27,58 @@ function getStatus() {
*/
function setStatus(key, val) {
status[key] = val;

if (key === 'dir') {
reproduceCheckedStatus(val);
}

if (key === 'mockChecked' || key === 'setChecked') {
recordChecked();
}
}

function resetStaus() {
status = { ...defaultStatus };
}

/**
* 设置勾选状态
* @param {String} dir - mock dir path
*/
function reproduceCheckedStatus(dir) {
const checkFile = path.resolve(dir, '_checked.js');
if (!fs.existsSync(checkFile)) {
return;
}

try {
const checked = require(checkFile);
status.mockChecked = checked.mockChecked || {};
status.setChecked = checked.setChecked || {};
} catch (e) {}
}

/**
* 记录勾选状态, 用于重启服务时恢复上次使用时的勾选状态
*/
function recordChecked() {
const { setChecked, mockChecked, dir } = getStatus();
const checkFile = path.resolve(dir, '_checked.js');
const content = `
/**
* please do not modify this file manual
* unless you really know what you are doing
*/
module.exports = {
mockChecked: ${JSON.stringify(mockChecked)},
setChecked: ${setChecked ? `'${setChecked}'` : 'null'}
}
`;
fs.outputFile(checkFile, content, err => {
err && console.log(err);
});
}

module.exports = {
getStatus,
setStatus,
Expand Down
63 changes: 38 additions & 25 deletions test/test.local.ip.request.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ const send = require('koa-send');
jest.mock('../lib/server/status');
jest.mock('koa-send');

const TEST_DIR = path.resolve(__dirname, './__test.create__');

afterAll(() => {
fs.removeSync(path.resolve(TEST_DIR, '_checked.js'));
fs.removeSync(path.resolve(TEST_DIR, 'api.mock.com'));
fs.removeSync(path.resolve(TEST_DIR, 'proxy.js'));
fs.removeSync(path.resolve(TEST_DIR, '_set'));
});

function setupEnv(port) {
const instance = axios.create({
baseURL: `http://127.0.0.1:${port}`,
Expand Down Expand Up @@ -103,14 +112,16 @@ test('it will code 0 when request "/$mock-check" with correct params', () => {
getStatus.mockImplementation(() => {
return require.requireActual('../lib/server/status').getStatus();
});
setStatus.mockImplementationOnce((key, val) => {
setStatus.mockImplementation((key, val) => {
return require.requireActual('../lib/server/status').setStatus(key, val);
});
setStatus('dir', TEST_DIR);

return instance.get('/$mock-check?id=api.mock.com&index=1').then(res => {
const status = getStatus();
expect(res.status).toBe(200);
expect(res.data.code).toBe('0');
expect(getStatus().mockChecked['api.mock.com']).toBe(1);
expect(status.mockChecked['api.mock.com']).toBe(1);
getStatus.mockReset();
server.close();
});
Expand All @@ -126,75 +137,77 @@ test('it will code -1 when request "/$set-check" with error params', () => {
});
});

test('it will code 0 when request "/$set-check" with correct params', () => {
test('it will return code 0 when request "/$set-check" with correct params', () => {
const { instance, server } = setupEnv(8897);

getStatus.mockImplementation(() => {
return require.requireActual('../lib/server/status').getStatus();
return {
...require.requireActual('../lib/server/status').getStatus(),
dir: TEST_DIR
};
});
setStatus.mockImplementationOnce((key, val) => {
setStatus.mockImplementation((key, val) => {
return require.requireActual('../lib/server/status').setStatus(key, val);
});
setStatus('dir', TEST_DIR);
const setId = 'a set of api';
return instance.get(`/$set-check?id=${setId}`).then(res => {

return instance.get(`/$set-check?id=${setId}`).then(async res => {
const status = getStatus();
const dir = status.dir;
const checkFile = path.resolve(dir, '_checked.js');
expect(res.status).toBe(200);
expect(res.data.code).toBe('0');
expect(getStatus().setChecked).toBe(setId);
expect(status.setChecked).toBe(setId);
expect(fs.existsSync(checkFile)).toBe(true);
getStatus.mockReset();
server.close();
});
});

test('it will create default mock template when request "/$create"', () => {
const { instance, server } = setupEnv(8897);
const dir = path.resolve(__dirname, './__test.create__');
const apiPath = path.join(dir, 'api.mock.com');
getStatus.mockImplementation(() => ({ dir, mock: {} }));
const { instance, server } = setupEnv(8898);
const apiPath = path.join(TEST_DIR, 'api.mock.com');
getStatus.mockImplementation(() => ({ dir: TEST_DIR, mock: {} }));

return instance.get('/$create').then(res => {
expect(res.status).toBe(200);
expect(res.data.code).toBe('0');
expect(fs.existsSync(apiPath)).toBe(true);
server.close();
fs.removeSync(apiPath);
});
});

test('it will create default mock template when request "/$create" with type proxy', () => {
const { instance, server } = setupEnv(8898);
const dir = path.resolve(__dirname, './__test.create__');
const proxyPath = path.join(dir, 'proxy.js');
getStatus.mockImplementation(() => ({ dir, mock: {} }));
const { instance, server } = setupEnv(8899);
const proxyPath = path.join(TEST_DIR, 'proxy.js');
getStatus.mockImplementation(() => ({ dir: TEST_DIR, mock: {} }));

return instance.get('/$create?type=proxy').then(res => {
expect(res.status).toBe(200);
expect(res.data.code).toBe('0');
expect(fs.existsSync(proxyPath)).toBe(true);
server.close();
fs.removeSync(proxyPath);
});
});

test('it will create default mock template when request "/$create" with type set', () => {
const { instance, server } = setupEnv(8899);
const dir = path.resolve(__dirname, './__test.create__');
const setPath = path.join(dir, '_set');
getStatus.mockImplementation(() => ({ dir, mock: {} }));
const { instance, server } = setupEnv(8900);
const setPath = path.join(TEST_DIR, '_set');
getStatus.mockImplementation(() => ({ dir: TEST_DIR, mock: {} }));

return instance.get('/$create?type=set').then(res => {
expect(res.status).toBe(200);
expect(res.data.code).toBe('0');
expect(fs.existsSync(setPath)).toBe(true);
server.close();
fs.removeSync(setPath);
});
});

test('it will return error when mock data is exist', () => {
const { instance, server } = setupEnv(8900);
const dir = path.resolve(__dirname, './__test.create__');
const { instance, server } = setupEnv(8901);
getStatus.mockImplementation(() => ({
dir,
dir: TEST_DIR,
mock: { _set: { notEmpty: true } }
}));

Expand Down
12 changes: 12 additions & 0 deletions test/test.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,15 @@ test('mock server will not be started when error', async () => {
expect(getStatus().setChecked).toEqual(null);
expect(getStatus().dir).toEqual('');
});

test('get checked data from _checked.js', async () => {
chokidar.watch.mockImplementation(chokidarWatchMockFn);
Koa.mockReset();
Koa.mockImplementationOnce(KoaMockFn);

startMock('./example/with checked data', '8080');
expect(getStatus().mockChecked).toEqual({
'api.mock.com/api-bin/api.fcgi': 1
});
expect(getStatus().setChecked).toBe(1);
});

0 comments on commit 154fb56

Please sign in to comment.