Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
ng-directive-compiler-helper
Advanced tools
helper function for easier angularJS 1.x directive compilation in unit tests
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.
npm
$ npm install ng-directive-compiler-helper --save-dev
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'
]
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:
compile(callbackFn)
;
* compile(parentScopeObject, callbackFn)
;
* compile(parentScopeObject, elementAttributesObject, callbackFn)
;most simple usage:
```js
compile((scope, element) => {
expect(scope).toBeDefined();
expect(element).toBeDefined();
});
```
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();
```
createCompiler
:let myDirectiveTemplate = '<my-directive></my-directive>';
let compile;
beforeEach(($rootScope, $compile) => {
compile = createCompiler(myDirectiveTemplate, $rootScope, $compile);
});
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');
});
});
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. 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:
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');
});
});
i use this helper thing to test one of mine angular projects, you can check here: argshook/orodarius
Please provide tests for pull requests.
Testing with karma:
npm run test
npm run test:watch
FAQs
helper function for easier angularJS 1.x directive compilation in unit tests
We found that ng-directive-compiler-helper demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.