Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress - programmatically manipulating Angular/NGRX app

How do you programmatically interact with Angular/NGRX from Cypress? The cypress docs seem to refer only to React: https://www.cypress.io/blog/2018/11/14/testing-redux-store/

// expose store when run in Cypress
if (window.Cypress) {
  window.store = store
}
cy
 .window()
 .its('store')
 .invoke('dispatch', { type: 'ADD_TODO', text: 'Test dispatch' })
// check if the app has updated its UI

This would be the React approach; so what about Angular?

like image 316
Phil Avatar asked Aug 18 '19 07:08

Phil


1 Answers

In Angular it is almost the same. In your AppComponent or wherever you have the store you can do something like:

// Expose the store
@Component({...})
export class AppComponent {
    constructor(private store: Store<AppState>){
        if(window.Cypress){
            window.store = this.store;
        }
    }
}

You can then create your own Cypress utility:

function dispatchAction(action: Action): Cypress.Chainable<any> {
    return cy.window().then(w => {
        const store = w.store;
        store.dispatch(action);
    });
}

And finally you can use it in a Cypress test:

dispatchAction(new MyAction()).then(() => {
     // Assert the side effect of your action
     // ...
     // cy.get('.name').should('exist');
});
like image 129
Nicola Tommasi Avatar answered Nov 17 '22 22:11

Nicola Tommasi