render a test only once

pull/5370/head
Aniket-Engg 3 years ago committed by Aniket
parent 45f074855c
commit 8ebb852df3
  1. 159
      libs/remix-ui/solidity-unit-testing/src/lib/solidity-unit-testing.tsx

@ -37,7 +37,7 @@ export const SolidityUnitTesting = (props: any) => {
const [checkSelectAll, setCheckSelectAll] = useState(true) const [checkSelectAll, setCheckSelectAll] = useState(true)
const [testsOutput, setTestsOutput] = useState<Element[]>([]) const [testsOutput, setTestsOutput] = useState<Element[]>([])
let [testsSummary, setTestsSummary] = useState<TestSummary>() // let [testsSummary, setTestsSummary] = useState<TestSummary>()
const [testsSummaryHidden, setTestsSummaryHidden] = useState('hidden') const [testsSummaryHidden, setTestsSummaryHidden] = useState('hidden')
const [testsExecutionStoppedHidden, setTestsExecutionStoppedHidden] = useState(true) const [testsExecutionStoppedHidden, setTestsExecutionStoppedHidden] = useState(true)
@ -255,7 +255,7 @@ export const SolidityUnitTesting = (props: any) => {
const showTestsResult = () => { const showTestsResult = () => {
console.log('runningTests---->', runningTests) console.log('runningTests---->', runningTests)
setTestsOutput([]) // setTestsOutput([])
let filenames = Object.keys(testsResultByFilename) let filenames = Object.keys(testsResultByFilename)
for(const filename of filenames) { for(const filename of filenames) {
const fileTestsResult = testsResultByFilename[filename] const fileTestsResult = testsResultByFilename[filename]
@ -285,81 +285,90 @@ export const SolidityUnitTesting = (props: any) => {
{label}<span className="font-weight-bold">{contract} ({filename})</span> {label}<span className="font-weight-bold">{contract} ({filename})</span>
</div> </div>
) )
setTestsOutput(prevCards => ([...prevCards, ContractCard])) setTestsOutput(prevCards => {
const index = prevCards.findIndex((card: any) => card.props.id === runningTestFileName)
prevCards[index] = ContractCard
return prevCards
})
// show tests // show tests
for(const test of tests) { for(const test of tests) {
console.log('test---->', test) console.log('test---->', test)
let debugBtn if(!test.rendered) {
if (test.debugTxHash) { let debugBtn
const { web3, debugTxHash } = test if (test.debugTxHash) {
debugBtn = ( const { web3, debugTxHash } = test
<div id={test.value.replaceAll(' ', '_')} className="btn border btn btn-sm ml-1" style={{ cursor: 'pointer' }} title="Start debugging" onClick={() => startDebug(debugTxHash, web3)}> debugBtn = (
<i className="fas fa-bug"></i> <div id={test.value.replaceAll(' ', '_')} className="btn border btn btn-sm ml-1" style={{ cursor: 'pointer' }} title="Start debugging" onClick={() => startDebug(debugTxHash, web3)}>
</div> <i className="fas fa-bug"></i>
)
}
if (test.type === 'testPass') {
if (test.hhLogs && test.hhLogs.length) printHHLogs(test.hhLogs, test.value)
const testPassCard: any = (
<div
id={runningTestFileName}
data-id="testTabSolidityUnitTestsOutputheader"
className="testPass testLog bg-light mb-2 px-2 text-success border-0"
onClick={() => discardHighlight()}
>
<div className="d-flex my-1 align-items-start justify-content-between">
<span > {test.value}</span>
{debugBtn}
</div>
</div> </div>
) )
setTestsOutput(prevCards => ([...prevCards, testPassCard])) }
} else if (test.type === 'testFailure') { if (test.type === 'testPass') {
if (test.hhLogs && test.hhLogs.length) printHHLogs(test.hhLogs, test.value) if (test.hhLogs && test.hhLogs.length) printHHLogs(test.hhLogs, test.value)
if (!test.assertMethod) { const testPassCard: any = (
const testFailCard1: any = (<div <div
className="bg-light mb-2 px-2 testLog d-flex flex-column text-danger border-0" id={runningTestFileName}
id={"UTContext" + test.context} data-id="testTabSolidityUnitTestsOutputheader"
onClick={() => highlightLocation(test.location, runningTests, test.filename)} className="testPass testLog bg-light mb-2 px-2 text-success border-0"
> onClick={() => discardHighlight()}
<div className="d-flex my-1 align-items-start justify-content-between"> >
<span> {test.value}</span> <div className="d-flex my-1 align-items-start justify-content-between">
{debugBtn} <span > {test.value}</span>
</div>
<span className="text-dark">Error Message:</span>
<span className="pb-2 text-break">"{test.errMsg}"</span>
</div>)
setTestsOutput(prevCards => ([...prevCards, testFailCard1]))
} else {
const preposition = test.assertMethod === 'equal' || test.assertMethod === 'notEqual' ? 'to' : ''
const method = test.assertMethod === 'ok' ? '' : test.assertMethod
const expected = test.assertMethod === 'ok' ? '\'true\'' : test.expected
const testFailCard2: any = (<div
className="bg-light mb-2 px-2 testLog d-flex flex-column text-danger border-0"
id={"UTContext" + test.context}
onClick={() => highlightLocation(test.location, runningTests, test.filename)}
>
<div className="d-flex my-1 align-items-start justify-content-between">
<span> {test.value}</span>
{debugBtn} {debugBtn}
</div>
<span className="text-dark">Error Message:</span>
<span className="pb-2 text-break">"{test.errMsg}"</span>
<span className="text-dark">Assertion:</span>
<div className="d-flex flex-wrap">
<span>Expected value should be</span>
<div className="mx-1 font-weight-bold">{method}</div>
<div>{preposition} {expected}</div>
</div> </div>
<span className="text-dark">Received value:</span> </div>
<span>{test.returned}</span> )
<span className="text-dark text-sm pb-2">Skipping the remaining tests of the function.</span> setTestsOutput(prevCards => ([...prevCards, testPassCard]))
</div>) test.rendered = true
setTestsOutput(prevCards => ([...prevCards, testFailCard2])) } else if (test.type === 'testFailure') {
if (test.hhLogs && test.hhLogs.length) printHHLogs(test.hhLogs, test.value)
if (!test.assertMethod) {
const testFailCard1: any = (<div
className="bg-light mb-2 px-2 testLog d-flex flex-column text-danger border-0"
id={"UTContext" + test.context}
onClick={() => highlightLocation(test.location, runningTests, test.filename)}
>
<div className="d-flex my-1 align-items-start justify-content-between">
<span> {test.value}</span>
{debugBtn}
</div>
<span className="text-dark">Error Message:</span>
<span className="pb-2 text-break">"{test.errMsg}"</span>
</div>)
setTestsOutput(prevCards => ([...prevCards, testFailCard1]))
} else {
const preposition = test.assertMethod === 'equal' || test.assertMethod === 'notEqual' ? 'to' : ''
const method = test.assertMethod === 'ok' ? '' : test.assertMethod
const expected = test.assertMethod === 'ok' ? '\'true\'' : test.expected
const testFailCard2: any = (<div
className="bg-light mb-2 px-2 testLog d-flex flex-column text-danger border-0"
id={"UTContext" + test.context}
onClick={() => highlightLocation(test.location, runningTests, test.filename)}
>
<div className="d-flex my-1 align-items-start justify-content-between">
<span> {test.value}</span>
{debugBtn}
</div>
<span className="text-dark">Error Message:</span>
<span className="pb-2 text-break">"{test.errMsg}"</span>
<span className="text-dark">Assertion:</span>
<div className="d-flex flex-wrap">
<span>Expected value should be</span>
<div className="mx-1 font-weight-bold">{method}</div>
<div>{preposition} {expected}</div>
</div>
<span className="text-dark">Received value:</span>
<span>{test.returned}</span>
<span className="text-dark text-sm pb-2">Skipping the remaining tests of the function.</span>
</div>)
setTestsOutput(prevCards => ([...prevCards, testFailCard2]))
}
test.rendered = true
} else if (test.type === 'logOnly') {
if (test.hhLogs && test.hhLogs.length) printHHLogs(test.hhLogs, test.value)
test.rendered = true
} }
} else if (test.type === 'logOnly') { }
if (test.hhLogs && test.hhLogs.length) printHHLogs(test.hhLogs, test.value)
}
} }
} else { } else {
// show only contract and file name // show only contract and file name
@ -374,7 +383,7 @@ export const SolidityUnitTesting = (props: any) => {
} }
// show summary // show summary
const testSummary = fileTestsResult['summary'] const testSummary = fileTestsResult['summary']
if (testSummary && testSummary.filename) { if (testSummary && testSummary.filename && !testSummary.rendered) {
const summaryCard: any = (<div className="d-flex alert-secondary mb-3 p-3 flex-column"> const summaryCard: any = (<div className="d-flex alert-secondary mb-3 p-3 flex-column">
<span className="font-weight-bold">Result for {testSummary.filename}</span> <span className="font-weight-bold">Result for {testSummary.filename}</span>
<span className="text-success">Passed: {testSummary.passed}</span> <span className="text-success">Passed: {testSummary.passed}</span>
@ -382,6 +391,7 @@ export const SolidityUnitTesting = (props: any) => {
<span>Time Taken: {testSummary.timeTaken}s</span> <span>Time Taken: {testSummary.timeTaken}s</span>
</div>) </div>)
setTestsOutput(prevCards => ([...prevCards, summaryCard])) setTestsOutput(prevCards => ([...prevCards, summaryCard]))
fileTestsResult['summary']['rendered'] = true
} }
} }
} }
@ -397,8 +407,11 @@ export const SolidityUnitTesting = (props: any) => {
} }
if(result.type === 'contract') { if(result.type === 'contract') {
testsResultByFilename[result.filename][result.value] = [] testsResultByFilename[result.filename][result.value] = []
} else } else {
// Set that this test is not rendered on UI
result.rendered = false
testsResultByFilename[result.filename][result.context].push(result) testsResultByFilename[result.filename][result.context].push(result)
}
showTestsResult() showTestsResult()
} }
} }
@ -432,7 +445,7 @@ export const SolidityUnitTesting = (props: any) => {
// yo.update(this.resultStatistics, this.createResultLabel()) // yo.update(this.resultStatistics, this.createResultLabel())
if (result) { if (result) {
const totalTime = parseFloat(result.totalTime).toFixed(2) const totalTime = parseFloat(result.totalTime).toFixed(2)
testsSummary = { filename, passed: result.totalPassing, failed: result.totalFailing, timeTaken: totalTime } const testsSummary = { filename, passed: result.totalPassing, failed: result.totalFailing, timeTaken: totalTime, rendered: false }
testsResultByFilename[filename]['summary']= testsSummary testsResultByFilename[filename]['summary']= testsSummary
showTestsResult() showTestsResult()
// setTestsSummary(testsSummary) // setTestsSummary(testsSummary)

Loading…
Cancel
Save