Code Monkey home page Code Monkey logo

ngmidwaytester's Introduction

ngMidwayTester Build Status

A pure-javascript integration tester for AngularJS that can be run inline with application code.

Installation

  1. run npm install ng-midway-tester.
  2. include ./node_modules/ngMidwayTester/src/ngMidwayTester.js into your test runner.

Getting Started

Inside of your test spec code, you can use the midway tester like so:

//creating the tester
var tester = ngMidwayTester('moduleName', { /* options */ });

//destroying the tester
tester.destroy();
tester = null;

Be sure to have a new instance of the tester for each spec you're testing.

Example

//this is the module code
angular.module('myModule', ['ngRoute'])
  .config(function($routeProvider) {
    $routeProvider.when('/home', {
      template : 'welcome to {{ title }}',
      controller : 'HomeCtrl'
    });
  })
  .controller('HomeCtrl', function($scope) {
    $scope.title = 'my home page';
  });

//this test spec uses mocha since it has nice support for async testing...
describe('my test spec', function() {
  var tester;
  beforeEach(function() {
    tester = ngMidwayTester('myModule');
  });

  afterEach(function() {
    tester.destroy();
    tester = null;
  });

  it('should have a working home page', function(done) {
    tester.visit('/home', function() {
      expect(tester.path()).to.equal('/home');
      expect(tester.viewElement().html()).to.contain('welcome to my home page');

      var scope = tester.viewScope();
      expect(scope.title).to.equal('my home page');
      done();
    });
  });
});

Downloading Assets

The benefit to using the midway tester is that it will act like an AngularJS application and work around asset mocking. This means that if your code contains a remote XHR call to an asset file then the midway tester will download it just as normal. However, due to the mechanics of Karma, you'll need to assign a proxy which assigns the root path to point to a server hosting your website.

In your Karma settings, it should look something like this:

proxies: {
  //point this to the root of where your AngularJS application
  //is being hosted locally
  '/': 'http://localhost:8844/app'
}

When you start your test runner be sure to have the other server (in this case localhost:8844) running in the background. This can be easily handled in the same terminal window using grunt-contrib-connect and setting keepAlive:false).

Setting the index.html file

Just like an AngularJS application, your test runner expects to have an index.html file. However simply using the same index.html provided in your application code will not work since the JavaScript dependencies are loaded via karma. Therefore to get this to function properly, you need to specify your own HTML to act as the index.html layout code for the test page. This can be accomplished in two ways: using raw HTML directly or referencing a remote HTML file.

Raw HTML

Instantiate the tester using the configuration object using template:

var tester = ngMidwayTester('myApp', {
  template : '<div>' +
             '  <h1>hello</h1>' +
             '  <div id="view-container">' +
             '    <div ng-view></div>' +
             '  </div>' +
             '</div>'
});

Remote HTML Template File

Instantiate the tester using the configuration object using templateUrl:

var tester = ngMidwayTester('myApp', {
  templateUrl : '/test/test-index-template.html'
});

In order to make remote assets work with Karma as expected, you will need to setup a proxy which points the root path to a server pointing it's root to your application directory. This is explained in the previous section in the README.

Default HTML Template Code

The default HTML code for the midway tester looks as follows:

<div><div ng-view></div></div>

This will be used if a configuration object is not provided or either the template properties are not set.

Docs

http://yearofmoo.github.io/ngMidwayTester/docs/classes/ngMidwayTester.html

License

MIT

ngmidwaytester's People

Contributors

matsko avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.