admin管理员组文章数量:1431927
I am trying to write some unit testing, but they are failing. Can you suggest me the right way to do?
show(contactId: string, phoneNumber: string, contactType: string, section: string, memberId: string) {
this.$window.onbeforeunload = () => "";
$('.disabledcalldialog').on("click", e => {
this.$window.alert('Please close call dialog and try.');
e.preventDefault();
});
I am trying to write some unit testing, but they are failing. Can you suggest me the right way to do?
show(contactId: string, phoneNumber: string, contactType: string, section: string, memberId: string) {
this.$window.onbeforeunload = () => "";
$('.disabledcalldialog').on("click", e => {
this.$window.alert('Please close call dialog and try.');
e.preventDefault();
});
My spec file is like
beforeEach(() => {
inject(($rootScope: ng.IScope, $window) => {
spyOn($window, 'onbeforeunload');
$(window).trigger('onbeforeunload');
});
it('should be able to call when changing URL', () => {
expect($window.onbeforeunload).toHaveBeenCalled();
Karma throwing error message like " TypeError: Unable to get property 'onbeforeunload' of undefined or null reference";
Share Improve this question asked Nov 7, 2015 at 23:02 JohnJohn 411 silver badge4 bronze badges2 Answers
Reset to default 4In your service:
setUpBeforeunload(): void {
window.addEventListener('beforeunload', this.closeConnection);
}
And onto unit testing with Jasmine:
describe('setUpBeforeunload', () => {
it('should set up window eventListener', () => {
spyOn(window, 'addEventListener');
service.setUpBeforeunload();
expect(window.addEventListener).toHaveBeenCalledWith('beforeunload', service.closeConnection);
});
it('should call closeConnection()', () => {
spyOn(service, 'closeConnection');
service.setUpBeforeunload();
window.dispatchEvent(new Event('beforeunload'));
expect(service.closeConnection).toHaveBeenCalled();
});
});
Have you tried:
$window.trigger('onbeforeunload');
instead of:
$(window).trigger('onbeforeunload');
本文标签: javascriptJasmine unit testing windowonbeforeunloadStack Overflow
版权声明:本文标题:javascript - Jasmine unit testing window.onbeforeunload - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745585762a2664895.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论