I am trying to test the output of an angular component.
I have a checkbox component that output its value using an EventEmitter. The checkbox component is wrapped in a storybook story for demo and testing purposes:
export const basic = () => ({
moduleMetadata: {
imports: [InputCheckboxModule],
},
template: `
<div style="color: orange">
<checkbox (changeValue)="changeValue($event)" [selected]="checked" label="Awesome">
</checkbox>
</div>`,
props: {
checked: boolean('checked', true),
changeValue: action('Value Changed'),
},
});
I am using an action to capture the value change and log it to the screen.
When writing a cypress e2e for this component however, I am only using the iFrame and not the whole storybook application.
I would like to find a way to test is the output is working. I tried using a spy on the postMessage method in the iFrame but that does not work.
beforeEach(() => {
cy.visit('/iframe.html?id=inputcheckboxcomponent--basic', {
onBeforeLoad(win) {
cy.spy(window, 'postMessage').as('postMessage');
},
});
});
and then the assertions would be:
cy.get('@postMessage').should('be.called');
Is there any other way how I could assert the (changeValue)="changeValue($event)"
has fired?
You're spying on
window.postMessage(), which is a method that enables cross-origin communication between window objects (pop-ups, pages, iframes, ...).The iFrame in Storybook doesn't communicate any messages to another window object, but you can install Kuker or another external web debugger on your application to spy on the messages between both and thus make the Cypress spy method working.
If you choose to install Kuker on your angular application, here is how to do it:
Add the Kuker Chrome extension as well to make it work.