Skip to content

Fix: Collapsed state between redraws #703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"build:css": "sass --no-source-map --no-error-css src/layout/css/style.scss src/pytest_html/resources/style.css",
"build:jsapp": "browserify ./src/pytest_html/scripts/index.js > ./src/pytest_html/resources/app.js",
"lint": "eslint src/pytest_html/scripts/ testing/",
"unit": "nyc mocha testing/**/unittest.js",
"unit": "nyc mocha testing/**/unittest.js --require mock-local-storage",
"all": "npm run lint && npm run unit && npm run build:css && npm run build:jsapp"
},
"devDependencies": {
Expand All @@ -13,6 +13,7 @@
"eslint": "^8.20.0",
"eslint-config-google": "^0.14.0",
"mocha": "^10.0.0",
"mock-local-storage": "^1.1.24",
"nyc": "^15.1.0",
"sass": "^1.52.3",
"sinon": "^14.0.0"
Expand Down
14 changes: 12 additions & 2 deletions src/pytest_html/scripts/datamanager.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
const { getCollapsedCategory } = require('./storage.js')
const { getCollapsedCategory, setCollapsedIds } = require('./storage.js')

class DataManager {
setManager(data) {
const collapsedCategories = [...getCollapsedCategory(data.renderCollapsed)]
const collapsedIds = []
const tests = Object.values(data.tests).flat().map((test, index) => {
const collapsed = collapsedCategories.includes(test.result.toLowerCase())
const id = `test_${index}`
if (collapsed) {
collapsedIds.push(id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd avoid updating the collapsedIds collection in the map.
Instead I would do it after the construction of collapsed collection:
const collapsedIds = collapsed.map(({id}) => id)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or even better:
setCollapsedIds(collapsed.map(({id}) => id))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually had it that way at first. But thought it was better to not do two iterations over the same collection.

}
return {
...test,
id: `test_${index}`,
id,
collapsed,
}
})
const dataBlob = { ...data, tests }
this.data = { ...dataBlob }
this.renderData = { ...dataBlob }
setCollapsedIds(collapsedIds)
}

get allData() {
Expand Down Expand Up @@ -47,6 +53,10 @@ class DataManager {
get environment() {
return this.renderData.environment
}

get initialSort() {
return this.data.initialSort
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return [...this.data.initialSort]

This prevents data.initialSort from becoming compromised by being updated by reference.

}
}

module.exports = {
Expand Down
4 changes: 4 additions & 0 deletions src/pytest_html/scripts/filter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { manager } = require('./datamanager.js')
const { doSort } = require('./sort.js')
const storageModule = require('./storage.js')

const getFilteredSubSet = (filter) =>
Expand All @@ -20,6 +21,9 @@ const doFilter = (type, show) => {
const currentFilter = storageModule.getVisible()
const filteredSubset = getFilteredSubSet(currentFilter)
manager.setRender(filteredSubset)

const sortColumn = storageModule.getSort()
doSort(sortColumn, true)
}

module.exports = {
Expand Down
34 changes: 31 additions & 3 deletions src/pytest_html/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ const { dom, findAll } = require('./dom.js')
const { manager } = require('./datamanager.js')
const { doSort } = require('./sort.js')
const { doFilter } = require('./filter.js')
const { getVisible, getSort, getSortDirection, possibleFilters } = require('./storage.js')
const {
getVisible,
getCollapsedIds,
setCollapsedIds,
getSort,
getSortDirection,
possibleFilters,
} = require('./storage.js')

const removeChildren = (node) => {
while (node.firstChild) {
Expand All @@ -22,7 +29,7 @@ const renderStatic = () => {
}

const renderContent = (tests) => {
const sortAttr = getSort(manager.allData.initialSort)
const sortAttr = getSort(manager.initialSort)
const sortAsc = JSON.parse(getSortDirection())
const rows = tests.map(dom.getResultTBody)
const table = document.getElementById('results-table')
Expand Down Expand Up @@ -53,7 +60,17 @@ const renderContent = (tests) => {

findAll('.collapsible td:not(.col-links').forEach((elem) => {
elem.addEventListener('click', ({ target }) => {
manager.toggleCollapsedItem(target.parentElement.dataset.id)
const id = target.parentElement.dataset.id
manager.toggleCollapsedItem(id)

const collapsedIds = getCollapsedIds()
if (collapsedIds.includes(id)) {
const updated = collapsedIds.filter((item) => item !== id)
setCollapsedIds(updated)
} else {
collapsedIds.push(id)
setCollapsedIds(collapsedIds)
}
redraw()
})
})
Expand All @@ -73,6 +90,14 @@ const bindEvents = () => {
const { testResult } = element.dataset

doFilter(testResult, element.checked)
const collapsedIds = getCollapsedIds()
const updated = manager.renderData.tests.map((test) => {
return {
...test,
collapsed: collapsedIds.includes(test.id),
}
})
manager.setRender(updated)
redraw()
}

Expand All @@ -88,10 +113,13 @@ const bindEvents = () => {
})
document.getElementById('show_all_details').addEventListener('click', () => {
manager.allCollapsed = false
setCollapsedIds([])
redraw()
})
document.getElementById('hide_all_details').addEventListener('click', () => {
manager.allCollapsed = true
const allIds = manager.renderData.tests.map((test) => test.id)
setCollapsedIds(allIds)
redraw()
})
}
Expand Down
19 changes: 14 additions & 5 deletions src/pytest_html/scripts/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ const durationSort = (list, ascending) => {
}

const doInitSort = () => {
const type = storageModule.getSort(manager.allData.initialSort)
const type = storageModule.getSort(manager.initialSort)
const ascending = storageModule.getSortDirection()
const list = manager.testSubset
const initialOrder = ['Error', 'Failed', 'Rerun', 'XFailed', 'XPassed', 'Skipped', 'Passed']

storageModule.setSort(type)
storageModule.setSortDirection(ascending)

if (type?.toLowerCase() === 'original') {
manager.setRender(list)
} else {
Expand All @@ -66,14 +70,19 @@ const doInitSort = () => {
}
}

const doSort = (type) => {
const newSortType = storageModule.getSort(manager.allData.initialSort) !== type
const doSort = (type, skipDirection) => {
const newSortType = storageModule.getSort(manager.initialSort) !== type
const currentAsc = storageModule.getSortDirection()
const ascending = newSortType ? true : !currentAsc
let ascending
if (skipDirection) {
ascending = currentAsc
} else {
ascending = newSortType ? false : !currentAsc
}
storageModule.setSort(type)
storageModule.setSortDirection(ascending)
const list = manager.testSubset

const list = manager.testSubset
const sortedList = type === 'duration' ? durationSort(list, ascending) : genericSort(list, type, ascending)
manager.setRender(sortedList)
}
Expand Down
Loading