Currently experimenting with the method below:
startScriptLoad(): void {
const documentDefaultView = this.getDocumentDefaultView();
if (documentDefaultView) {
const twitterData: ICourseContentElementEmbedTweetWidgetData = this.getTwitterWidgetData() ?? {
// eslint-disable-next-line @typescript-eslint/no-empty-function
ready: () => {},
_e: [],
};
if (this.scriptExists()) {
((this.document.defaultView as unknown) as ICourseContentElementEmbedTweetDocument)[
this.TWITTER_OBJECT
] = twitterData;
return;
}
this.appendScriptToDOM();
twitterData._e = [];
twitterData.ready = (callback: () => void) => {
twitterData._e.push(callback);
};
((this.document.defaultView as unknown) as ICourseContentElementEmbedTweetDocument)[
this.TWITTER_OBJECT
] = twitterData;
}
}
Testing out the above with the following test scenario:
describe('startScriptLoad()', () => {
it('should load script', () => {
jest.spyOn(service, 'getDocumentDefaultView');
jest.spyOn(service, 'appendScriptToDOM');
service.startScriptLoad();
expect(service.getDocumentDefaultView).toHaveBeenCalled();
expect(service.appendScriptToDOM).toHaveBeenCalled();
});
});
I'm encountering issues getting coverage for these specific lines within startScriptLoad():
twitterData.ready = (callback: () => void) => {
twitterData._e.push(callback);
};
Any suggestions on how to potentially mock the callback method call?