admin管理员组文章数量:1415460
In a service I have the following code snippet
angular.element('html, body').animate({
scrollTop: this.parentHeight + ... - ...
}, 500);
In my unit test I want to check if the correct values are given to the animate
function. But how can I mock or spy this animate
function ? I can think of something like this:
beforeEach(() => {
angular.element = () => {
return { animate: (options) => { .. }
}
});
or better (but not working)
spyOn(angular.element('html, body'), 'animate');
Is there a better (angular) way to do this ?
In a service I have the following code snippet
angular.element('html, body').animate({
scrollTop: this.parentHeight + ... - ...
}, 500);
In my unit test I want to check if the correct values are given to the animate
function. But how can I mock or spy this animate
function ? I can think of something like this:
beforeEach(() => {
angular.element = () => {
return { animate: (options) => { .. }
}
});
or better (but not working)
spyOn(angular.element('html, body'), 'animate');
Is there a better (angular) way to do this ?
Share Improve this question edited Oct 25, 2017 at 15:50 Vadim Kotov 8,2848 gold badges50 silver badges63 bronze badges asked Oct 15, 2015 at 14:48 Jeanluca ScaljeriJeanluca Scaljeri 29.3k66 gold badges235 silver badges382 bronze badges1 Answer
Reset to default 4Something like this maybe?
var element = {
animate: null,
parentHeight: 100
};
spyOn(angular, 'element').andReturn(element);
spyOn(element, 'animate');
// Your test code goes here
expect(angular.element).toHaveBeenCalledWith('html, body');
expect(element.animate).toHaveBeenCalledWith(jasmine.objectContaining({
scrollTop: 100
}, 500);
本文标签: javascriptHow to mock angularelementStack Overflow
版权声明:本文标题:javascript - How to mock `angular.element` - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745235144a2649001.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论