import React, { ReactElement, useState, useEffect } from "react"; import "./TestSuiteCard.scss"; import { Test } from "../types"; import TestComponent from "./Test"; import TestCard from "./TestCard"; interface TestSuiteProps { name: string; tests: Test[]; beforeAll?: (...args: any) => Promise; afterAll?: (...args: any) => Promise; onCompleted: ( name: string, completedTests: { test: Test; result: boolean; error: Error | null; executionTime: number; }[] ) => void; } const TestSuite = (props: TestSuiteProps): ReactElement => { const { name, tests, beforeAll, afterAll, onCompleted } = props; const [context, setContext] = useState(null); const [completedTests, setCompletedTests] = useState< { test: Test; result: boolean; error: Error | null; executionTime: number; }[] >([]); const [currentTest, setCurrentTest] = useState( (null as unknown) as Test ); useEffect(() => { if (beforeAll) { beforeAll().then((data) => setContext({ data })); } }, [beforeAll]); useEffect(() => { if (tests.length) { setCurrentTest(tests[0]); } setCompletedTests([]); setContext(null); }, [tests]); return (!!beforeAll && !!context) || !beforeAll ? (
{name}
{currentTest && ( { 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)); } else { onCompleted(name, newCompleteTests); } } }} /> )} {completedTests.map((completedTest, index) => { const { test, result, error } = completedTest; const { title, description } = test; return ( ); })}
) : ( <> ); }; export default TestSuite;