EmberJS-Testing Interacting With Components



Description

Components are used to create the interactive custom HTML elements to test the methods of component and interaction with component.

Example

<!DOCTYPE html>
<html>
   <head>
      <title>Emberjs  Interacting With 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">
         {{my-comp}}
      </script>

      <script type="text/x-handlebars" data-template-name="components/my-comp">
         <h2>Welcome to Ember.js</h2>
         {{#each item in model}}
            <button {{action 'updateTitle'}}>Click Me</button>
      </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({
            title:'Tutorialspoint',
            actions:{
               updateTitle: function(){
                  this.set('title', 'Welcome to Tutorialspoint');
               }
            }
         });

         //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("clicking link updates the title", function(){
            //'this.subject()' is available because we used 'moduleForComponent'
            var component = this.subject();

            // append the component to the DOM
            this.append();

            // Here, it asserts the default state
            equal(find('h2').text(), 'Tutorialspoint');
            click('button');

            // Wait for async helpers to complete
            andThen(function() {
               equal(find('h2').text(), 'Welcome to Tutorialspoint');
            });
         });
      </script>
   </body>
</html>

Output

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

  • Save above code in testing_interact_component.htm file

  • Open this HTML file in a browser.

Advertisements