Mock Window object in Cypress for Angular

115 views Asked by At

I'm trying to mock window object with additional property in cypress.

component.ts

public appCustDetails: IAppCustDetails = (<any>this.window).RunConfig.custDetails;

component.cy.ts

describe('AppComponent', () => {
  let __mock = {
    NgbModal: {},
    PageTitle: {},
    WindowMock: {},
    envProviders: {},
};

  let config: MountConfig<AppComponent>;

  beforeEach(() => {
    cy.window().then(win => {
   __mock.WindowMock = {
        onbeforeunload: cy.stub().returns({ result: Promise.resolve(null) }),
        location: {
          reload: cy.stub().returns({ result: Promise.resolve(null) }),
          href: cy.stub().returns({ result: Promise.resolve(null) }),
          origin: cy.stub().returns({ result: Promise.resolve(null) }),
        },
        RunConfig: {
          custDetails: mockCustDetails,
        },
      };
   });
config = {
      imports: [FormsModule, ReactiveFormsModule],
      declarations: [AppComponent],
      providers: [
        { provide: Window, useValue: __mock.WindowMock },
      ],
    };

  it('mount', () => {
    cy.mount(LoginComponent, config);
  });
});

Error

Cannot read properties of undefined (reading 'custDetails')

In same fashion i did for Jasmine spec. its working fine.

0

There are 0 answers