Skip to content

Commit

Permalink
Export import unit tests (#2663)
Browse files Browse the repository at this point in the history
Co-authored-by: Opender Singh <[email protected]>
  • Loading branch information
sonicyeti and develohpanda authored Sep 25, 2020
1 parent 9d4390d commit 19a9d6a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 24 deletions.
11 changes: 11 additions & 0 deletions packages/insomnia-app/app/common/__tests__/import.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,14 @@ describe('export', () => {
name: 'Request 2',
parentId: f2._id,
});
const uts1 = await models.unitTestSuite.create({
name: 'Unit Test Suite One',
parentId: w._id,
});
const ut1 = await models.unitTest.create({
name: 'Unit Test One',
parentId: uts1._id,
});
const eBase = await models.environment.getOrCreateForWorkspace(w);
const ePub = await models.environment.create({
name: 'Public',
Expand All @@ -290,6 +298,7 @@ describe('export', () => {
});

const result = await importUtil.exportWorkspacesData(w, false, 'json');

expect(JSON.parse(result)).toEqual({
_type: 'export',
__export_format: 4,
Expand All @@ -301,6 +310,8 @@ describe('export', () => {
expect.objectContaining({ _id: jar._id }),
expect.objectContaining({ _id: r1._id }),
expect.objectContaining({ _id: r2._id }),
expect.objectContaining({ _id: uts1._id }),
expect.objectContaining({ _id: ut1._id }),
expect.objectContaining({ _id: ePub._id }),
expect.objectContaining({ _id: spec._id }),
]),
Expand Down
17 changes: 14 additions & 3 deletions packages/insomnia-app/app/common/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const EXPORT_FORMAT = 4;

const EXPORT_TYPE_REQUEST = 'request';
const EXPORT_TYPE_REQUEST_GROUP = 'request_group';
const EXPORT_TYPE_UNIT_TEST_SUITE = 'unit_test_suite';
const EXPORT_TYPE_UNIT_TEST = 'unit_test';
const EXPORT_TYPE_WORKSPACE = 'workspace';
const EXPORT_TYPE_COOKIE_JAR = 'cookie_jar';
const EXPORT_TYPE_ENVIRONMENT = 'environment';
Expand All @@ -31,6 +33,8 @@ const REPLACE_ID_REGEX = /__\w+_\d+__/g;
const MODELS = {
[EXPORT_TYPE_REQUEST]: models.request,
[EXPORT_TYPE_REQUEST_GROUP]: models.requestGroup,
[EXPORT_TYPE_UNIT_TEST_SUITE]: models.unitTestSuite,
[EXPORT_TYPE_UNIT_TEST]: models.unitTest,
[EXPORT_TYPE_WORKSPACE]: models.workspace,
[EXPORT_TYPE_COOKIE_JAR]: models.cookieJar,
[EXPORT_TYPE_ENVIRONMENT]: models.environment,
Expand Down Expand Up @@ -354,10 +358,10 @@ export async function exportRequestsData(
__export_source: `insomnia.desktop.app:v${getAppVersion()}`,
resources: [],
};

const docs: Array<BaseModel> = [];
const workspaces: Array<BaseModel> = [];
const mapTypeAndIdToDoc: Object = {};

for (const req of requests) {
const ancestors: Array<BaseModel> = clone(await db.withAncestors(req));
for (const ancestor of ancestors) {
Expand All @@ -379,7 +383,9 @@ export async function exportRequestsData(
return (
d.type === models.cookieJar.type ||
d.type === models.environment.type ||
d.type === models.apiSpec.type
d.type === models.apiSpec.type ||
d.type === models.unitTestSuite.type ||
d.type === models.unitTest.type
);
});
docs.push(...descendants);
Expand All @@ -390,6 +396,8 @@ export async function exportRequestsData(
// Only export these model types.
if (
!(
d.type === models.unitTestSuite.type ||
d.type === models.unitTest.type ||
d.type === models.request.type ||
d.type === models.requestGroup.type ||
d.type === models.workspace.type ||
Expand All @@ -410,6 +418,10 @@ export async function exportRequestsData(
d._type = EXPORT_TYPE_COOKIE_JAR;
} else if (d.type === models.environment.type) {
d._type = EXPORT_TYPE_ENVIRONMENT;
} else if (d.type === models.unitTestSuite.type) {
d._type = EXPORT_TYPE_UNIT_TEST_SUITE;
} else if (d.type === models.unitTest.type) {
d._type = EXPORT_TYPE_UNIT_TEST;
} else if (d.type === models.requestGroup.type) {
d._type = EXPORT_TYPE_REQUEST_GROUP;
} else if (d.type === models.request.type) {
Expand All @@ -424,7 +436,6 @@ export async function exportRequestsData(
});

trackEvent('Data', 'Export', `Insomnia ${format}`);

if (format.toLowerCase() === 'yaml') {
return YAML.stringify(data);
} else if (format.toLowerCase() === 'json') {
Expand Down
52 changes: 31 additions & 21 deletions packages/insomnia-app/app/ui/components/wrapper-unit-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,30 +320,40 @@ class WrapperUnitTest extends React.PureComponent<Props, State> {
);
}

const { stats, tests } = activeUnitTestResult.results;

if (activeUnitTestResult.results) {
const { stats, tests } = activeUnitTestResult.results;
return (
<div className="unit-tests__results">
{activeUnitTestResult && (
<div key={activeUnitTestResult._id}>
<div className="unit-tests__top-header">
{stats.failures ? (
<h2 className="warning">
Tests Failed {stats.failures}/{stats.tests}
</h2>
) : (
<h2 className="success">
Tests Passed {stats.passes}/{stats.tests}
</h2>
)}
</div>
<ListGroup>
{tests.map((t, i) => (
<UnitTestResultItem key={i} item={t} />
))}
</ListGroup>
</div>
)}
</div>
);
}
return (
<div className="unit-tests__results">
{activeUnitTestResult && (
<div key={activeUnitTestResult._id}>
<div className="unit-tests__top-header">
{stats.failures ? (
<h2 className="warning">
Tests Failed {stats.failures}/{stats.tests}
</h2>
) : (
<h2 className="success">
Tests Passed {stats.passes}/{stats.tests}
</h2>
)}
</div>
<ListGroup>
{tests.map((t, i) => (
<UnitTestResultItem key={i} item={t} />
))}
</ListGroup>
<div>
<div className="unit-tests__top-header">
<h2 className="success">Awaiting Test Execution</h2>
</div>
)}
</div>
</div>
);
}
Expand Down

0 comments on commit 19a9d6a

Please sign in to comment.