[FEATURE] toggle document.hidden in jsdom
See original GitHub issueBasic info:
- Node.js version: 8.12.0
- jsdom version: 12.2.0
I need to test code that set a timestamp value depending on document.hidden
.
jsdom toggles the value depending on pretendToBeVisual
as documented in the jsdom README.
I am using jest which uses jsdom.
Minimal reproduction case
I need a way to toggle document.hidden
in jsdom in order to test code that has different code paths depending on the value.
To test the following function getTimeDuration
:
/**
* First call starts a timer and returns a function
* that when called will give the elapsed time in
* milisecond, while subtracting "hidden" time.
* @returns {function} Calling this function will return the elapsed time.
*/
export default function getTimeDuration () {
const startTimestamp = Date.now()
let hiddenTimeDelta = 0
let hiddenTimeStart = 0
document.addEventListener('visibilitychange', trackHiddenTime)
function trackHiddenTime () {
if (document.hidden) {
hiddenTimeStart = Date.now()
} else {
hiddenTimeDelta += (Date.now() - hiddenTimeStart)
}
}
return () => {
document.removeEventListener('visibilitychange', trackHiddenTime)
return (Date.now() - hiddenTimeDelta) - startTimestamp
}
}
We could write the following test code:
test('visibilitychange duration used by PayloadTiming', () => {
// jestDateMock.advanceTo(0) is
// needed due to issue: https://github.com/hustcc/jest-date-mock/issues/15
jestDateMock.advanceTo(0)
const timer = getTimeDuration()
jestDateMock.advanceBy(10) // visible state
jsdom.pretendToBeVisual = false // hidden state
document.dispatchEvent(new Event('visibilitychange'))
jestDateMock.advanceBy(10)
jsdom.pretendToBeVisual = true // visible state
document.dispatchEvent(new Event('visibilitychange'))
jestDateMock.advanceBy(10)
expect(timer()).toBe(20)
jestDateMock.clear()
})
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:11 (3 by maintainers)
Top Results From Across the Web
Document.hidden - Web APIs - MDN Web Docs
The Document.hidden read-only property returns a Boolean value indicating if the page is considered hidden or not.
Read more >How to Make Simple Feature Flags in React - Webtips
Feature flags — or feature toggles — is a technique where you can provide some means to turn on and off certain features...
Read more >Use Scroll and Width in JSDOM - Stack Overflow
It's default is to set things like document.hidden to indicate this, ... Jest has the ability to temporarily switch out the environment used ......
Read more >9. Feature Toggle - Programming JavaScript Applications [Book]
Feature Toggle Continuous deployment is the process of testing, integrating ... You can hide features with CSS or by wrapping the lines of...
Read more >Media queries in React for responsive design - Material UI - MUI
Some of the key features: ... It's performant, it observes the document to detect when its media queries ... For instance, jsdom doesn't...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@dotnetCarpenter try something like
Super old thread here, but is there a way to mock
visibilityState
? Would using the same form of @kapouer 's solution work?