cleanup: remove unnecessary if elses; replace self with this

pull/1/head
Iuri Matias 6 years ago committed by yann300
parent 8d9c9bda58
commit 544df4cecc
  1. 165
      src/app/tabs/test-tab.js

@ -33,69 +33,62 @@ module.exports = class TestTab extends ApiFactory {
} }
listenToEvents () { listenToEvents () {
const self = this this.filePanel.event.register('newTestFileCreated', file => {
var testList = this.view.querySelector("[class^='testList']")
self.filePanel.event.register('newTestFileCreated', file => {
var testList = self.view.querySelector("[class^='testList']")
var test = yo`<label class="singleTestLabel"><input class="singleTest" onchange=${(e) => this.toggleCheckbox(e.target.checked, file)} type="checkbox" checked="true">${file}</label>` var test = yo`<label class="singleTestLabel"><input class="singleTest" onchange=${(e) => this.toggleCheckbox(e.target.checked, file)} type="checkbox" checked="true">${file}</label>`
testList.appendChild(test) testList.appendChild(test)
self.data.allTests.push(file) this.data.allTests.push(file)
self.data.selectedTests.push(file) this.data.selectedTests.push(file)
}) })
self.fileManager.event.register('currentFileChanged', (file, provider) => { this.fileManager.event.register('currentFileChanged', (file, provider) => {
self.getTests(self, (error, tests) => { this.getTests((error, tests) => {
if (error) return tooltip(error) if (error) return tooltip(error)
self.data.allTests = tests this.data.allTests = tests
self.data.selectedTests = [...self.data.allTests] this.data.selectedTests = [...this.data.allTests]
if (!tests.length) { if (!tests.length) {
yo.update(self.testList, yo`<div class=${css.testList}>No test file available</div>`) yo.update(this.testList, yo`<div class=${css.testList}>No test file available</div>`)
} else { } else {
yo.update(self.testList, yo`<div class=${css.testList}>${self.listTests()}</div>`) yo.update(this.testList, yo`<div class=${css.testList}>${this.listTests()}</div>`)
} }
if (!self.testsOutput || !self.testsSummary) return if (!this.testsOutput || !this.testsSummary) return
self.testsOutput.hidden = true this.testsOutput.hidden = true
self.testsSummary.hidden = true this.testsSummary.hidden = true
self.testsOutput.innerHTML = '' this.testsOutput.innerHTML = ''
self.testsSummary.innerHTML = '' this.testsSummary.innerHTML = ''
}) })
}) })
} }
getTests (self, cb) { getTests (cb) {
var path = self.fileManager.currentPath() var path = this.fileManager.currentPath()
if (!path) return cb(null, []) if (!path) return cb(null, [])
var provider = self.fileManager.fileProviderOf(path) var provider = this.fileManager.fileProviderOf(path)
if (!provider) return cb(null, []) if (!provider) return cb(null, [])
var tests = [] var tests = []
self.fileManager.filesFromPath(path, (error, files) => { this.fileManager.filesFromPath(path, (error, files) => {
if (error) return cb(error) if (error) return cb(error)
if (!error) { for (var file in files) {
for (var file in files) { if (/.(_test.sol)$/.exec(file)) tests.push(provider.type + '/' + file)
if (/.(_test.sol)$/.exec(file)) tests.push(provider.type + '/' + file)
}
cb(null, tests)
} }
cb(null, tests)
}) })
} }
listTests () { listTests () {
const self = this return this.data.allTests.map(test => yo`<label class="singleTestLabel"><input class="singleTest" onchange =${(e) => this.toggleCheckbox(e.target.checked, test)} type="checkbox" checked="true">${test} </label>`)
var tests = self.data.allTests
return tests.map(test => yo`<label class="singleTestLabel"><input class="singleTest" onchange =${(e) => self.toggleCheckbox(e.target.checked, test)} type="checkbox" checked="true">${test} </label>`)
} }
toggleCheckbox (eChecked, test) { toggleCheckbox (eChecked, test) {
const self = this if (!this.data.selectedTests) {
if (!self.data.selectedTests) { this.data.selectedTests = this._view.el.querySelectorAll('.singleTest:checked')
self.data.selectedTests = self._view.el.querySelectorAll('.singleTest:checked')
} }
let selectedTests = self.data.selectedTests let selectedTests = this.data.selectedTests
selectedTests = eChecked ? [...selectedTests, test] : selectedTests.filter(el => el !== test) selectedTests = eChecked ? [...selectedTests, test] : selectedTests.filter(el => el !== test)
self.data.selectedTests = selectedTests this.data.selectedTests = selectedTests
let checkAll = self._view.el.querySelector('[id="checkAllTests"]') let checkAll = this._view.el.querySelector('[id="checkAllTests"]')
if (eChecked) { if (eChecked) {
checkAll.checked = true checkAll.checked = true
} else if (!selectedTests.length) { } else if (!selectedTests.length) {
@ -104,25 +97,23 @@ module.exports = class TestTab extends ApiFactory {
} }
checkAll (event) { checkAll (event) {
const self = this let checkBoxes = this._view.el.querySelectorAll('.singleTest')
let checkBoxes = self._view.el.querySelectorAll('.singleTest') const checkboxesLabels = this._view.el.querySelectorAll('.singleTestLabel')
const checkboxesLabels = self._view.el.querySelectorAll('.singleTestLabel')
// checks/unchecks all // checks/unchecks all
for (let i = 0; i < checkBoxes.length; i++) { for (let i = 0; i < checkBoxes.length; i++) {
checkBoxes[i].checked = event.target.checked checkBoxes[i].checked = event.target.checked
self.toggleCheckbox(event.target.checked, checkboxesLabels[i].innerText) this.toggleCheckbox(event.target.checked, checkboxesLabels[i].innerText)
} }
} }
testCallback (result) { testCallback (result) {
const self = this this.testsOutput.hidden = false
self.testsOutput.hidden = false
if (result.type === 'contract') { if (result.type === 'contract') {
self.testsOutput.appendChild(yo`<div class=${css.outputTitle}>${result.filename} (${result.value})</div>`) this.testsOutput.appendChild(yo`<div class=${css.outputTitle}>${result.filename} (${result.value})</div>`)
} else if (result.type === 'testPass') { } else if (result.type === 'testPass') {
self.testsOutput.appendChild(yo`<div class='${css.testPass} ${css.testLog}'>✓ (${result.value})</div>`) this.testsOutput.appendChild(yo`<div class='${css.testPass} ${css.testLog}'>✓ (${result.value})</div>`)
} else if (result.type === 'testFailure') { } else if (result.type === 'testFailure') {
self.testsOutput.appendChild(yo`<div class='${css.testFailure} ${css.testLog}'>✘ (${result.value})</div>`) this.testsOutput.appendChild(yo`<div class='${css.testFailure} ${css.testLog}'>✘ (${result.value})</div>`)
} }
} }
@ -135,71 +126,63 @@ module.exports = class TestTab extends ApiFactory {
} }
updateFinalResult (_err, result, filename) { updateFinalResult (_err, result, filename) {
const self = this this.testsSummary.hidden = false
self.testsSummary.hidden = false
if (_err) { if (_err) {
self.testsSummary.appendChild(yo`<div class=${css.testFailureSummary} >${_err.message}</div>`) this.testsSummary.appendChild(yo`<div class=${css.testFailureSummary} >${_err.message}</div>`)
return return
} }
self.testsSummary.appendChild(yo`<div class=${css.summaryTitle}> ${filename} </div>`) this.testsSummary.appendChild(yo`<div class=${css.summaryTitle}> ${filename} </div>`)
if (result.totalPassing > 0) { if (result.totalPassing > 0) {
self.testsSummary.appendChild(yo`<div>${result.totalPassing} passing (${result.totalTime}s)</div>`) this.testsSummary.appendChild(yo`<div>${result.totalPassing} passing (${result.totalTime}s)</div>`)
self.testsSummary.appendChild(yo`<br>`) this.testsSummary.appendChild(yo`<br>`)
} }
if (result.totalFailing > 0) { if (result.totalFailing > 0) {
self.testsSummary.appendChild(yo`<div>${result.totalFailing} failing</div>`) this.testsSummary.appendChild(yo`<div>${result.totalFailing} failing</div>`)
self.testsSummary.appendChild(yo`<br>`) this.testsSummary.appendChild(yo`<br>`)
} }
result.errors.forEach((error, index) => { result.errors.forEach((error, index) => {
self.testsSummary.appendChild(yo`<div>${error.context} - ${error.value} </div>`) this.testsSummary.appendChild(yo`<div>${error.context} - ${error.value} </div>`)
self.testsSummary.appendChild(yo`<div class=${css.testFailureSummary} >${error.message}</div>`) this.testsSummary.appendChild(yo`<div class=${css.testFailureSummary} >${error.message}</div>`)
self.testsSummary.appendChild(yo`<br>`) this.testsSummary.appendChild(yo`<br>`)
}) })
} }
runTest (testFilePath, callback) { runTest (testFilePath, callback) {
const self = this this.fileManager.fileProviderOf(testFilePath).get(testFilePath, (error, content) => {
self.fileManager.fileProviderOf(testFilePath).get(testFilePath, (error, content) => { if (error) return
if (!error) { var runningTest = {}
var runningTest = {} runningTest[testFilePath] = { content }
runningTest[testFilePath] = { content } remixTests.runTestSources(runningTest, this.testCallback, this.resultsCallback, (error, result) => {
remixTests.runTestSources(runningTest, self.testCallback, self.resultsCallback, (error, result) => { this.updateFinalResult(error, result, testFilePath)
self.updateFinalResult(error, result, testFilePath) callback(error)
callback(error) }, (url, cb) => {
}, (url, cb) => { return this.compileTab.compileTabLogic.importFileCb(url, cb)
return self.compileTab.compileTabLogic.importFileCb(url, cb) })
})
}
}) })
} }
runTests () { runTests () {
const self = this this.testsOutput.innerHTML = ''
self.testsOutput.innerHTML = '' this.testsSummary.innerHTML = ''
self.testsSummary.innerHTML = '' var tests = this.data.selectedTests
var tests = self.data.selectedTests async.eachOfSeries(tests, (value, key, callback) => { this.runTest(value, callback) })
async.eachOfSeries(tests, (value, key, callback) => { self.runTest(value, callback) })
} }
generateTestFile () { generateTestFile () {
const self = this var fileManager = this.fileManager
var fileManager = self.fileManager
var path = fileManager.currentPath() var path = fileManager.currentPath()
var fileProvider = fileManager.fileProviderOf(path) var fileProvider = fileManager.fileProviderOf(path)
if (fileProvider) { if (!fileProvider) return
helper.createNonClashingNameWithPrefix(path + '/test.sol', fileProvider, '_test', (error, newFile) => { helper.createNonClashingNameWithPrefix(path + '/test.sol', fileProvider, '_test', (error, newFile) => {
if (error) return modalDialogCustom.alert('Failed to create file. ' + newFile + ' ' + error) if (error) return modalDialogCustom.alert('Failed to create file. ' + newFile + ' ' + error)
if (!fileProvider.set(newFile, testContractSample)) { if (!fileProvider.set(newFile, testContractSample)) {
modalDialogCustom.alert('Failed to create test file ' + newFile) return modalDialogCustom.alert('Failed to create test file ' + newFile)
} else { }
fileManager.switchFile(newFile) fileManager.switchFile(newFile)
} })
})
}
} }
render () { render () {
const self = this
this.testsOutput = yo`<div class=${css.container} hidden='true' id="tests"></div>` this.testsOutput = yo`<div class=${css.container} hidden='true' id="tests"></div>`
this.testsSummary = yo`<div class=${css.container} hidden='true' id="tests"></div>` this.testsSummary = yo`<div class=${css.container} hidden='true' id="tests"></div>`
@ -214,12 +197,12 @@ module.exports = class TestTab extends ApiFactory {
<br/> <br/>
For more details, see For more details, see
How to test smart contracts guide in our documentation. How to test smart contracts guide in our documentation.
<div class="${css.generateTestFile}" onclick="${self.generateTestFile(self)}">Generate test file</div> <div class="${css.generateTestFile}" onclick="${this.generateTestFile(this)}">Generate test file</div>
</div> </div>
<div class="${css.tests}"> <div class="${css.tests}">
${self.testList} ${this.testList}
<div class="${css.buttons}"> <div class="${css.buttons}">
<div class="${css.runButton}" onclick="${self.runTests.bind(self)}">Run Tests</div> <div class="${css.runButton}" onclick="${this.runTests.bind(this)}">Run Tests</div>
<label class="${css.label}" for="checkAllTests"> <label class="${css.label}" for="checkAllTests">
<input id="checkAllTests" <input id="checkAllTests"
type="checkbox" type="checkbox"
@ -229,12 +212,12 @@ module.exports = class TestTab extends ApiFactory {
Check/Uncheck all Check/Uncheck all
</label> </label>
</div> </div>
${self.testsOutput} ${this.testsOutput}
${self.testsSummary} ${this.testsSummary}
</div> </div>
</div> </div>
` `
if (!self._view.el) self._view.el = el if (!this._view.el) this._view.el = el
return el return el
} }

Loading…
Cancel
Save