I'm trying to write e2e tests with Cypress and would like to dynamically be able to intantiate objects and use them as parameters for the invoke method.
The subject under test is a SPA with a redux store. For debugging (and possibly e2e testing), I have a window.debug object. The debug object exposes a dispatchAction(action: Action) method and properties that return classes, for instance Actions.SetMenu.
So on the browsers console I can call something like this to dispatch an action:debug.dispatchAction(new debug.Actions.SetMenu({ menu }))
For Cypress I need something like the following:
let setMenuAction;
cy.window()
.its('debug')
.then(debug => {
const SetMenu = debug.Actions.SetMenu;
setMenuAction = new SetMenu({ menu });
console.log(setMenuAction);
return debug;
})
.invoke('dispatchAction', setMenuAction);
But the above doesn't work. Is there a way to achieve what I'm trying?
Edit: Got it to work, was an async problem:
cy.window()
.its('debug.Actions.SetMenu')
.then(SetMenu => {
cy.window()
.its('debug')
.invoke('dispatchAction', new SetMenu({ menu }));
})