solution This had nothing to do with the fact that the context is separated by iframes, it was the behavior around asynchronous
Explain with a minimum of code First, a simple component, setGlobal with useEffect (1) App.tsx
function App() {
const [fusens] = useGlobal("fusens");
console.log("render");
useEffect(() => {
console.log("useEffect");
window.movidea.callSetGlobal(); // (1)
}, []);
return (...);
}
At this time, after the component body runs, useEffect runs, in which it is setGlobal and redrawn when it detects it. :
render
useEffect
call setGlobal
render
Now comment out (1) and setGlobal on the test case side (2) test.js
describe('adjust font size', () => {
beforeEach(() => {
cy.visit('/')
cy.window().its('movidea').then(movidea => {
console.log("movidea resolved")
movidea.callSetGlobal() // (2)
});
})...
At this time, after the first drawing, setGlobal is called before useEffect is called, and this update is not detected :
render
movidea resolved
call setGlobal
useEffect
What happens if this call (2) is made asynchronous by interrupting setTimeout (3) test.js
describe('adjust font size', () => {
beforeEach(() => {
cy.visit('/')
cy.window().its('movidea').then(movidea => {
console.log("movidea resolved")
setTimeout(movidea.callSetGlobal, 0) // (3)
});
})...
In this case, setGlobal is called after useEffect, and the change is detected and redrawn as expected :
render
movidea resolved
useEffect
call setGlobal
render
So the work-around would be to make the state updates from the test case asynchronous, but it's a delicate solution...
--- log Testing Redux Store
I've tried this, which is the equivalent of explicitly creating a store in ReactN, but it doesn't work as expected. https://github.com/CharlesStover/reactn/blob/master/Provider.md
What we found out
What we don't know
Hmmm, I thought it would be nice to be able to test the display by tweaking the state from a test case...
This page is auto-translated from /nishio/✅CypressからsetGlobalするとフックが反応しない using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I'm very happy to spread my thought to non-Japanese readers.