Skip to content

Commit

Permalink
Merge pull request #2 from sasjs/rerun-test-suite
Browse files Browse the repository at this point in the history
feat(re-run): add ability to re-run test suites
  • Loading branch information
krishna-acondy authored Jul 26, 2020
2 parents 82f2510 + a621f65 commit a9059cd
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 98 deletions.
131 changes: 83 additions & 48 deletions src/components/TestSuite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import "./TestSuiteCard.scss";
import { Test } from "../types";
import TestComponent from "./Test";
import TestCard from "./TestCard";
import TestSuiteCard from "./TestSuiteCard";

interface TestSuiteProps {
name: string;
tests: Test[];
isRunning: boolean;
beforeAll?: (...args: any) => Promise<any>;
afterAll?: (...args: any) => Promise<any>;
onCompleted: (
Expand All @@ -20,8 +22,9 @@ interface TestSuiteProps {
) => void;
}
const TestSuite = (props: TestSuiteProps): ReactElement<TestSuiteProps> => {
const { name, tests, beforeAll, afterAll, onCompleted } = props;
const { name, tests, isRunning, beforeAll, afterAll, onCompleted } = props;
const [context, setContext] = useState<any>(null);
const [running, setRunning] = useState<boolean>(false);
const [completedTests, setCompletedTests] = useState<
{
test: Test;
Expand All @@ -40,6 +43,10 @@ const TestSuite = (props: TestSuiteProps): ReactElement<TestSuiteProps> => {
}
}, [beforeAll]);

useEffect(() => {
setRunning(isRunning);
}, [isRunning]);

useEffect(() => {
if (tests.length) {
setCurrentTest(tests[0]);
Expand All @@ -48,56 +55,84 @@ const TestSuite = (props: TestSuiteProps): ReactElement<TestSuiteProps> => {
setContext(null);
}, [tests]);

return (!!beforeAll && !!context) || !beforeAll ? (
<div className="test-suite">
<div className="test-suite-name running">{name}</div>
{currentTest && (
<TestComponent
{...currentTest}
context={context}
onCompleted={(completedTest) => {
const newCompleteTests = [
...completedTests,
{
test: currentTest,
result: completedTest.result,
error: completedTest.error,
executionTime: completedTest.executionTime
}
];
setCompletedTests(newCompleteTests);
const currentIndex = tests.indexOf(currentTest);
const nextIndex =
currentIndex < tests.length - 1 ? currentIndex + 1 : -1;
if (nextIndex >= 0) {
setCurrentTest(tests[nextIndex]);
} else {
setCurrentTest(null);
}
if (newCompleteTests.length === tests.length) {
if (afterAll) {
afterAll().then(() => onCompleted(name, newCompleteTests));
if (!running && completedTests.length) {
return (
<TestSuiteCard
tests={completedTests}
name={name}
onRerun={() => {
setCompletedTests([]);
setRunning(true);
setContext(null);
if (beforeAll) {
beforeAll().then((data) => setContext({ data }));
}
if (tests.length) {
setCurrentTest(tests[0]);
}
}}
/>
);
}

return running ? (
(!!beforeAll && !!context) || !beforeAll ? (
<div className="test-suite">
<div className="test-suite-name running">{name}</div>
{currentTest && (
<TestComponent
{...currentTest}
context={context}
onCompleted={(completedTest) => {
const newCompleteTests = [
...completedTests,
{
test: currentTest,
result: completedTest.result,
error: completedTest.error,
executionTime: completedTest.executionTime
}
];
setCompletedTests(newCompleteTests);
const currentIndex = tests.indexOf(currentTest);
const nextIndex =
currentIndex < tests.length - 1 ? currentIndex + 1 : -1;
if (nextIndex >= 0) {
setCurrentTest(tests[nextIndex]);
} else {
onCompleted(name, newCompleteTests);
setCurrentTest(null);
}
if (newCompleteTests.length === tests.length) {
if (afterAll) {
afterAll().then(() => {
setRunning(false);
onCompleted(name, newCompleteTests);
});
} else {
setRunning(false);
onCompleted(name, newCompleteTests);
}
}
}
}}
/>
)}
{completedTests.map((completedTest, index) => {
const { test, result, error } = completedTest;
const { title, description } = test;
return (
<TestCard
key={index}
title={title}
description={description}
status={result === true ? "passed" : "failed"}
error={error}
}}
/>
);
})}
</div>
)}
{completedTests.map((completedTest, index) => {
const { test, result, error } = completedTest;
const { title, description } = test;
return (
<TestCard
key={index}
title={title}
description={description}
status={result === true ? "passed" : "failed"}
error={error}
/>
);
})}
</div>
) : (
<div />
)
) : (
<div />
);
Expand Down
11 changes: 11 additions & 0 deletions src/components/TestSuiteCard.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
font-size: 1.5em;
font-weight: bold;
color: #1f2027;
display: flex;
align-items: center;

&.passed {
color: green;
Expand All @@ -17,3 +19,12 @@
}
}
}

.re-run-button {
background-color: rgb(0, 115, 255);
color: #ffffff;
border: none;
border-radius: 5px;
padding: 5px;
margin-left: 10px;
}
6 changes: 5 additions & 1 deletion src/components/TestSuiteCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ interface TestSuiteCardProps {
error: Error | null;
executionTime: number;
}[];
onRerun: () => void;
}
const TestSuiteCard = (
props: TestSuiteCardProps
): ReactElement<TestSuiteCardProps> => {
const { name, tests } = props;
const { name, tests, onRerun } = props;
const overallStatus = tests.map((t) => t.result).reduce((x, y) => x && y);

return (
<div className="test-suite">
<div className={`test-suite-name ${overallStatus ? "passed" : "failed"}`}>
{name}
<button className="re-run-button" onClick={onRerun}>
Re-run
</button>
</div>
{tests.map((completedTest, index) => {
const { test, result, error, executionTime } = completedTest;
Expand Down
72 changes: 23 additions & 49 deletions src/components/TestSuiteRunner.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useEffect, useState, ReactElement } from "react";
import TestSuiteComponent from "./TestSuite";
import TestSuiteCard from "./TestSuiteCard";
import { TestSuite, Test } from "../types";
import "./TestSuiteRunner.scss";

Expand All @@ -23,22 +22,10 @@ const TestSuiteRunner = (
}[];
}[]
>([]);
const [currentTestSuite, setCurrentTestSuite] = useState<TestSuite | null>(
(null as unknown) as TestSuite
);

useEffect(() => {
if (testSuites && testSuites.length) {
setCurrentTestSuite(testSuites[0]);
}
}, [testSuites]);

useEffect(() => {
if (runTests) {
setCompletedTestSuites([]);
if (testSuites && testSuites.length) {
setCurrentTestSuite(testSuites[0]);
}
}
}, [runTests, testSuites]);

Expand All @@ -59,47 +46,34 @@ const TestSuiteRunner = (
)}
</button>
</div>
{completedTestSuites.map((completedTestSuite, index) => {
{testSuites.map((testSuite, index) => {
return (
<TestSuiteCard
<TestSuiteComponent
key={index}
tests={completedTestSuite.completedTests}
name={completedTestSuite.name}
{...testSuite}
isRunning={runTests}
onCompleted={(
name,
completedTests: {
test: Test;
result: boolean;
error: Error | null;
executionTime: number;
}[]
) => {
const newCompletedTestSuites = [
...completedTestSuites,
{ name, completedTests }
];
setCompletedTestSuites(newCompletedTestSuites);

if (newCompletedTestSuites.length === testSuites.length) {
setRunTests(false);
}
}}
/>
);
})}
{currentTestSuite && runTests && (
<TestSuiteComponent
{...currentTestSuite}
onCompleted={(
name,
completedTests: {
test: Test;
result: boolean;
error: Error | null;
executionTime: number;
}[]
) => {
const currentIndex = testSuites.indexOf(currentTestSuite);
const nextIndex =
currentIndex < testSuites.length - 1 ? currentIndex + 1 : -1;
if (nextIndex >= 0) {
setCurrentTestSuite(testSuites[nextIndex]);
} else {
setCurrentTestSuite(null);
}
const newCompletedTestSuites = [
...completedTestSuites,
{ name, completedTests }
];
setCompletedTestSuites(newCompletedTestSuites);

if (newCompletedTestSuites.length === testSuites.length) {
setRunTests(false);
}
}}
/>
)}
</div>
);
};
Expand Down

0 comments on commit a9059cd

Please sign in to comment.