GithubHelp home page GithubHelp logo

jasmine-sugar's Introduction

jasmine-sugar Build Status

Installation

Using Bower bower install jasmine-sugar or just copy needed helpers from lib directory

Ajax

This helper allows you to mock and control ajax requests flow. It's extended Pivotal's helper with a little more flexible API

describe('Twitter widget', function() {
  var Ajax = jasmine.Ajax, json;
  
  beforeEach(function() {
    Ajax.useMock();
    json = { data: [{ text: 'Hello', ...}, ...] };
  });
  
  it('shows twitter time-line', function() {
    Twitter.search();
    Ajax.respond(json);
    
    expect('.tweet').toExist();
  });

  it('shows error message if search failed', function() {
    Twitter.search();
    Ajax.respond({}, { status: 500 });

    expect('.error').toHaveText('Something went wrong');
  });
});

As you might noticed above you can use Ajax.respond (as well as Ajax.timeout and Ajax.abort) for the most recent ajax request. It's the same as you do Ajax.mostRecentCall.respond(json). As usually you can use specific call in a stack via Ajax.calls[3].respond(json). There's also alias response to respond.

Listening for future requests

Sometimes you might want to setup requests responses before running it. It's useful when u testing one piece of functionality that should always resolve same request. It allows you to setup response only once and later just write code in a synchronous manner

describe('Twitter widget', function() {
  beforeEach(function() {
    // Exact url match
    Ajax.listen('/feed', { data: [...] });

    // RegExp match
    Ajax.listen(/^auth\/(\d*)/, function(match) {
      return { id: match[1] };
    });
  });

  it('authorizes user and renders menu', function() {
    Twitter.auth(1, 'secret');
    expect('.user-menu').toExist();
  });

  it('renders feed', function() {
    Twitter.fetch(); // E.g. it runs request to `/feed` which immediately resolved with { data: [...] }
    expect('.tweet').toExist();
  });

  it('renders counter', function() {
    Twitter.fetch();
    expect('.count').toExist();
  });
});

Note that newly added listeners have higher priority, so you can always override top level listeners with more specific for nested specs.

Fixture

Simple helper to load either html or json with cache support

var json;

beforeEach(function() {
  json = jasmine.fixture('user.json');
  // Requests to '${jasmine.fixture.path}/user.json'
  // By default jasmine.fixture.path = '/base/spec/support/fixtures/' to work well with Karma (former testacular runner)
});

Sandbox

Reference to html element that is cleared before each spec so you can use it to render needed html

beforeEach(function() {
  var view = new View();
  view.render();
  view.$el.appendTo(jasmine.sandbox);
});

Dom

A bunch of dom matchers that useful to check element attributes and properties in a various ways. It's tightly coupled with jQuery

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.