Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
angularjs-enzyme
Advanced tools
[![npm](https://img.shields.io/npm/v/angularjs-enzyme.svg)](https://www.npmjs.com/package/angularjs-enzyme) [![GitHub release](https://img.shields.io/github/release/oliverviljamaa/angularjs-enzyme.svg)](https://github.com/oliverviljamaa/angularjs-enzyme/r
Unit testing utility for AngularJS (1.x), heavily inspired by the wonderful Enzyme API. :heart:
Therefore, it is well suited for organisations and individuals moving from AngularJS to React. It is test framework and runner agnostic, but the examples are written using Jest syntax.
An example showing the utility in use can be found here.
Available methods:
mount
mockComponent
Returned classes:
TestElementWrapper
mock
npm install angularjs-enzyme --save-dev
import { mount, mockComponent } from 'angularjs-enzyme';
node_modules/angularjs-enzyme/dist/angularjs-enzyme.js
.angularjsEnzyme
.mount(template[, props]) => TestElementWrapper
Mounts the template
(String
) with optional props
(Object
) and returns a TestElementWrapper
with numerous helper methods. The props are attached to the $ctrl
available in the template scope.
import 'angular';
import 'angular-mocks';
import { mount } from 'angularjs-enzyme';
describe('Component under test', () => {
const TEMPLATE = `
<h1>{{ $ctrl.title }}</h1>
<p>{{ $ctrl.text }}</p>
`;
let component;
beforeEach(() => {
angular.mock.module('moduleOfComponentUnderTest');
component = mount(TEMPLATE, { title: 'A title', text: 'Some text' });
});
});
mockComponent(name) => mock
By default, AngularJS renders the whole component tree. This function mocks a child component with name
(String
) in the component under test and returns a mock
. The child component won't be compiled and its controller won't be invoked, enabling testing the component under test in isolation. In addition, the returned mock
has methods useful for testing.
import 'angular';
import 'angular-mocks';
import { mockComponent } from 'angularjs-enzyme';
describe('Component under test', () => {
let childComponent;
beforeEach(() => {
angular.mock.module('moduleOfComponentUnderTest');
childComponent = mockComponent('child-component'); // ⇦ after module, before inject
});
});
TestElementWrapper
API.length => Number
The number of elements in the wrapper.
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
it('has three list items', () => {
expect(component.find('li').length).toBe(3);
});
.html() => String
Returns HTML of the wrapper. It should only be used for logging purposes, in tests other methods should be preferred.
<h1>Some title</h1>
it('renders title as html', () => {
expect(component.html()).toBe('<h1>Some title</h1>');
});
.text() => String
<h1>Some title</h1>
<p>Some text</p>
it('has paragraph text', () => {
expect(component.find('p').text()).toBe('Some text');
});
.hasClass(className) => Boolean
Returns whether the wrapper has a class with className
(String
) or not.
<button class="success">Pay</button>
it('has success class', () => {
expect(component.find('button').hasClass('success')).toBe(true);
});
it('does not have error class', () => {
expect(component.find('button').hasClass('error')).toBe(false);
});
.exists() => Boolean
Returns whether or not the wrapper contains any elements.
<button>Pay</button>
it('has button', () => {
expect(component.find('button').exists()).toBe(true);
});
it('does not have link', () => {
expect(component.find('a').exists()).toBe(false);
});
.find(selector) => TestElementWrapper
Returns a TestElementWrapper
(for chaining) with every element matching the selector
(String
).
<div class="left">
<a href="https://neopets.com">Wrong</a>
<a href="https://transferwise.com">Wrong</a>
</div>
<div class="right">
<a href="https://neopets.com">Wrong</a>
<a href="https://transferwise.com">Correct</a>
</div>
it('has one transferwise link with corrext text on the right', () => {
const link = component.find('.right a[href="https://transferwise.com"]');
expect(link.length).toBe(1);
expect(link.text()).toBe('Correct');
});
.map(fn) => Array<Any>
Maps the nodes in the wrapper to another array using fn
(Function
).
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
it('has three list items with their number as a word', () => {
const items = component.find('li');
expect(items.map(item => item.text())).toEqual(['One', 'Two', 'Three']);
});
.props() => Object
Returns all wrapper props/attributes.
<a href="https://transferwise.com" target="_blank">Send money</a>
it('has transferwise link that opens in a new tab', () => {
expect(component.find('a').props()).toEqual({
href: 'https://transferwise.com',
target: '_blank',
});
});
.prop(key) => String
Returns wrapper prop/attribute value with provided key
(String
).
<a href="https://transferwise.com">Send money</a>
it('has transferwise link', () => {
expect(component.find('a').prop('href')).toBe('https://transferwise.com');
});
.simulate(event[, data]) => Self
Calls an event handler on the wrapper for passed event
with data
(optional) and returns wrapper for chaining.
NOTE: event
should be written in camelCase and without the on
present in the event handler name. Currently, change
and click
events are supported, with change
requiring an event format.
<input ng-model="$ctrl.text" />
<p>{{ $ctrl.text }}</p>
<button ng-click="$ctrl.onClick({ $event: $ctrl.text })">Click me</button>
let component;
let onClick;
beforeEach(() => {
onClick = jest.fn();
component = mount(
`
<some-component
text="$ctrl.text"
on-click="$ctrl.onClick($event)"
></some-component>
`,
{ text: 'Original text', onClick },
);
});
it('calls click handler on button click', () => {
const button = component.find('button');
expect(onClick).not.toBeCalled();
button.simulate('click');
expect(onClick).toBeCalledWith('Original text');
});
it('changes text on input change', () => {
const input = component.find('input');
const text = () => component.find('p').text();
expect(text()).toBe('Original text');
input.simulate('change', { target: { value: 'New text' } });
expect(text()).toBe('New text');
});
.setProps(props) => Self
Merges props
(Object
) with existing props and updates view to reflect them, returning itself for chaining.
<h1>{{ $ctrl.title }}</h1>
<p>{{ $ctrl.text }}</p>
it('changes title and text when props change', () => {
const component = mount(
`
<some-component
title="$ctrl.title"
text="$ctrl.text"
></some-component>
`,
{ title: 'Original title', text: 'Original text' },
);
const title = () => component.find('h1').text();
const text = () => component.find('p').text();
expect(title()).toBe('Original title');
expect(text()).toBe('Original text');
component.setProps({ title: 'New title', text: 'New text' });
expect(title()).toBe('New title');
expect(text()).toBe('New text');
});
mock
API.exists() => Boolean
Returns whether or not the mocked component exists in the rendered template.
let component;
beforeEach(() => {
component = mount(`
<button ng-click="$ctrl.show = !$ctrl.show">
Show child
</button>
<child-component ng-if="$ctrl.show"></child-component>
`);
});
it('allows toggling child component', () => {
const button = component.find('button');
expect(childComponent.exists()).toBe(false);
button.simulate('click');
expect(childComponent.exists()).toBe(true);
button.simulate('click');
expect(childComponent.exists()).toBe(false);
});
.props() => Object
Returns all mocked component props.
let component;
beforeEach(() => {
component = mount(`
<div>Something else</div>
<child-component
some-prop="'A string'",
some-other-prop="12345"
></child-component>
`);
});
it('passes props to child component', () => {
expect(childComponent.props()).toEqual({
someProp: 'A string',
someOtherProp: 12345,
});
});
.prop(key) => Any
Returns mocked component prop value with the provided key
(String
).
let component;
beforeEach(() => {
component = mount(`
<div>Something else</div>
<child-component some-prop="'A string'"></child-component>
`);
});
it('passes some prop to child component', () => {
expect(childComponent.prop('someProp')).toBe('A string');
});
.simulate(event[, data]) => Self
Calls an event handler on the mocked component for passed event
with data
(optional) and returns mocked component for chaining.
NOTE: event
should be written in camelCase and without the on
present in the event handler name. So, to call onSomePropChange
, .simulate('somePropChange')
should be used.
it('calls parent component with data when child component is called', () => {
const onSomePropChange = jest.fn();
mount(
`
<div>Something else</div>
<child-component
on-some-prop-change="onSomePropChange($event)"
></child-component>
`,
{ onSomePropChange } // ⇦ props for component under test
);
expect(onSomePropChange).not.toBeCalled();
childComponent.simulate('somePropChange', 'New value');
expect(onSomePropChange).toBeCalledWith('New value');
});
npm run test:watch
. npm test
will check for package and changelog version match, ESLint and Prettier format in addition.package.json
according to semver and add an item that a release will be based on to CHANGELOG.md
.package.json
.For features and bugs, feel free to add issues or contribute.
v1.0.3
FAQs
[![npm](https://img.shields.io/npm/v/angularjs-enzyme.svg)](https://www.npmjs.com/package/angularjs-enzyme) [![GitHub release](https://img.shields.io/github/release/oliverviljamaa/angularjs-enzyme.svg)](https://github.com/oliverviljamaa/angularjs-enzyme/r
The npm package angularjs-enzyme receives a total of 4 weekly downloads. As such, angularjs-enzyme popularity was classified as not popular.
We found that angularjs-enzyme 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.