EmberJS-Testing Validation In Components



Description

Components uses 'sendAction' to interact with the Ember application.

Example

<!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>

Output

Let's carry out the following steps to see how above code works −

  • Save above code in testing_validation.htm file

  • Open this HTML file in a browser.

Advertisements