Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

angularjs-enzyme

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

angularjs-enzyme - npm Package Compare versions

Comparing version 0.2.0 to 0.2.1

3

CHANGELOG.md

@@ -0,1 +1,4 @@

# v0.2.1
## Fix readme
# v0.2.0

@@ -2,0 +5,0 @@ ## Adds `mount`

2

package.json
{
"name": "angularjs-enzyme",
"version": "0.2.0",
"version": "0.2.1",
"main": "index.js",

@@ -5,0 +5,0 @@ "files": [

@@ -1,505 +0,505 @@

# :rotating_light: AngularJS Enzyme
[![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/releases) [![CircleCI](https://img.shields.io/circleci/project/github/oliverviljamaa/angularjs-enzyme/master.svg)](https://circleci.com/gh/oliverviljamaa/angularjs-enzyme) [![npm](https://img.shields.io/npm/l/angularjs-enzyme.svg)](https://github.com/oliverviljamaa/angularjs-enzyme/blob/master/LICENSE)
# :rotating_light: AngularJS Enzyme
[![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/releases) [![CircleCI](https://img.shields.io/circleci/project/github/oliverviljamaa/angularjs-enzyme/master.svg)](https://circleci.com/gh/oliverviljamaa/angularjs-enzyme) [![npm](https://img.shields.io/npm/l/angularjs-enzyme.svg)](https://github.com/oliverviljamaa/angularjs-enzyme/blob/master/LICENSE)
Unit testing utility for [AngularJS (1.x)](https://angularjs.org/), heavily inspired by the wonderful [Enzyme](http://airbnb.io/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](https://github.com/facebook/jest) syntax.
Unit testing utility for [AngularJS (1.x)](https://angularjs.org/), heavily inspired by the wonderful [Enzyme](http://airbnb.io/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](https://github.com/facebook/jest) syntax.
[**An example showing the utility in use can be found here.**](example.test.js)
[**An example showing the utility in use can be found here.**](example.test.js)
Available methods:
[`mount`](#mounttemplate-props--testelementwrapper)
[`mockComponent`](#mockcomponentname--mock)
Available methods:
[`mount`](#mounttemplate-props--testelementwrapper)
[`mockComponent`](#mockcomponentname--mock)
Returned classes:
[`TestElementWrapper`](#testelementwrapper-api)
[`mock`](#mock-api)
Returned classes:
[`TestElementWrapper`](#testelementwrapper-api)
[`mock`](#mock-api)
## Usage
## Usage
```bash
npm install angularjs-enzyme --save-dev
```
```bash
npm install angularjs-enzyme --save-dev
```
### Module context
### Module context
```js
import { mount, mockComponent } from 'angularjs-enzyme';
```
```js
import { mount, mockComponent } from 'angularjs-enzyme';
```
### Non-module context
### Non-module context
1. Include the script from `node_modules/angularjs-enzyme/dist/angularjs-enzyme.js`.
2. Use the utility from the global context under the name `angularjsEnzyme`.
1. Include the script from `node_modules/angularjs-enzyme/dist/angularjs-enzyme.js`.
2. Use the utility from the global context under the name `angularjsEnzyme`.
## API
## API
### `mount(template[, props]) => TestElementWrapper`
### `mount(template[, props]) => TestElementWrapper`
Mounts the `template` (`String`) with optional `props` (`Object`) and returns a [`TestElementWrapper`](#testelementwrapper-api) with numerous helper methods. The props are attached to the `$ctrl` available in the template scope.
Mounts the `template` (`String`) with optional `props` (`Object`) and returns a [`TestElementWrapper`](#testelementwrapper-api) with numerous helper methods. The props are attached to the `$ctrl` available in the template scope.
<details>
<summary>Example</summary>
<details>
<summary>Example</summary>
```js
import 'angular';
import 'angular-mocks';
import { mount } from 'angularjs-enzyme';
```js
import 'angular';
import 'angular-mocks';
import { mount } from 'angularjs-enzyme';
describe('Component under test', () => {
const TEMPLATE = `
<h1>{{ $ctrl.title }}</h1>
<p>{{ $ctrl.text }}</p>
`;
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' });
});
let component;
beforeEach(() => {
angular.mock.module('moduleOfComponentUnderTest');
component = mount(TEMPLATE, { title: 'A title', text: 'Some text' });
});
```
});
```
</details>
</details>
### `mockComponent(name) => mock`
### `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`](#mock-api). 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.
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`](#mock-api). 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.
<details>
<summary>Example</summary>
<details>
<summary>Example</summary>
```js
import 'angular';
import 'angular-mocks';
import { mockComponent } from 'angularjs-enzyme';
```js
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
});
describe('Component under test', () => {
let childComponent;
beforeEach(() => {
angular.mock.module('moduleOfComponentUnderTest');
childComponent = mockComponent('child-component'); // ⇦ after module, before inject
});
```
});
```
</details>
</details>
### `TestElementWrapper` API
### `TestElementWrapper` API
#### `.length => Number`
#### `.length => Number`
The number of elements in the wrapper.
The number of elements in the wrapper.
<details>
<summary>Example</summary>
<details>
<summary>Example</summary>
```html
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
```
```html
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
```
```js
it('has three list items', () => {
expect(component.find('li').length).toBe(3);
});
```
```js
it('has three list items', () => {
expect(component.find('li').length).toBe(3);
});
```
</details>
</details>
#### `.html() => String`
#### `.html() => String`
Returns HTML of the wrapper. It should only be used for logging purposes, in tests other methods should be preferred.
Returns HTML of the wrapper. It should only be used for logging purposes, in tests other methods should be preferred.
<details>
<summary>Example</summary>
<details>
<summary>Example</summary>
```html
<h1>Some title</h1>
```
```html
<h1>Some title</h1>
```
```js
it('renders title as html', () => {
expect(component.html()).toBe('<h1>Some title</h1>');
});
```
```js
it('renders title as html', () => {
expect(component.html()).toBe('<h1>Some title</h1>');
});
```
</details>
</details>
#### `.text() => String`
#### `.text() => String`
<details>
<summary>Example</summary>
<details>
<summary>Example</summary>
```html
<h1>Some title</h1>
<p>Some text</p>
```
```html
<h1>Some title</h1>
<p>Some text</p>
```
```js
it('has paragraph text', () => {
expect(component.find('p').text()).toBe('Some text');
});
```
```js
it('has paragraph text', () => {
expect(component.find('p').text()).toBe('Some text');
});
```
</details>
</details>
#### `.hasClass(className) => Boolean`
#### `.hasClass(className) => Boolean`
Returns whether the wrapper has a class with `className` (`String`) or not.
Returns whether the wrapper has a class with `className` (`String`) or not.
<details>
<summary>Example</summary>
<details>
<summary>Example</summary>
```html
<button class="success">Pay</button>
```
```html
<button class="success">Pay</button>
```
```js
it('has success class', () => {
expect(component.find('button').hasClass('success')).toBe(true);
});
```js
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);
});
```
it('does not have error class', () => {
expect(component.find('button').hasClass('error')).toBe(false);
});
```
</details>
</details>
#### `.exists() => Boolean`
#### `.exists() => Boolean`
Returns whether or not the wrapper contains any elements.
Returns whether or not the wrapper contains any elements.
<details>
<summary>Example</summary>
<details>
<summary>Example</summary>
```html
<button>Pay</button>
```
```html
<button>Pay</button>
```
```js
it('has button', () => {
expect(component.find('button').exists()).toBe(true);
});
```js
it('has button', () => {
expect(component.find('button').exists()).toBe(true);
});
it('does not have link', () => {
expect(component.find('a').exists()).toBe(false);
});
```
it('does not have link', () => {
expect(component.find('a').exists()).toBe(false);
});
```
</details>
</details>
#### `.find(selector) => TestElementWrapper`
#### `.find(selector) => TestElementWrapper`
Returns a [`TestElementWrapper`](#testelementwrapper-api) (for chaining) with every element matching the `selector` (`String`).
Returns a [`TestElementWrapper`](#testelementwrapper-api) (for chaining) with every element matching the `selector` (`String`).
<details>
<summary>Example</summary>
<details>
<summary>Example</summary>
```html
<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>
```
```html
<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>
```
```js
it('has one transferwise link with corrext text on the right', () => {
const link = component.find('.right a[href="https://transferwise.com"]');
```js
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');
});
```
expect(link.length).toBe(1);
expect(link.text()).toBe('Correct');
});
```
</details>
</details>
#### `.map(fn) => Array<Any>`
#### `.map(fn) => Array<Any>`
Maps the nodes in the wrapper to another array using `fn` (`Function`).
Maps the nodes in the wrapper to another array using `fn` (`Function`).
<details>
<summary>Example</summary>
<details>
<summary>Example</summary>
```html
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
```
```html
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
```
```js
it('has three list items with their number as a word', () => {
const items = component.find('li');
```js
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']);
});
```
expect(items.map(item => item.text())).toEqual(['One', 'Two', 'Three']);
});
```
</details>
</details>
#### `.props() => Object`
#### `.props() => Object`
Returns all wrapper props/attributes.
Returns all wrapper props/attributes.
<details>
<summary>Example</summary>
<details>
<summary>Example</summary>
```html
<a href="https://transferwise.com" target="_blank">Send money</a>
```
```html
<a href="https://transferwise.com" target="_blank">Send money</a>
```
```js
it('has transferwise link that opens in a new tab', () => {
expect(component.find('a').props()).toEqual({
href: 'https://transferwise.com',
target: '_blank',
});
```js
it('has transferwise link that opens in a new tab', () => {
expect(component.find('a').props()).toEqual({
href: 'https://transferwise.com',
target: '_blank',
});
```
});
```
</details>
</details>
#### `.prop(key) => String`
#### `.prop(key) => String`
Returns wrapper prop/attribute value with provided `key` (`String`).
Returns wrapper prop/attribute value with provided `key` (`String`).
<details>
<summary>Example</summary>
<details>
<summary>Example</summary>
```html
<a href="https://transferwise.com">Send money</a>
```
```html
<a href="https://transferwise.com">Send money</a>
```
```js
it('has transferwise link', () => {
expect(component.find('a').prop('href')).toBe('https://transferwise.com');
});
```
```js
it('has transferwise link', () => {
expect(component.find('a').prop('href')).toBe('https://transferwise.com');
});
```
</details>
</details>
#### `.simulate(event[, data]) => Self`
#### `.simulate(event[, data]) => Self`
Calls an event handler on the wrapper for passed `event` with `data` (optional) and returns wrapper for chaining.
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.
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.
<details>
<summary>Example</summary>
<details>
<summary>Example</summary>
```html
<input ng-model="$ctrl.text" />
<p>{{ $ctrl.text }}</p>
<button ng-click="$ctrl.onClick({ $event: $ctrl.text })">Click me</button>
```
```html
<input ng-model="$ctrl.text" />
<p>{{ $ctrl.text }}</p>
<button ng-click="$ctrl.onClick({ $event: $ctrl.text })">Click me</button>
```
```js
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 },
);
});
```js
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');
it('calls click handler on button click', () => {
const button = component.find('button');
expect(onClick).not.toBeCalled();
button.simulate('click');
expect(onClick).toBeCalledWith('Original text');
});
expect(onClick).not.toBeCalled();
button.simulate('click');
expect(onClick).toBeCalledWith('Original text');
});
it('changes text on input change', () => {
const input = component.find('input');
it('changes text on input change', () => {
const input = component.find('input');
const text = () => component.find('p').text();
const text = () => component.find('p').text();
expect(text()).toBe('Original text');
input.simulate('change', { target: { value: 'New text' } });
expect(text()).toBe('New text');
});
```
expect(text()).toBe('Original text');
input.simulate('change', { target: { value: 'New text' } });
expect(text()).toBe('New text');
});
```
</details>
</details>
#### `.setProps(props) => Self`
#### `.setProps(props) => Self`
Merges `props` (`Object`) with existing props and updates view to reflect them, returning itself for chaining.
Merges `props` (`Object`) with existing props and updates view to reflect them, returning itself for chaining.
<details>
<summary>Example</summary>
<details>
<summary>Example</summary>
```html
<h1>{{ $ctrl.title }}</h1>
<p>{{ $ctrl.text }}</p>
```
```html
<h1>{{ $ctrl.title }}</h1>
<p>{{ $ctrl.text }}</p>
```
```js
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' },
);
```js
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();
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');
});
```
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');
});
```
</details>
</details>
### `mock` API
### `mock` API
#### `.exists() => Boolean`
#### `.exists() => Boolean`
Returns whether or not the mocked component exists in the rendered template.
Returns whether or not the mocked component exists in the rendered template.
<details>
<summary>Example</summary>
<details>
<summary>Example</summary>
```js
let component;
beforeEach(() => {
component = mount(`
<button ng-click="$ctrl.show = !$ctrl.show">
Show child
</button>
<child-component ng-if="$ctrl.show"></child-component>
`);
});
```js
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');
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);
});
```
expect(childComponent.exists()).toBe(false);
button.simulate('click');
expect(childComponent.exists()).toBe(true);
button.simulate('click');
expect(childComponent.exists()).toBe(false);
});
```
</details>
</details>
#### `.props() => Object`
#### `.props() => Object`
Returns all mocked component props.
Returns all mocked component props.
<details>
<summary>Example</summary>
<details>
<summary>Example</summary>
```js
let component;
beforeEach(() => {
component = mount(`
<div>Something else</div>
<child-component
some-prop="'A string'",
some-other-prop="12345"
></child-component>
`);
});
```js
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,
});
it('passes props to child component', () => {
expect(childComponent.props()).toEqual({
someProp: 'A string',
someOtherProp: 12345,
});
```
});
```
</details>
</details>
#### `.prop(key) => Any`
#### `.prop(key) => Any`
Returns mocked component prop value with the provided `key` (`String`).
Returns mocked component prop value with the provided `key` (`String`).
<details>
<summary>Example</summary>
<details>
<summary>Example</summary>
```js
let component;
beforeEach(() => {
component = mount(`
<div>Something else</div>
<child-component some-prop="'A string'"></child-component>
`);
});
```js
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');
});
```
it('passes some prop to child component', () => {
expect(childComponent.prop('someProp')).toBe('A string');
});
```
</details>
</details>
#### `.simulate(event[, data]) => Self`
#### `.simulate(event[, data]) => Self`
Calls an event handler on the mocked component for passed `event` with `data` (optional) and returns mocked component for chaining.
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.
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.
<details>
<summary>Example</summary>
<details>
<summary>Example</summary>
```js
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
);
```js
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');
});
```
expect(onSomePropChange).not.toBeCalled();
childComponent.simulate('somePropChange', 'New value');
expect(onSomePropChange).toBeCalledWith('New value');
});
```
</details>
</details>
## Contributing
## Contributing
1. Run tests with `npm run test:watch`. `npm test` will check for package and changelog version match, ESLint and Prettier format in addition.
1. Bump version number in `package.json` according to [semver](http://semver.org/) and add an item that a release will be based on to `CHANGELOG.md`.
1. Submit your pull request from a feature branch and get code reviews.
1. If the pull request is approved and the [CircleCI build](https://circleci.com/gh/oliverviljamaa/angularjs-enzyme) passes, you will be able to squash/rebase and merge.
1. Code will automatically be released to [GitHub](https://github.com/oliverviljamaa/angularjs-enzyme/releases) and published to [npm](https://www.npmjs.com/package/angularjs-enzyme) according to the version specified in the changelog and `package.json`.
1. Run tests with `npm run test:watch`. `npm test` will check for package and changelog version match, ESLint and Prettier format in addition.
1. Bump version number in `package.json` according to [semver](http://semver.org/) and add an item that a release will be based on to `CHANGELOG.md`.
1. Submit your pull request from a feature branch and get code reviews.
1. If the pull request is approved and the [CircleCI build](https://circleci.com/gh/oliverviljamaa/angularjs-enzyme) passes, you will be able to squash/rebase and merge.
1. Code will automatically be released to [GitHub](https://github.com/oliverviljamaa/angularjs-enzyme/releases) and published to [npm](https://www.npmjs.com/package/angularjs-enzyme) according to the version specified in the changelog and `package.json`.
## Other
## Other
For features and bugs, feel free to [add issues](https://github.com/oliverviljamaa/angularjs-enzyme/issues) or contribute.
For features and bugs, feel free to [add issues](https://github.com/oliverviljamaa/angularjs-enzyme/issues) or contribute.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc