npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

kiwi-js

v0.2.5

Published

DRY interface for Angular.js tests in Jasmine/Karma.

Downloads

15

Readme

Kiwi.js

Travis   npm

  • npm: npm install kiwi-js

Purpose

Kiwi.js attempts to provide a DRY interface for your Jasmine/Karma unit tests – requiring less code to write your tests, and making them easier to read. It is a wrapper around the Jasmine test suite.

Dependencies

Example

In order to demonstrate Kiwi.js, consider your basic controller test which requires injecting your $rootScope, instantiating a new scope and then instantiating your controller with the scope instance:

inject(function($rootScope, controller) {

    var scope = $rootScope.$new();
    $controller('KiwiController', { scope: scope });

});

With Kiwi.js you can write the above code in one line and it is much more intuitive:

var controller = kiwi.controller.create('KiwiController');

Fixtures

Loading a fixture requires the karma-html2js-preprocessor module – follow the instructions there for modifying your Karma configuration file.

By default the fixtures are configured to point to a specific location – likely you'll want to change that, and that can be done in the beforeEach hook:

beforeEach(function() {

    kiwi.service.path('project/services/fixtures/{{name}}.json');
    kiwi.directive.path('project/directives/fixtures/{{name}}.html');
    kiwi.controller.path('project/controllers/fixtures/{{name}}.json');

});

Syntax-wise the path method accepts a string that is parsed using the $interpolate service and therefore the {{name}} will become the property that is passed in when you're reading a fixture.

Once you've configured your Karma configuration and paths above, you can load a fixture for a controller, service, or directive with ease:

var people = kiwi.controller.fixture('people');

Controllers

For a controller you require a scope that is injected into the controller, and all of the methods and properties reside on the scope.

Therefore you can instantiate your controller and immediately begin adding your assertions.

var controller = kiwi.controller.create('PeopleController');
expect(controller.data.length).toEqual(5);

Injecting dependencies can be achieved with the second argument of the create method – which accepts an object of dependencies:

var controller = kiwi.controller.create('PeopleController', {
    PeopleService: function PeopleServiceMock() {}
});

Services

Kiwi.js does not provide a way for services to be created since they exist as injectable entities. Instead Kiwi.js only provides a way to supply fixtures to services.

it('Should be able to inject the service;', inject(function(PeopleService) {
    expect(PeopleService).toBeDefined();
});

Fixtures for services behave in the same way as they do for the controllers.

Directives

Directives can be somewhat difficult in testing because they often have many scopes. Kiwi.js has the philosophy that if you can find the root scope of the directive, then finding child scopes are as simple as using the find method to traverse the DOM. Therefore Kiwi.js returns the root scope of the directive – whether it's an isolated scope directive or not.

var directive = kiwi.directive.create('pets');
expect(directive.scope).toBeDefined();
expect(directive.html).toBeDefined();
expect(typeof directive.html.find).toEqual('function');

From there you can affect the behaviour of the directive and respond accordingly. For example, to initiate a click event we can use Angular's triggerHandler method:

var directive = kiwi.directive.create('pets');
directive.html.triggerHandler('click');

In initiating the pets directive, the pets.html fixture will be read from the fixture path and then compiled with a directive.