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

标签 angular ngrx 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

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

最佳答案

在 Angular 中,它几乎是一样的。在您的 AppComponent或者无论您有商店,您都可以执行以下操作:

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

然后,您可以创建自己的 Cypress 实用程序:
function dispatchAction(action: Action): Cypress.Chainable<any> {
    return cy.window().then(w => {
        const store = w.store;
        store.dispatch(action);
    });
}

最后,您可以在 Cypress 测试中使用它:
dispatchAction(new MyAction()).then(() => {
     // Assert the side effect of your action
     // ...
     // cy.get('.name').should('exist');
});

关于angular - Cypress - 以编程方式操作 Angular/NGRX 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57542554/

相关文章:

android - 无法编辑输入,除了 ionic 列表的第一个

javascript - Angular : SetData to HighChart widget from an array object

Marble 测试中的 Angular NgRx 效果错误 : Expected $. 长度 = 0 等于 2。/预期 $[0] = 未定义等于对象

TypeScript 类型不适用于扩展运算符

node.js - 创建项目后 Gatsby 开发不工作

Angular2 E2E Protractor 。等到元素有类

spring - CORS 预检请求 - header 中的自定义身份验证 token - Spring 和 Angular 4

angular - ngrx 效果 : Dispatch Empty Action

azure - 如何使用 Cypress.io 使用 MS Active Directory 登录?

javascript - 如何在 for 循环中跳过 cy.get() 函数