EmberJS 组件中的测试验证

2018-01-04 14:19 更新

描述

组件使用'sendAction'与Ember应用程序交互。

例子

<!DOCTYPE html>
<html>
   <head>
      <title>Emberjs Validation In Components</title>
         <!-- CDN's -->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script>
      <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
      <script src="https://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.prod.js"></script>
      <script src="https://code.jquery.com/qunit/qunit-1.18.0.js"></script>
      <script src="https://rawgit.com/rwjblue/ember-qunit-builds/master/ember-qunit.js"></script>
      <script src="https://builds.emberjs.com/release/ember.debug.js"></script>
      <script src="https://builds.emberjs.com/beta/ember-data.js"></script>
   </head>
<body>
   <div id="qunit"></div>
   <div id="ember-testing"></div>

   <script type="text/x-handlebars" data-template-name="index">
       <h2>Action Not Specified</h2>
       <p>Tag: {{title}}</p>
       <p>{{my-comp title=title action="compFunc"}}</p>
   </script>

   <script type="text/x-handlebars" data-template-name="components/my-comp">
       <input type="button" value="Click me" {{action "compFunc"}}/>
   	</script>


   <script type="text/javascript">
      //Creates an instance of Ember.Application and assign it to a global variable
      App = Ember.Application.create();

      App.MyComponent = Em.Component.extend({
         //action not specified: error
         click: function() {
            this.set('title', "Tutorialspoint...");
            this.sendAction();
         }
      });

      //These methods are used to prepare the application for testing
      //emq.globalize();
      App.setupForTesting();
      App.injectTestHelpers();
      App.rootElement = '#ember-testing';   //Ember.js applications's root element
      setResolver(Ember.DefaultResolver.create({ namespace: App }));

      //Test the component done using the 'moduleForComponent' helper and will find the component by name(my-color)
      moduleForComponent('my-comp', 'MyComponent');
      test('trigger external action when button is clicked', function() {

         //'this.subject()' is available because we used 'moduleForComponent'
         var component = this.subject();

         //Defining the component dom instance
         var $component = this.append();

         var targetObject = {
            externalAction: function(){
               ok(true, 'external Action was called!');
            }
         };

         //External action to be called when button is clicked
         component.set('internalAction', 'externalAction');

         //Set the targetObject to our dummy object
         component.set('targetObject', targetObject);
         click('button');
      });
   </script>
</body>
</html>

输出

让我们执行以下步骤,看看上面的代码如何工作

  • 将上面的代码保存在testing_validation.html文件中

  • 在浏览器中打开此HTML文件。

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号