Skip to content

Commit 62b0eea

Browse files
authored
Feature: Template table header (#696)
1 parent 33b3b66 commit 62b0eea

File tree

5 files changed

+25
-31
lines changed

5 files changed

+25
-31
lines changed

src/pytest_html/basereport.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def _generate_report(self, self_contained=False):
5353
self.css,
5454
self_contained=self_contained,
5555
test_data=cleanup_unserializable(self._report.data),
56+
table_head=self._report.data["resultsTableHeader"],
5657
prefix=self._report.data["additionalSummary"]["prefix"],
5758
summary=self._report.data["additionalSummary"]["summary"],
5859
postfix=self._report.data["additionalSummary"]["postfix"],
@@ -123,6 +124,7 @@ def _render_html(
123124
styles,
124125
self_contained,
125126
test_data,
127+
table_head,
126128
summary,
127129
prefix,
128130
postfix,
@@ -134,6 +136,7 @@ def _render_html(
134136
styles=styles,
135137
self_contained=self_contained,
136138
test_data=json.dumps(test_data),
139+
table_head=table_head,
137140
summary=summary,
138141
prefix=prefix,
139142
postfix=postfix,

src/pytest_html/resources/index.jinja2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
<template id="template_results-table__head">
5656
<thead id="results-table-head">
5757
<tr>
58+
{% for th in table_head %}
59+
{{ th|safe }}
60+
{% endfor %}
5861
</tr>
5962
</thead>
6063
</template>

src/pytest_html/scripts/dom.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
const storageModule = require('./storage.js')
21
const mediaViewer = require('./mediaviewer.js')
32
const templateEnvRow = document.querySelector('#template_environment_row')
43
const templateCollGroup = document.querySelector('#template_table-colgroup')
54
const templateResult = document.querySelector('#template_results-table__tbody')
6-
const listHeader = document.querySelector('#template_results-table__head')
75
const listHeaderEmpty = document.querySelector('#template_results-table__head--empty')
86

97
function htmlToElements(html) {
@@ -40,21 +38,6 @@ const dom = {
4038

4139
return envRow
4240
},
43-
getListHeader: ({ initialSort, resultsTableHeader }) => {
44-
const header = listHeader.content.cloneNode(true)
45-
const sortAttr = storageModule.getSort(initialSort)
46-
const sortAsc = JSON.parse(storageModule.getSortDirection())
47-
48-
resultsTableHeader.forEach((html) => {
49-
const t = document.createElement('template')
50-
t.innerHTML = html
51-
header.querySelector('#results-table-head > tr').appendChild(t.content)
52-
})
53-
54-
header.querySelector(`.sortable[data-column-type="${sortAttr}"]`)?.classList.add(sortAsc ? 'desc' : 'asc')
55-
56-
return header
57-
},
5841
getListHeaderEmpty: () => listHeaderEmpty.content.cloneNode(true),
5942
getColGroup: () => templateCollGroup.content.cloneNode(true),
6043
getResultTBody: ({ testId, id, log, duration, extras, resultsTableRow, tableHtml, result, collapsed }) => {

src/pytest_html/scripts/main.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { dom, findAll } = require('./dom.js')
22
const { manager } = require('./datamanager.js')
33
const { doSort } = require('./sort.js')
44
const { doFilter } = require('./filter.js')
5-
const { getVisible, possibleResults } = require('./storage.js')
5+
const { getVisible, getSort, getSortDirection, possibleResults } = require('./storage.js')
66

77
const removeChildren = (node) => {
88
while (node.firstChild) {
@@ -28,10 +28,15 @@ const renderStatic = () => {
2828
}
2929

3030
const renderContent = (tests) => {
31+
const sortAttr = getSort(manager.allData.initialSort)
32+
const sortAsc = JSON.parse(getSortDirection())
3133
const rows = tests.map(dom.getResultTBody)
3234
const table = document.querySelector('#results-table')
35+
const tableHeader = document.getElementById('template_results-table__head').content.cloneNode(true)
36+
3337
removeChildren(table)
34-
const tableHeader = dom.getListHeader(manager.renderData)
38+
39+
tableHeader.querySelector(`.sortable[data-column-type="${sortAttr}"]`)?.classList.add(sortAsc ? 'desc' : 'asc')
3540
if (!rows.length) {
3641
tableHeader.appendChild(dom.getListHeaderEmpty())
3742
}

testing/unittest.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const storageModule = require('../src/pytest_html/scripts/storage.js')
77

88

99
const setTestData = () => {
10-
const jsonDatan = {
10+
const jsonData = {
1111
'tests':
1212
[
1313
{
@@ -36,7 +36,17 @@ const setTestData = () => {
3636
},
3737
],
3838
}
39-
dataModule.manager.setManager(jsonDatan)
39+
dataModule.manager.setManager(jsonData)
40+
}
41+
42+
const mockWindow = (queryParam) => {
43+
const mock = {
44+
location: {
45+
href: `https://example.com/page?${queryParam}`,
46+
},
47+
}
48+
originalWindow = global.window
49+
global.window = mock
4050
}
4151

4252
describe('Filter tests', () => {
@@ -190,16 +200,6 @@ describe('Sort tests', () => {
190200
})
191201
})
192202

193-
const mockWindow = (queryParam) => {
194-
const mock = {
195-
location: {
196-
href: `https://example.com/page?${queryParam}`,
197-
},
198-
}
199-
originalWindow = global.window
200-
global.window = mock
201-
}
202-
203203
describe('Storage tests', () => {
204204
describe('getCollapsedCategory', () => {
205205
let originalWindow

0 commit comments

Comments
 (0)