Friday, January 13, 2012

my first Jasmine test!

I wrote widgetExample.js as described in my last posting. I got it under a Jasmine test by having the following three files sit in the same folder with it:

 
 

1. fixture.html:

<div id="wrapper">
   <input id="lefty" />
   <button type="button" id="mover">Move Right -></button>
   <input id="righty" />
</div>

 
 

2. SpecRunner.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

   <title></title>

   <script src="/Scripts/jquery-latest.js" type="text/javascript"></script>

   <script src="/Scripts/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>

   <script src="/Scripts/underscore.js" type="text/javascript"></script>

   <link rel="stylesheet" type="text/css" href="/Scripts/jasmine/jasmine.css" />

   <script type="text/javascript" src="/Scripts/jasmine/jasmine.js" ></script>

   <script type="text/javascript" src="/Scripts/jasmine/jasmine-html.js" ></script>

   <script type="text/javascript" src="/Scripts/jasmine/lib/jasmine-jquery.js" ></script>

   

   <!-- target script for testing -->

   <script type="text/javascript" src="widgetExample.js"></script>

   

   <!-- specs for target script for testing -->   

   <script type="text/javascript" src="widgetExample.spec.js"></script>

</head>

<body>

   <table>

      <tr>

         <td>

            <div id="tests">

               <script type="text/javascript">

                  $(document).ready(function () {

                     _.defer(function () {

                        setTimeout(function () {

                           jasmine.getEnv().addReporter(new jasmine.TrivialReporter());

                           jasmine.getEnv().execute();

                        },

                        1000);

                     });

                  });

               </script>

            </div>

         </td>

      </tr>

   </table>

</body>

</html>

 
 

3. widgetExample.spec.js:

//path relative to SpecRunner.html

jasmine.getFixtures().fixturesPath = './';

   

describe("excersise your component's behavior", function () {

   describe("clicking the button copies data appropriately", function () {

      it('copies data successfully', function () {

   

         //arrange

         loadFixtures('fixture1.html');

         var target = $('#wrapper');

         target.children("#lefty").val('foo');

         var widget = target.Example();

         

         //act

         widget.children("#mover").click();

         

         //assert

         expect(widget.children("#righty")).toHaveValue('foo');

      });

   });

});

No comments:

Post a Comment