New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

saywhen

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

saywhen - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

2

package.json
{
"name": "saywhen",
"version": "0.0.2",
"version": "0.0.3",
"description": "Better spy fake calls for Jasmine",

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

saywhen
=======
[![Build Status](https://travis-ci.org/pushtechnology/saywhen.svg?branch=master)](https://travis-ci.org/pushtechnology/saywhen)
[![npm version](https://badge.fury.io/js/saywhen.svg)](http://badge.fury.io/js/saywhen)
Top up your Jasmine spies with better fake calls.
Top up your [Jasmine](http://jasmine.github.io/) spies with better fake calls.

@@ -37,2 +38,6 @@ Motivated by the fact that using spies in Jasmine requires a lot of boilerplate to perform different actions depending on provided arguments.

###Installation
The easiest way to install ```saywhen``` is via [npm](http://npmjs.org/). Simply run ```npm install saywhen``` in your project directory.
###Usage

@@ -104,2 +109,33 @@

**Use captors to capture argument values**
```javascript
var captor = when.captor();
when(spy).isCalledWith(jasmine.any(String), captor);
spy("foo", 123);
spy("foo", 456);
spy(null, 789);
captor.values(); // => [123, 456]
captor.value(); // => 456 (last value)
```
**Captors can also wrap matchers, to allow only capture specific arguments**
```javascript
var captor = when.captor(jasmine.any(Number));
when(spy).isCalledWith(captor).then(function(arg) {
return arg * 2;
});
spy(2); // => 4
spy(3); // => 6
spy("foo") // => undefined (doesn't match)
captor.values(); // => [2, 3]
captor.value(); // => 3
```
---

@@ -106,0 +142,0 @@

var handlers = [];
function defaultMatcher() {
return true;
}
function createMatcher(val) {
if (val instanceof Function) {
return val;
} else if (val && val.jasmineMatches !== undefined) {
return val.jasmineMatches.bind(val);
} else {
return function(arg) { return val === arg };
}
}
function MatcherSet(matchers) {

@@ -11,13 +26,6 @@ matchers = matchers || [];

for (var i = 0; i < args.length; ++i) {
var matcher = matchers[i], result = false;
if (matcher instanceof Function) {
result = matcher(args[i]);
} else if (matcher.jasmineMatches !== undefined) {
result = matcher.jasmineMatches(args[i]);
} else {
result = matchers[i] === args[i];
// Quickly bail if we can't match
if (!matchers[i](args[i])) {
return false;
}
if (!result) return false;
}

@@ -41,2 +49,27 @@

function captor(val) {
var matcher = arguments.length === 0 ? defaultMatcher : createMatcher(val);
var values = [];
var matcherProxy = function(arg) {
if (matcher(arg)) {
values.push(arg);
return true;
}
return false;
};
matcherProxy.value = function() {
return values[values.length - 1];
};
matcherProxy.values = function() {
return values;
};
return matcherProxy;
}
function proxy(matcher) {

@@ -66,3 +99,3 @@ return {

function CallHandler(spy) {
var matchers = [];
var matcherSets = [];
var defaultSet = new MatcherSet();

@@ -73,4 +106,4 @@

for (var i = 0; i < matchers.length; ++i) {
var set = matchers[i];
for (var i = 0; i < matcherSets.length; ++i) {
var set = matcherSets[i];

@@ -86,8 +119,13 @@ if (set.matches(args)) {

this.isCalledWith = function() {
var args = Array.prototype.slice.call(arguments, 0);
var args = Array.prototype.slice.call(arguments, 0);
var matchers = [];
var matcher = new MatcherSet(args);
matchers.push(matcher);
for (var i = 0; i < args.length; ++i) {
matchers.push(createMatcher(args[i]));
}
return proxy(matcher);
var matcherSet = new MatcherSet(matchers);
matcherSets.push(matcherSet);
return proxy(matcherSet);
};

@@ -122,2 +160,4 @@

module.exports = when;
when.captor = captor;
module.exports = when;

@@ -198,2 +198,89 @@ var when = require('../saywhen');

});
});
it("can be applied to spy object methods", function() {
var spy = jasmine.createSpyObj('spy', ['fake1', 'fake2', 'fake3']);
when(spy.fake1).isCalled.thenReturn(123);
when(spy.fake2).isCalledWith(jasmine.any(String)).thenReturn(456);
expect(spy.fake1()).toBe(123);
expect(spy.fake2('foo')).toBe(456);
});
it("can use default argument captor", function() {
var captor = when.captor();
when(spy).isCalledWith(captor);
spy("foo");
expect(captor.value()).toBe("foo");
expect(captor.values()).toEqual(["foo"]);
});
it("captors can track multiple arguments", function() {
var captor = when.captor();
when(spy).isCalledWith(captor);
spy("foo");
spy("bar");
expect(captor.value()).toBe("bar");
expect(captor.values()).toEqual(["foo", "bar"]);
});
it("captors only capture specific argument position", function() {
var captor = when.captor();
when(spy).isCalledWith("foo", captor);
spy("foo", 123);
spy("bar", 456);
spy("foo", 789);
expect(captor.value()).toBe(789);
expect(captor.values()).toEqual([123, 789]);
});
it("captors can take value matchers", function() {
var captor = when.captor('foo');
when(spy).isCalledWith(captor);
spy('foo');
spy('bar');
spy('foo');
expect(captor.values()).toEqual(['foo', 'foo']);
});
it('captors can take jasmine.any matchers', function() {
var captor = when.captor(jasmine.any(String));
when(spy).isCalledWith(captor);
spy('foo');
spy(12345);
spy([678]);
spy('bar');
expect(captor.value()).toBe('bar');
expect(captor.values()).toEqual(['foo', 'bar']);
});
it('captors can be used with responses', function() {
var captor = when.captor(jasmine.any(Number));
when(spy).isCalledWith(captor).then(function(arg) {
return arg * 2;
});
expect(spy(1)).toBe(2);
expect(spy(3)).toBe(6);
expect(spy("foo")).toBe(undefined);
expect(captor.value()).toBe(3);
expect(captor.values()).toEqual([1, 3]);
});
});
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