久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

AngularJS Karma 測試中的 jQuery 觸發事件

jQuery Trigger Event in AngularJS Karma Test(AngularJS Karma 測試中的 jQuery 觸發事件)
本文介紹了AngularJS Karma 測試中的 jQuery 觸發事件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試測試我正在編寫的新指令.但是,我似乎無法在 Karma/Jasmine 中使用 jQuery 觸發 keydown 事件.

I'm trying to test a new directive I'm writing. However, I can't seem to get the keydown event to trigger with jQuery inside Karma/Jasmine.

這是一個簡化版的測試:

Here is a simplified version of the test :

'use strict';

describe('', function() {

  var $compile;
  var $scope;

  beforeEach(inject(function(_$compile_, _$rootScope_) {
    $compile = _$compile_;
    $scope = _$rootScope_.$new();
  }));

  describe('Getting Trigger To Work', function() {

    it('Should Trigger a KeyDown Event', function() {

      var el = $compile('<form name="testing"><input id="field1" name="testfield" ng-model="result" type="text" ng-minlength="5" ng-maxlength="50"/></form>')($scope);
      console.log(el);

      var inputEl = el.find('input');
      console.log(inputEl);

      var e = $.Event('keydown');
      e.which = 65;

      inputEl.trigger(e);

    });

  });

});

我的 karma 配置包括 AngularJS 和 jQuery.這是該部分:

My karma config includes AngularJS and jQuery. Here's that section:

//
files: [
  'lib/angular.js',
  'lib/angular-mocks.js',
  'lib/jquery-1.10.2.min.js',
  'test/**/*Spec.js'
],

當我運行測試時,我得到一個錯誤,我的輸入元素沒有觸發方法:

When I run the test, I get an error that my input element has no trigger method :

INFO [watcher]: Changed file "/Volumes/Macintosh HD/dev_libraries/val-on-timeout/test/valOnTimeoutSpec.js".
LOG: Object{0: <form name="testing" class="ng-scope ng-pristine ng-valid"><input id="field1" name="testfield" ng-model="result" type="text" ng-minlength="5" ng-maxlength="50" class="ng-pristine ng-valid"></form>, length: 1}
LOG: Object{0: <input id="field1" name="testfield" ng-model="result" type="text" ng-minlength="5" ng-maxlength="50" class="ng-pristine ng-valid">, length: 1}
Chrome 32.0.1700 (Mac OS X 10.8.5)  testing valOnTimeout Should load FAILED
    TypeError: Object [object Object] has no method 'trigger'
        at null.<anonymous> (/Volumes/Macintosh HD/dev_libraries/val-on-timeout/test/valOnTimeoutSpec.js:79:21)
Chrome 32.0.1700 (Mac OS X 10.8.5): Executed 1 of 1 (1 FAILED) ERROR (0.327 secs / 0.033 secs)

一開始,你可能會認為 jQuery 是不包括在內的.但是,它包括在內.如果不是,則測試將在 $.Event 處失敗.

At first, you may think that jQuery is not included. However, it is included. If it were not, the test would fail at $.Event.

關于讓 keydown 事件對輸入起作用的建議?

Suggestions on getting the keydown event to work on the input?

推薦答案

我希望這可行

  var e = $.Event('keydown');
  e.which = 65;

  $(inputEl).trigger(e); // convert inputEl into a jQuery object first
  // OR
  angular.element(inputEl).triggerHandler(e); // angular.element uses triggerHandler instead of trigger to trigger events

這篇關于AngularJS Karma 測試中的 jQuery 觸發事件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發技術
What do jasmine runs and waitsFor actually do?(jasmine 運行和等待實際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈式方法進行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 综合九九 | 日本黄色片免费在线观看 | 久久精品久久久久久 | 女朋友的闺蜜3韩国三级 | 成人在线中文字幕 | 亚洲欧美日韩电影 | 黄色网毛片 | 日韩一级免费电影 | 亚洲精选一区 | 91精品久久久久久久久中文字幕 | 日日人人 | 欧美日韩亚洲在线 | 久久久国产精品一区 | www国产亚洲精品久久网站 | 九九热这里只有精品在线观看 | 一区二区三区欧美 | 懂色一区二区三区免费观看 | 欧美黄色一级毛片 | 国产午夜精品久久久 | 国产精品久久久久久久久久久久冷 | 国产视频精品在线 | 涩涩导航| 午夜成人在线视频 | 亚洲国产欧美在线 | 中文字幕av网站 | 亚洲日韩中文字幕一区 | a免费视频 | 日本超碰| 国产日韩精品一区二区三区 | 国产精品视频久久久久 | 久久久国产一区二区三区四区小说 | 成人免费视频网站在线看 | 日韩伦理电影免费在线观看 | 欧美日韩国产在线 | 亚洲小视频在线观看 | 天堂一区在线观看 | 婷婷一级片 | 精品久久久久久久 | 国产精品免费看 | 91在线视频播放 | 中文字幕国产一区 |