Skip to content

Commit

Permalink
feat: show failure message in electron web page
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcc committed Dec 11, 2019
1 parent b9f9597 commit 3fe0ba6
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jest-electron",
"version": "0.1.9",
"version": "0.1.10",
"description": "Easiest way to run jest unit test cases in electron.",
"main": "index.js",
"files": [
Expand Down
43 changes: 38 additions & 5 deletions src/electron/main/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,30 @@
margin-left: 16px;
}

#__jest-electron-test-results-list__ .test-result-info {
#__jest-electron-test-results-list__ .test-result-block {

}

#__jest-electron-test-results-list__ .test-result-block .test-result-info {
display: flex;
flex-direction: row;
align-items: center;
justify-content: flex-start;
}

#__jest-electron-test-results-list__ .test-result-info .test-result-title {
#__jest-electron-test-results-list__ .test-result-block .test-result-info .test-result-title {
margin-right: 16px;
}

#__jest-electron-test-results-list__ .test-result-info .test-result-time {
#__jest-electron-test-results-list__ .test-result-block .test-result-info .test-result-time {
color: #AAA;
}

#__jest-electron-test-results-list__ .test-result-info.failed::before {
#__jest-electron-test-results-list__ .test-result-block .test-result-info.failed {
cursor: pointer;
}

#__jest-electron-test-results-list__ .test-result-block .test-result-info.failed::before {
content: '✖';
font-size: 12px;
display: block;
Expand All @@ -81,11 +89,36 @@
color: #c00;
}

#__jest-electron-test-results-list__ .test-result-info.passed::before {
#__jest-electron-test-results-list__ .test-result-block .test-result-info.passed::before {
content: '✓';
font-size: 12px;
display: block;
float: left;
margin-right: 5px;
color: #00d6b2;
}


/*failure message*/
#__jest-electron-test-results-list__ .test-result-block .test-result-code {
display: none;
}

#__jest-electron-test-results-list__ .test-result-block .test-result-code.show {
display: block;
}

#__jest-electron-test-results-list__ .test-result-block .test-result-code pre {
padding: 8px;
border: 1px solid #eee;
word-wrap: break-word;
border-bottom-color: #ddd;
box-shadow: 0 1px 3px #eee;
border-radius: 3px;
overflow-x: auto;
margin-right: 8px;
}

#__jest-electron-test-results-list__ .test-result-block .test-result-code code {
font-family: monospace!important;
}
30 changes: 27 additions & 3 deletions src/electron/renderer/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,18 @@ function appendTestResultDOM(r) {
if (!title) return;

const ts = r.testResults.map((tr) => {
const { title, status, duration } = tr;
const { title, status, duration, failureMessages } = tr;
const code = Array.isArray(failureMessages) ? failureMessages[0] : '';

return `<div class="test-result-info ${status}">
return `<div class="test-result-block">
<div class="test-result-info ${status}">
<div class="test-result-title">${title}</div>
<div class="test-result-time">${duration}ms</div>
</div>`;
</div>
<div class="test-result-code">
<pre><code>${code}</code></pre>
</div>
</div>`;
});

const html = `<div class="test-result-suit">
Expand All @@ -115,3 +121,21 @@ function appendTestResultDOM(r) {

$testResults.innerHTML = $testResults.innerHTML + html;
}

export function bindFailureMessageClickEvent() {
document.addEventListener('click', (e) => {
try {
// @ts-ignore
const node = e.target.parentNode;
if (node.matches('.test-result-info.failed')) {
// failure
const codeClassList = node.parentNode.querySelector('.test-result-code').classList;

// toggle
codeClassList.contains('show') ? codeClassList.remove('show') : codeClassList.add('show');
}
} catch (e) {
console.warn(e)
}
});
}
4 changes: 3 additions & 1 deletion src/electron/renderer/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ipcRenderer, remote } from 'electron';
import { EventsEnum } from '../../utils/constant';
import { fail, run } from './uitl';
import { addResult, clearResult } from './dom';
import { addResult, bindFailureMessageClickEvent, clearResult } from './dom';

export type Args = {
readonly debugMode?: boolean;
Expand Down Expand Up @@ -53,4 +53,6 @@ ipcRenderer.on(EventsEnum.ClearTestResults, async (event) => {
});

// web contents ready
bindFailureMessageClickEvent(); // bind event
ipcRenderer.send(EventsEnum.WebContentsReady);

0 comments on commit 3fe0ba6

Please sign in to comment.