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

ng-directive-compiler-helper

v1.0.0

Published

helper function for easier angularJS 1.x directive compilation in unit tests

Downloads

25

Readme

Build Status

Helper for easier directive compiling

Small function to ease testing Angular 1.x directives.

Usually you would have to set up directive compiler, give it a template, manage its attributes, parent or isolate scopes. This helper abstracts these things so you can focus on tests.

Installing

  1. install through npm
$ npm install ng-directive-compiler-helper --save-dev
  1. include in test runner:

if using karma as test runner, make sure the following is in karma.conf.js:

files: [
  'node_modules/ng-directive-compiler-helper/lib/ng-directive-compiler-helper.js'
]

Quick reference

After including this package you will be able to use a global createCompiler function. It requires directive template, $rootScope and $compile services:

compile = createCompiler(templateString, $rootScope, $compile)

compile is now a function which can be used in two ways:

  1. using callbackFn which is called after directive is compiled. callbackFn is passed with scope and element arguments * compile(callbackFn); * compile(parentScopeObject, callbackFn); * compile(parentScopeObject, elementAttributesObject, callbackFn);
most simple usage:

```js
compile((scope, element) => {
  expect(scope).toBeDefined();
  expect(element).toBeDefined();
});
```
  1. using returned object which contains scope and element properties: * let compiled = compile(); * let compiled = compile(parrentScopeObject); * let compiled = compile(parrentScopeObject, elementAttributesObject);
most simple usage:

```js
expect(compile().scope).toBeDefined();
expect(compile().element).toBeDefined();
```

More usage examples:

  1. setup compiler first using createCompiler:
let myDirectiveTemplate = '<my-directive></my-directive>';
let compile;

beforeEach(($rootScope, $compile) => {
  compile = createCompiler(myDirectiveTemplate, $rootScope, $compile);
});
  1. use created compiler in tests:
it('should compile', () => {
  compile((scope, element) => {
    expect(scope).toBeDefined();
    // etc...
  });
});
// adjust parent scope
it('should have parent scope values', () => {
  compile({ parentScopeValue: true }, (scope, element) => {
    expect(scope.parentScopeValue).toBe(true);
  });
});
// adjust directive element attributes
it('should have additional attributes', () => {
  // first param === parentScope, empty in this case
  // note that attribute properties are kebab-case and not camelCase!
  compile({}, { 'new-attribute': 'hello' }, (scope, element) => {
    expect(element.attr('new-attribute')).toBe('hello');
  });
});
  1. working with isolate scope directives:
it('should set isolate scope properties from attributes', () => {
  // note that attribute properties are kebab-case and not camelCase!
  compile({}, { 'isolate-scope-attribute': 'hello' }, scope => {
    expect(scope.isolateScopeAttribute).toBe('hello');
  });
});
  1. working with drivers
// 1. define driver
let driver = {
  parent: e => e.find('.imaginary-parent-with-3-children'); // e - reference to element, passed if no other arguments given,
  children: parent => parent.children;
  alsoChildren: function() { return this.$.children; } // this.$ - also reference to element
};

// 2. hook driver when creating compiler (as last argument)
let compile = createCompiler(templateString, $rootScope, $compile, driver);

// 3. use in tests
it('should contain 3 items', () => {
  compile(function(scope, element, driver) { // <-- driver is passed as third argument
    expect(driver.parent().length).toBe(1);
    expect(driver.children(element).length).toBe(3);
    expect(driver.alsoChildren().length).toBe(3);
  })
});

testing like this should be cool because:

  • driver can be reused for multiple tests, drying up the test suite
  • no need to repeat selectors everywhere
  • other more complicated logic can be reused (e.g. do some component setup for assertions)

Few notes about drivers

  • if driver method is called without arguments, it automatically gets element reference (but ONLY if there are no arguments given):

    Note: the following examples assume you have let compile = createCompiler setup with driver.

    let driver = {
      myTitle: e => e.find('.title-element')
    }
    
    it('should have title', () => {
      compile((scope, element, driver) {
        expect(driver.myTitle().text()).toBe('some text');
      });
    });
  • if driver method is called with arguments, element reference is available through this.$:

    let driver = {
      myListItem: function(n) {
        return this.$.find('.my-list').get(n);
      }
    };
    
    it('should have correct item', () => {
      compile(function(scope, element, driver) {
        expect(driver.myListItem(2).text()).toBe('my list item #3');
      });
    });

More examples

i use this helper thing to test one of mine angular projects, you can check here: argshook/orodarius

Contributing

Please provide tests for pull requests.

Testing with karma:

  • single run npm run test
  • watch tests npm run test:watch