refactor: refactor exportWorkspacesHAR() and exportWorkspacesJSON(), and fix affected tests

This commit is contained in:
Ricky Chandra 2019-03-12 23:37:05 +07:00
parent 10265edd02
commit 683ac86f4f
3 changed files with 21 additions and 91 deletions

View File

@ -3,9 +3,9 @@ import * as importUtil from '../import';
import { getAppVersion } from '../constants';
import { globalBeforeEach } from '../../__jest__/before-each';
describe('exportWorkspacesHAR()', () => {
describe('exportWorkspacesHAR() and exportRequestsHAR()', () => {
beforeEach(globalBeforeEach);
it('exports a single workspace as an HTTP Archive', async () => {
it('exports a single workspace and some requests only as an HTTP Archive', async () => {
const wrk1 = await models.workspace.create({
_id: 'wrk_1',
name: 'Workspace 1',
@ -155,28 +155,28 @@ describe('exportWorkspacesHAR()', () => {
expect(data).toMatchObject({
log: {
entries: [
{
request: {
entries: expect.arrayContaining([
expect.objectContaining({
request: expect.objectContaining({
headers: [{ name: 'X-Environment', value: 'public1' }],
},
}),
comment: 'Request 1',
},
{
request: {
}),
expect.objectContaining({
request: expect.objectContaining({
headers: [{ name: 'X-Environment', value: 'base2' }],
},
}),
comment: 'Request 2',
},
],
}),
]),
},
});
});
});
describe('exportWorkspacesJSON()', () => {
describe('exportWorkspacesJSON() and exportRequestsJSON()', () => {
beforeEach(globalBeforeEach);
it('exports all workspaces', async () => {
it('exports all workspaces and some requests only', async () => {
const w = await models.workspace.create({ name: 'Workspace' });
const jar = await models.cookieJar.getOrCreateForParentId(w._id);
const r1 = await models.request.create({

View File

@ -192,43 +192,9 @@ export async function exportWorkspacesHAR(
parentDoc: BaseModel | null = null,
includePrivateDocs: boolean = false,
): Promise<string> {
let workspaces;
if (parentDoc) {
workspaces = [parentDoc];
} else {
workspaces = await models.workspace.all();
}
const workspaceEnvironmentLookup = {};
for (let workspace of workspaces) {
const workspaceMeta = await models.workspaceMeta.getByParentId(workspace._id);
let environmentId = workspaceMeta ? workspaceMeta.activeEnvironmentId : null;
const environment = await models.environment.getById(environmentId || 'n/a');
if (!environment || (environment.isPrivate && !includePrivateDocs)) {
environmentId = 'n/a';
}
workspaceEnvironmentLookup[workspace._id] = environmentId;
}
const requests = [];
for (let workspace of workspaces) {
const docs: Array<BaseModel> = await getDocWithDescendants(workspace, includePrivateDocs);
const workspaceRequests = docs
.filter(d => d.type === models.request.type)
.sort((a: Object, b: Object) => (a.metaSortKey < b.metaSortKey ? -1 : 1))
.map((request: BaseModel) => {
return {
requestId: request._id,
environmentId: workspaceEnvironmentLookup[workspace._id],
};
});
requests.push(...workspaceRequests);
}
const data = await har.exportHar(requests);
return JSON.stringify(data, null, '\t');
const docs: Array<BaseModel> = await getDocWithDescendants(parentDoc, includePrivateDocs);
const requests: Array<BaseModel> = docs.filter(doc => doc.type === models.request.type);
return exportRequestsHAR(requests, includePrivateDocs);
}
export async function exportRequestsHAR(
@ -287,45 +253,9 @@ export async function exportWorkspacesJSON(
parentDoc: BaseModel | null = null,
includePrivateDocs: boolean = false,
): Promise<string> {
const data = {
_type: 'export',
__export_format: EXPORT_FORMAT,
__export_date: new Date(),
__export_source: `insomnia.desktop.app:v${getAppVersion()}`,
resources: [],
};
const docs: Array<BaseModel> = await getDocWithDescendants(parentDoc, includePrivateDocs);
data.resources = docs
.filter(
d =>
// Only export these model types
d.type === models.request.type ||
d.type === models.requestGroup.type ||
d.type === models.workspace.type ||
d.type === models.cookieJar.type ||
d.type === models.environment.type,
)
.map((d: Object) => {
if (d.type === models.workspace.type) {
d._type = EXPORT_TYPE_WORKSPACE;
} else if (d.type === models.cookieJar.type) {
d._type = EXPORT_TYPE_COOKIE_JAR;
} else if (d.type === models.environment.type) {
d._type = EXPORT_TYPE_ENVIRONMENT;
} else if (d.type === models.requestGroup.type) {
d._type = EXPORT_TYPE_REQUEST_GROUP;
} else if (d.type === models.request.type) {
d._type = EXPORT_TYPE_REQUEST;
}
// Delete the things we don't want to export
delete d.type;
return d;
});
return JSON.stringify(data, null, '\t');
const requests: Array<BaseModel> = docs.filter(doc => doc.type === models.request.type);
return exportRequestsJSON(requests, includePrivateDocs);
}
export async function exportRequestsJSON(

View File

@ -171,7 +171,7 @@ describe('app.export.*', () => {
__export_format: 3,
__export_source: `insomnia.desktop.app:v${getAppVersion()}`,
_type: 'export',
resources: [
resources: expect.arrayContaining([
{
_id: 'wrk_1',
_type: 'workspace',
@ -204,7 +204,7 @@ describe('app.export.*', () => {
settingMaxTimelineDataSize: 1000,
url: 'https://insomnia.rest',
},
],
]),
});
});