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.1 to 0.3.0

5

CHANGELOG.md

@@ -0,1 +1,6 @@

# v0.3.0
## Add `options.mockComponents` to `mount`
See [README](README.md#mounttemplate-props-options--testelementwrapper).
# v0.2.1

@@ -2,0 +7,0 @@ ## Fix readme

509

dist/angularjs-enzyme.js

@@ -23,3 +23,77 @@ (function (global, factory) {

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
var createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
var defineProperty = function (obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
};
var _extends = Object.assign || function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
var objectWithoutProperties = function (obj, keys) {
var target = {};
for (var i in obj) {
if (keys.indexOf(i) >= 0) continue;
if (!Object.prototype.hasOwnProperty.call(obj, i)) continue;
target[i] = obj[i];
}
return target;
};
var toConsumableArray = function (arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
return arr2;
} else {
return Array.from(arr);
}
};
function mockComponent(kebabCaseName) {

@@ -30,5 +104,13 @@ var name = convertKebabCaseToCamelCase(kebabCaseName);

var component = $delegate[0];
component.controller = function controller() {
mock._controller = this;
};
if (component.templateUrl) {
delete component.templateUrl;
}
component.template = '<!-- mock of ' + kebabCaseName + ' -->';
component.controller = function () {
function _class() {
classCallCheck(this, _class);
mock._controller = this;
}
return _class;
}();
return $delegate;

@@ -53,3 +135,3 @@ });

controller = _mock$_controller.controller,
props = _objectWithoutProperties(_mock$_controller, ['controller']);
props = objectWithoutProperties(_mock$_controller, ['controller']);
return props;

@@ -74,60 +156,347 @@ };

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var TestElementWrapper = function TestElementWrapper(angularElement) {
var _this = this;
_classCallCheck(this, TestElementWrapper);
this.length = angularElement.length;
this.html = function () {
return angularElement.html();
};
this.text = function () {
return angularElement.text();
};
this.hasClass = function (name) {
return angularElement.hasClass(name);
};
this.exists = function () {
return angularElement && angularElement.length > 0;
};
this.find = function (selector) {
var foundElement = angularElement[0].querySelectorAll(selector);
return elementToTestElementWrapper(foundElement);
};
this.map = function (fn) {
return [].concat(_toConsumableArray(angularElement)).map(elementToTestElementWrapper).map(fn);
};
this.props = function () {
return [].concat(_toConsumableArray(angularElement[0].attributes)).reduce(function (props, _ref) {
var name = _ref.name,
value = _ref.value;
return _extends({}, props, _defineProperty({}, name, value));
}, {});
};
this.prop = function (key) {
return angularElement.attr(key);
};
this.simulate = function (event, data) {
if (event === 'change') {
if (!data || !data.target) {
throw new Error('Simulating change expects an event object ({ target: { value } })');
var validTypes = { object: true, symbol: true };
var isImplemented = function () {
if (typeof Symbol !== 'function') return false;
try { } catch (e) { return false; }
if (!validTypes[typeof Symbol.iterator]) return false;
if (!validTypes[typeof Symbol.toPrimitive]) return false;
if (!validTypes[typeof Symbol.toStringTag]) return false;
return true;
};
function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}
var isImplemented$1 = function () {
var assign = Object.assign, obj;
if (typeof assign !== "function") return false;
obj = { foo: "raz" };
assign(obj, { bar: "dwa" }, { trzy: "trzy" });
return (obj.foo + obj.bar + obj.trzy) === "razdwatrzy";
};
var isImplemented$2 = function () {
try {
return true;
} catch (e) {
return false;
}
};
var noop = function () {};
var _undefined = noop();
var isValue = function (val) {
return (val !== _undefined) && (val !== null);
};
var keys = Object.keys;
var shim = function (object) { return keys(isValue(object) ? Object(object) : object); };
var keys$1 = isImplemented$2() ? Object.keys : shim;
var validValue = function (value) {
if (!isValue(value)) throw new TypeError("Cannot use null or undefined");
return value;
};
var max = Math.max;
var shim$1 = function (dest, src ) {
var error, i, length = max(arguments.length, 2), assign;
dest = Object(validValue(dest));
assign = function (key) {
try {
dest[key] = src[key];
} catch (e) {
if (!error) error = e;
}
};
for (i = 1; i < length; ++i) {
src = arguments[i];
keys$1(src).forEach(assign);
}
if (error !== undefined) throw error;
return dest;
};
var assign = isImplemented$1()
? Object.assign
: shim$1;
var forEach = Array.prototype.forEach, create = Object.create;
var process = function (src, obj) {
var key;
for (key in src) obj[key] = src[key];
};
var normalizeOptions = function (opts1 ) {
var result = create(null);
forEach.call(arguments, function (options) {
if (!isValue(options)) return;
process(Object(options), result);
});
return result;
};
var isCallable = function (obj) {
return typeof obj === "function";
};
var str = "razdwatrzy";
var isImplemented$3 = function () {
if (typeof str.contains !== "function") return false;
return (str.contains("dwa") === true) && (str.contains("foo") === false);
};
var indexOf = String.prototype.indexOf;
var shim$2 = function (searchString ) {
return indexOf.call(this, searchString, arguments[1]) > -1;
};
var contains = isImplemented$3()
? String.prototype.contains
: shim$2;
var d_1 = createCommonjsModule(function (module) {
var d;
d = module.exports = function (dscr, value ) {
var c, e, w, options, desc;
if ((arguments.length < 2) || (typeof dscr !== 'string')) {
options = value;
value = dscr;
dscr = null;
} else {
options = arguments[2];
}
if (dscr == null) {
c = w = true;
e = false;
} else {
c = contains.call(dscr, 'c');
e = contains.call(dscr, 'e');
w = contains.call(dscr, 'w');
}
desc = { value: value, configurable: c, enumerable: e, writable: w };
return !options ? desc : assign(normalizeOptions(options), desc);
};
d.gs = function (dscr, get, set ) {
var c, e, options, desc;
if (typeof dscr !== 'string') {
options = set;
set = get;
get = dscr;
dscr = null;
} else {
options = arguments[3];
}
if (get == null) {
get = undefined;
} else if (!isCallable(get)) {
options = get;
get = set = undefined;
} else if (set == null) {
set = undefined;
} else if (!isCallable(set)) {
options = set;
set = undefined;
}
if (dscr == null) {
c = true;
e = false;
} else {
c = contains.call(dscr, 'c');
e = contains.call(dscr, 'e');
}
desc = { get: get, set: set, configurable: c, enumerable: e };
return !options ? desc : assign(normalizeOptions(options), desc);
};
});
var isSymbol = function (x) {
if (!x) return false;
if (typeof x === 'symbol') return true;
if (!x.constructor) return false;
if (x.constructor.name !== 'Symbol') return false;
return (x[x.constructor.toStringTag] === 'Symbol');
};
var validateSymbol = function (value) {
if (!isSymbol(value)) throw new TypeError(value + " is not a symbol");
return value;
};
var create$1 = Object.create, defineProperties = Object.defineProperties
, defineProperty$1 = Object.defineProperty, objPrototype = Object.prototype
, NativeSymbol, SymbolPolyfill, HiddenSymbol, globalSymbols = create$1(null)
, isNativeSafe;
if (typeof Symbol === 'function') {
NativeSymbol = Symbol;
try {
String(NativeSymbol());
isNativeSafe = true;
} catch (ignore) {}
}
var generateName = (function () {
var created = create$1(null);
return function (desc) {
var postfix = 0, name, ie11BugWorkaround;
while (created[desc + (postfix || '')]) ++postfix;
desc += (postfix || '');
created[desc] = true;
name = '@@' + desc;
defineProperty$1(objPrototype, name, d_1.gs(null, function (value) {
if (ie11BugWorkaround) return;
ie11BugWorkaround = true;
defineProperty$1(this, name, d_1(value));
ie11BugWorkaround = false;
}));
return name;
};
}());
HiddenSymbol = function Symbol(description) {
if (this instanceof HiddenSymbol) throw new TypeError('Symbol is not a constructor');
return SymbolPolyfill(description);
};
var polyfill = SymbolPolyfill = function Symbol(description) {
var symbol;
if (this instanceof Symbol) throw new TypeError('Symbol is not a constructor');
if (isNativeSafe) return NativeSymbol(description);
symbol = create$1(HiddenSymbol.prototype);
description = (description === undefined ? '' : String(description));
return defineProperties(symbol, {
__description__: d_1('', description),
__name__: d_1('', generateName(description))
});
};
defineProperties(SymbolPolyfill, {
for: d_1(function (key) {
if (globalSymbols[key]) return globalSymbols[key];
return (globalSymbols[key] = SymbolPolyfill(String(key)));
}),
keyFor: d_1(function (s) {
var key;
validateSymbol(s);
for (key in globalSymbols) if (globalSymbols[key] === s) return key;
}),
hasInstance: d_1('', (NativeSymbol && NativeSymbol.hasInstance) || SymbolPolyfill('hasInstance')),
isConcatSpreadable: d_1('', (NativeSymbol && NativeSymbol.isConcatSpreadable) ||
SymbolPolyfill('isConcatSpreadable')),
iterator: d_1('', (NativeSymbol && NativeSymbol.iterator) || SymbolPolyfill('iterator')),
match: d_1('', (NativeSymbol && NativeSymbol.match) || SymbolPolyfill('match')),
replace: d_1('', (NativeSymbol && NativeSymbol.replace) || SymbolPolyfill('replace')),
search: d_1('', (NativeSymbol && NativeSymbol.search) || SymbolPolyfill('search')),
species: d_1('', (NativeSymbol && NativeSymbol.species) || SymbolPolyfill('species')),
split: d_1('', (NativeSymbol && NativeSymbol.split) || SymbolPolyfill('split')),
toPrimitive: d_1('', (NativeSymbol && NativeSymbol.toPrimitive) || SymbolPolyfill('toPrimitive')),
toStringTag: d_1('', (NativeSymbol && NativeSymbol.toStringTag) || SymbolPolyfill('toStringTag')),
unscopables: d_1('', (NativeSymbol && NativeSymbol.unscopables) || SymbolPolyfill('unscopables'))
});
defineProperties(HiddenSymbol.prototype, {
constructor: d_1(SymbolPolyfill),
toString: d_1('', function () { return this.__name__; })
});
defineProperties(SymbolPolyfill.prototype, {
toString: d_1(function () { return 'Symbol (' + validateSymbol(this).__description__ + ')'; }),
valueOf: d_1(function () { return validateSymbol(this); })
});
defineProperty$1(SymbolPolyfill.prototype, SymbolPolyfill.toPrimitive, d_1('', function () {
var symbol = validateSymbol(this);
if (typeof symbol === 'symbol') return symbol;
return symbol.toString();
}));
defineProperty$1(SymbolPolyfill.prototype, SymbolPolyfill.toStringTag, d_1('c', 'Symbol'));
defineProperty$1(HiddenSymbol.prototype, SymbolPolyfill.toStringTag,
d_1('c', SymbolPolyfill.prototype[SymbolPolyfill.toStringTag]));
defineProperty$1(HiddenSymbol.prototype, SymbolPolyfill.toPrimitive,
d_1('c', SymbolPolyfill.prototype[SymbolPolyfill.toPrimitive]));
var es6Symbol = isImplemented() ? Symbol : polyfill;
var angularElementSymbol = es6Symbol('_angularElement');
var mockedComponentsSymbol = es6Symbol('_mockedComponents');
var TestElementWrapper = function () {
function TestElementWrapper(angularElement) {
var _Object$definePropert;
var mockedComponents = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
classCallCheck(this, TestElementWrapper);
Object.defineProperties(this, (_Object$definePropert = {}, defineProperty(_Object$definePropert, angularElementSymbol, { value: angularElement }), defineProperty(_Object$definePropert, mockedComponentsSymbol, { value: mockedComponents }), _Object$definePropert));
this.length = angularElement.length;
}
createClass(TestElementWrapper, [{
key: 'html',
value: function html() {
return this[angularElementSymbol].html();
}
}, {
key: 'text',
value: function text() {
return this[angularElementSymbol].text();
}
}, {
key: 'hasClass',
value: function hasClass(name) {
return this[angularElementSymbol].hasClass(name);
}
}, {
key: 'exists',
value: function exists() {
return this[angularElementSymbol] && this[angularElementSymbol].length > 0;
}
}, {
key: 'find',
value: function find(selector) {
var mockedComponents = this[mockedComponentsSymbol];
var mockedComponentNames = Object.keys(mockedComponents);
if (mockedComponentNames.indexOf(selector) > -1) {
return mockedComponents[selector];
}
angularElement.val(data.target.value).triggerHandler('input');
return _this;
var foundElement = this[angularElementSymbol][0].querySelectorAll(selector);
return elementToTestElementWrapper(foundElement);
}
angularElement.triggerHandler(event);
return _this;
};
this.setProps = function (props) {
var $rootScope = void 0;
angular.mock.inject(function ($injector) {
$rootScope = $injector.get('$rootScope');
});
$rootScope.$ctrl = _extends({}, $rootScope.$ctrl, props);
$rootScope.$digest();
return _this;
};
};
}, {
key: 'map',
value: function map(fn) {
return [].concat(toConsumableArray(this[angularElementSymbol])).map(elementToTestElementWrapper).map(fn);
}
}, {
key: 'props',
value: function props() {
return [].concat(toConsumableArray(this[angularElementSymbol][0].attributes)).reduce(function (props, _ref) {
var name = _ref.name,
value = _ref.value;
return _extends({}, props, defineProperty({}, name, value));
}, {});
}
}, {
key: 'prop',
value: function prop(key) {
return this[angularElementSymbol].attr(key);
}
}, {
key: 'simulate',
value: function simulate(event, data) {
if (event === 'change') {
if (!data || !data.target) {
throw new Error('Simulating change expects an event object ({ target: { value } })');
}
this[angularElementSymbol].val(data.target.value).triggerHandler('input');
return this;
}
this[angularElementSymbol].triggerHandler(event);
return this;
}
}, {
key: 'setProps',
value: function setProps(props) {
var $rootScope = void 0;
angular.mock.inject(function ($injector) {
$rootScope = $injector.get('$rootScope');
});
$rootScope.$ctrl = _extends({}, $rootScope.$ctrl, props);
$rootScope.$digest();
return this;
}
}]);
return TestElementWrapper;
}();
function elementToTestElementWrapper(element) {

@@ -137,3 +506,12 @@ return new TestElementWrapper(angular.element(element));

function mount(template, props) {
function mount(template) {
var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
_ref$mockComponents = _ref.mockComponents,
mockComponents = _ref$mockComponents === undefined ? [] : _ref$mockComponents;
var mockedComponents = getMockedComponents(mockComponents);
var angularElement = getAngularElement(template, props);
return new TestElementWrapper(angularElement, mockedComponents);
}
function getAngularElement(template, props) {
var $rootScope = void 0;

@@ -147,4 +525,9 @@ var element = void 0;

$rootScope.$digest();
return new TestElementWrapper(element);
return element;
}
function getMockedComponents(names) {
return names.reduce(function (components, name) {
return _extends({}, components, defineProperty({}, name, mockComponent(name)));
}, {});
}

@@ -151,0 +534,0 @@ exports.mockComponent = mockComponent;

5

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

@@ -37,2 +37,3 @@ "files": [

"babel-core": "^6.26.3",
"babel-plugin-external-helpers": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",

@@ -54,2 +55,3 @@ "babel-preset-env": "^1.7.0",

"rollup-plugin-cleanup": "^3.0.0",
"rollup-plugin-commonjs": "^9.1.5",
"rollup-plugin-node-resolve": "^3.3.0"

@@ -64,4 +66,5 @@ },

"dependencies": {
"es6-symbol": "^3.1.1",
"lodash": "^4.17.10"
}
}

@@ -10,3 +10,3 @@ # :rotating_light: AngularJS Enzyme

Available methods:
[`mount`](#mounttemplate-props--testelementwrapper)
[`mount`](#mounttemplate-props-options--testelementwrapper)
[`mockComponent`](#mockcomponentname--mock)

@@ -38,5 +38,5 @@

### `mount(template[, props]) => TestElementWrapper`
### `mount(template[, props, options]) => 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. When `options.mockComponents` (`Array`) is specified, the child components with the names in the array will be [`mocked`](#mockcomponentname--mock) and will be findable from the resulting [`TestElementWrapper`](#testelementwrapper-api), returning their [`mock`](#mock-api).

@@ -60,3 +60,7 @@ <details>

angular.mock.module('moduleOfComponentUnderTest');
component = mount(TEMPLATE, { title: 'A title', text: 'Some text' });
component = mount(
TEMPLATE,
{ title: 'A title', text: 'Some text' },
{ mockComponents: ['child-component', 'another-child-component'] }, // optional
);
});

@@ -199,5 +203,5 @@ });

#### `.find(selector) => TestElementWrapper`
#### `.find(selector) => TestElementWrapper | mock`
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`). When the component was[`mount`](#mounttemplate-props-options--testelementwrapper)ed with `options.mockComponents` specified, these components will also be findable by their name and will return their [`mock`](#mock-api).

@@ -204,0 +208,0 @@ <details>

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