Skip to content

Commit ae54ee6

Browse files
authored
Fix: Duration sorting (#691)
1 parent 2ce0aa6 commit ae54ee6

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/pytest_html/scripts/dom.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const dom = {
5151
header.querySelector('#results-table-head > tr').appendChild(t.content)
5252
})
5353

54-
header.querySelector(`.sortable[data-column-type="${sortAttr}"]`).classList.add(sortAsc ? 'desc' : 'asc')
54+
header.querySelector(`.sortable[data-column-type="${sortAttr}"]`)?.classList.add(sortAsc ? 'desc' : 'asc')
5555

5656
return header
5757
},

src/pytest_html/scripts/sort.js

+31-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ const genericSort = (list, key, ascending, customOrder) => {
2424
return sorted
2525
}
2626

27+
const durationSort = (list, ascending) => {
28+
const parseDuration = (duration) => {
29+
if (duration.includes(':')) {
30+
// If it's in the format "HH:mm:ss"
31+
const [hours, minutes, seconds] = duration.split(':').map(Number)
32+
return (hours * 3600 + minutes * 60 + seconds) * 1000
33+
} else {
34+
// If it's in the format "nnn ms"
35+
return parseInt(duration)
36+
}
37+
}
38+
const sorted = list.sort((a, b) => parseDuration(a['duration']) - parseDuration(b['duration']))
39+
if (ascending) {
40+
sorted.reverse()
41+
}
42+
return sorted
43+
}
44+
2745
const doInitSort = () => {
2846
const type = storageModule.getSort()
2947
const ascending = storageModule.getSortDirection()
@@ -32,7 +50,18 @@ const doInitSort = () => {
3250
if (type?.toLowerCase() === 'original') {
3351
manager.setRender(list)
3452
} else {
35-
const sortedList = genericSort(list, type, ascending, initialOrder)
53+
let sortedList
54+
switch (type) {
55+
case 'duration':
56+
sortedList = durationSort(list, ascending)
57+
break
58+
case 'result':
59+
sortedList = genericSort(list, type, ascending, initialOrder)
60+
break
61+
default:
62+
sortedList = genericSort(list, type, ascending)
63+
break
64+
}
3665
manager.setRender(sortedList)
3766
}
3867
}
@@ -45,7 +74,7 @@ const doSort = (type) => {
4574
storageModule.setSortDirection(ascending)
4675
const list = manager.testSubset
4776

48-
const sortedList = genericSort(list, type, ascending)
77+
const sortedList = type === 'duration' ? durationSort(list, ascending) : genericSort(list, type, ascending)
4978
manager.setRender(sortedList)
5079
}
5180

testing/unittest.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ describe('Sort tests', () => {
9494

9595
afterEach(() => [sortMock, sortDirectionMock, managerSpy].forEach((fn) => fn.restore()))
9696
it('has no stored sort', () => {
97-
sortMock = sinon.stub(storageModule, 'getSort').returns(null)
97+
sortMock = sinon.stub(storageModule, 'getSort').returns('result')
9898
sortDirectionMock = sinon.stub(storageModule, 'getSortDirection').returns(null)
9999
managerSpy = sinon.spy(dataModule.manager, 'setRender')
100100

0 commit comments

Comments
 (0)