Cypress - 以编程方式操作 Angular/NGRX 应用程序

Phi*_*hil 13 ngrx angular cypress

您如何以编程方式与 Cypress 的 Angular/NGRX 交互?cypress 文档似乎仅指 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
Run Code Online (Sandbox Code Playgroud)

这将是 React 方法;那么 Angular 呢?

Nic*_*asi 5

在 Angular 中,它几乎是一样的。在您AppComponent或您拥有商店的任何地方,您都可以执行以下操作:

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

然后,您可以创建自己的 Cypress 实用程序:

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

最后,您可以在 Cypress 测试中使用它:

dispatchAction(new MyAction()).then(() => {
     // Assert the side effect of your action
     // ...
     // cy.get('.name').should('exist');
});
Run Code Online (Sandbox Code Playgroud)