You are testing the component.navigateTo()
method, so you should NOT use spyOn
for it. Otherwise, the window.parent.postMessage()
will never be called. You should use spyOn
for window.parent.postMessage()
method.
From the Default Spy Strategy
Normally, the default strategy is .and.stub()
, which returns undefined
if the spy is called.
So the correct test code should be:
example.component.ts
:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'example',
})
export class ExampleComponent implements OnInit {
ngOnInit() {}
navigateTo = (): void => {
window.parent.postMessage('NESTED_NAVIGATION', 'target_origin');
};
}
example.component.test.ts
:
import { ExampleComponent } from './example.component';
describe('70440045', () => {
it('should post message on click', async () => {
const component = new ExampleComponent();
let postMessageSpy: jasmine.Spy<(message: any, targetOrigin: string, transfer?: Transferable[]) => void> = spyOn(
window.parent,
'postMessage',
);
component.navigateTo();
expect(postMessageSpy).toHaveBeenCalledWith('NESTED_NAVIGATION', 'target_origin');
});
});
We specify the overload signature of postMessage
to avoid TS type errors.
Test result:
Chrome Headless 95.0.4638.69 (Mac OS 10.15.7): Executed 1 of 1 SUCCESS (0.008 secs / 0.004 secs)
TOTAL: 1 SUCCESS
=============================== Coverage summary ===============================
Statements : 94.12% ( 16/17 )
Branches : 100% ( 0/0 )
Functions : 62.5% ( 5/8 )
Lines : 100% ( 13/13 )
================================================================================
package version:
"@angular/core": "~11.0.3",
"@angular/cli": "~11.0.3",
"karma-jasmine": "~4.0.0",
"karma": "~5.1.0",
"jasmine-core": "^3.10.1"