Git Product home page Git Product logo

jasmine-mox-matchers's Introduction

Jasmine Mox Matchers

Build Status Test Coverage Code Climate devDependency Status Dependency Status

This package include some matchers for testing Angular promises without having to $digest manually and two other useful matchers. They were made for the Resource testing DSL in Mox, but also very useful when included separately.

  • Promise matchers
  • Matcher for testing query parameters
  • Matcher for directive isolate scope testing

Installing

bower install jasmine-mox-matchers --save-dev or npm install jasmine-mox-matchers.

Include src/jasmine-mox-matchers.js or dist/jasmine-mox-matchers.min.js file in the files list of your test runner config files. Or when your are using ES6 modules: `import { jasmineMoxMatchers } from 'jasmine-mox-matchers';

beforeEach(function () {
  this.addMatchers(jasmineMoxMatchers.v1); // Jasmine 1.x
  jasmine.addMatchers(jasmineMoxMatchers.v2); // Jasmine 2.x
});

Documentation

Promise matchers

Promises are a powerful concept in Javascript but somewhat hard to test. Your test case usually may look like this:

SomeService.getData() // Returns a promise that resolves to 'data'
  .then(function success(result) {
    expect(result).toEqual('data');
  });

With the promise matchers, this is all you need to do:

  expect(SomeService.getData()).toResolveWith('data');

Apart from the 3 lines of boilerplate code, the usual way of testing does not guarantee that your promise will resolve! If the promise does not resolve or rejects, the expect will not be called and the test passes because the expect statement is not called.

Note that a lot of promise matchers on Github still work this way!

In short, these promise matchers are really clean to use and have a correctly implemented 'failing' case.

Query parameter matcher

Test if query parameters exist in a certain URL.

expect('path?param1=param1').toHaveQueryParams({ param1: 'param1' });

Strict matching is also supported by passing true as second argument.

var path = 'path?param1=param1&param2=param2';
expect(path).toHaveQueryParams({ param1: 'param1' }, true); // This fails
expect(path).toHaveQueryParams({ param1: 'param1', param2: 'param2' }, true); // This passes

Directive matcher

When you mock a directive away, you still want to test of scope vars are passed to the directive correctly. This can be tested by testing the directive attribute. These attributes usually are models or expressions, so you will be testing the literal value of the attribute. It is better to test the evaluated value, which can be done by testing the isolate scope of the directive.

$scope.list = ['first', 'second'];
$scope.title = 'The title';
var element = $compile('<div directive-name="list" title="title"></div>')($scope);
$scope.$digest();
expect(element).toContainIsolateScope({
  directiveName: $scope.list,
  title: $scope.title
});

API

toBePromise()

Tests if a given object is a promise object. The Promises/A spec (http://wiki.commonjs.org/wiki/Promises/A) only says it must have a function 'then', so, I guess we'll go with that for now.

expect(promise).toBePromise();

toResolve() / toHaveBeenResolved()

Asserts that a Promise is resolved.

expect(promise).toResolve();

toResolveWith() / toHaveBeenResolved()

Verifies that a Promise is resolved with the specified argument.

expect(promise).toResolveWith('something');

If you pass a function, you can use that as callback to get the resolved value and some some further assertions on it.

expect(promise).toResolveWith(function (data) { // Checks only if the promise resolves
  expect(data.length).toBe(2); // Do further assertions
});

toReject() / toHaveBeenRejected()

Asserts that a Promise is rejected before the end of the test.

expect(promise).toReject();

toRejectWith() / toHaveBeenRejectedWith()

Asserts that a Promise is rejected with the specified argument.

expect(promise).toRejectWith('something');

If you pass a function, you can use that as callback to get the resolved value and some some further assertions on it.

expect(promise).toRejectWith(function (data) { // Checks only if the promise rejects
  expect(data.message).toBe('Error fetching data'); // Do further assertions
});

toContainIsolateScope()

Asserts that the passed key/value pairs are on the isolate scope of the element.

expect(element).toContainIsolateScope({ bindingKey: 'value' });

Development

  • npm install

Run npm run to see available tasks.

jasmine-mox-matchers's People

Contributors

dependabot[bot] avatar fvanwijk avatar greenkeeper[bot] avatar

Watchers

 avatar  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.