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

43

package.json
{
"name": "saywhen",
"version": "0.0.1",
"version": "0.0.2",
"description": "Better spy fake calls for Jasmine",
"keywords" : ["jasmine", "spy", "fake", "mock"],
"homepage" : "https://github.com/pushtechnology/saywhen",
"repository" : {
"type" : "git",
"url" : "https://github.com/pushtechnology/saywhen.git"
"keywords": [
"jasmine",
"spy",
"fake",
"mock"
],
"homepage": "https://github.com/pushtechnology/saywhen",
"repository": {
"type": "git",
"url": "https://github.com/pushtechnology/saywhen.git"
},
"author" : "Peter Hughes",
"license" : "Apache-2.0",
"main" : "saywhen.js",
"files" : ["README.md", "LICENSE.txt", "saywhen.js", "spec"],
"scripts" : {
"author": "Peter Hughes",
"license": "Apache-2.0",
"main": "saywhen.js",
"files": [
"README.md",
"LICENSE.txt",
"saywhen.js",
"spec"
],
"scripts": {
"test" : "jasmine"
},
"dependencies" : {
}
"dependencies": {},
"devDependencies": {
"jasmine": "~2.1.0"
}
}
saywhen
=======
[![Build Status](https://travis-ci.org/pushtechnology/saywhen.svg?branch=master)](https://travis-ci.org/pushtechnology/saywhen)
Provides conditional fake calls for Jasmine 2 spies.
Top up your Jasmine spies with better fake calls.
Motivated by the fact that using spies in Jasmine requires a lot of boilerplate to perform different actions depending on provided arguments.
Heavily influenced by the [Mockito](https://github.com/mockito/mockito) library for Java.
Currently supports Jasmine 2 only. Heavily influenced by the [Mockito](https://github.com/mockito/mockito) library for Java.

@@ -17,8 +18,8 @@ ---

if (arg === 'something') {
return 'a value';
} else if (arg === 'something else') {
return 'a different value';
} else {
throw new Error('some error');
}
return 'a value';
} else if (arg === 'something else') {
return 'a different value';
} else {
throw new Error('some error');
}
};

@@ -30,5 +31,5 @@ ```

```javascript
when(spy).isCalled.thenThrow(new Error('some error'));
when(spy).isCalledWith('something').thenReturn('a value');
when(spy).isCalledWith('something else').thenReturn('a value');
when(spy).isCalledWith(jasmine.any(Object)).thenThrow(new Error('some error'));
when(spy).isCalledWith('something else').thenReturn('a different value');
```

@@ -53,2 +54,13 @@

**Mix default handlers and specific handlers**
```javascript
when(spy).isCalled.thenReturn(1);
when(spy).isCalledWith('two').thenReturn(2);
spy(); // => 1
spy('bar'); // => 1
spy('two'); // => 2
```
**Make a spy call a particular function, when called with a specific argument**

@@ -58,3 +70,3 @@

when(spy).isCalledWith('bar').then(function(arg) {
// Do something
// Do something with arg
});

@@ -69,9 +81,21 @@ ```

**Works with jasmine.any & jasmine.objectContaining**
```javascript
when(spy).isCalledWith(jasmine.any(String)).thenReturn("string!");
when(spy).isCalledWith(jasmine.objectContaining({
foo : "bar"
})).thenReturn("object!");
spy('abc'); // => string!
spy({ foo : "bar" }); // => object!
```
**Multiple callbacks can be added and will be executed in order**
```javascript
when(spy).isCalledWith().thenReturn(1)
.thenReturn(2)
.thenReturn(3)
.thenThrow(new Error('eof'));
when(spy).isCalled.thenReturn(1)
.thenReturn(2)
.thenReturn(3)
.thenThrow(new Error('eof'));

@@ -86,4 +110,12 @@ spy(); // => 1

###Contributing
Say When is an open source project, maintained by Push Technology. Issues and pull requests are welcomed.
Tests can be run by installing a dev dependencies with ```npm install``` and then running ```npm test```
---
###License
Licensed under the Apache 2.0 license. See LICENSE.txt.
Licensed under the Apache 2.0 license. See LICENSE.txt.

@@ -40,4 +40,28 @@ var handlers = [];

function proxy(matcher) {
return {
then : function(fn) {
matcher.addResponse(fn);
return this;
},
thenReturn : function(val) {
matcher.addResponse(function() {
return val;
});
return this;
},
thenThrow : function(err) {
matcher.addResponse(function() {
throw err;
});
return this;
}
};
}
function CallHandler(spy) {
var matchers = [];
var defaultSet = new MatcherSet();

@@ -54,2 +78,4 @@ spy.and.callFake(function() {

}
return defaultSet.apply(args);
});

@@ -63,23 +89,7 @@

return {
then : function(fn) {
matcher.addResponse(fn);
return this;
},
thenReturn : function(val) {
matcher.addResponse(function() {
return val;
});
return proxy(matcher);
};
return this;
},
thenThrow : function(err) {
matcher.addResponse(function() {
throw err;
});
return this;
}
};
};
this.isCalled = proxy(defaultSet);
spy.and.callFake = this.isCalled.then;
}

@@ -86,0 +96,0 @@

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

it("can set default handler", function() {
when(spy).isCalled.thenReturn("foo");
expect(spy()).toBe("foo");
expect(spy("foo", 123, [4, 5, 6])).toBe("foo");
});
it("can set multiple responses to default handler", function() {
when(spy).isCalled.thenReturn(1)
.thenReturn(2)
.thenReturn(3);
expect(spy()).toBe(1);
expect(spy()).toBe(2);
expect(spy()).toBe(3);
expect(spy()).toBe(3);
});
it("can set default handler and specific handler", function() {
when(spy).isCalledWith("foo").thenReturn("bar");
when(spy).isCalled.thenReturn("bing");
expect(spy("foo")).toBe("bar");
expect(spy("baz")).toBe("bing");
});
it("proxies and.callFake behaviour", function() {
when(spy).isCalledWith('foo').thenReturn('bar');
spy.and.callFake(function() {
return 123;
});
expect(spy('foo')).toBe('bar');
expect(spy()).toBe(123);
});
it("can assign multiple argument matchers", function() {

@@ -151,0 +188,0 @@ when(spy).isCalledWith(jasmine.any(String)).then(function() {

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