Skip to content

Commit

Permalink
Upgraded to 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorhakes committed Dec 28, 2017
1 parent 147187d commit fc2f9ca
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 11 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ npm install promise-mock
### Simple use with Mocha or Jasmine
Get the result of a Promise `PromiseMock.getResult`
```js
var PromiseMock = require('promise-mock');
import PromiseMock from 'promise-mock';

describe('testing', function() {
beforeEach(function() {
Expand All @@ -38,7 +38,7 @@ describe('testing', function() {

Handle errors synchronously
```js
var PromiseMock = require('promise-mock');
import PromiseMock from 'promise-mock';

describe('testing', function() {
beforeEach(function() {
Expand All @@ -57,7 +57,7 @@ describe('testing', function() {

Execute a single async callback
```js
var PromiseMock = require('promise-mock');
import PromiseMock from 'promise-mock';

describe('testing', function() {
beforeEach(function() {
Expand All @@ -79,7 +79,7 @@ describe('testing', function() {
```
Resolve all pending Promises
```js
var PromiseMock = require('promise-mock');
import PromiseMock from 'promise-mock';

describe('testing', function() {
beforeEach(function() {
Expand Down Expand Up @@ -108,7 +108,12 @@ describe('testing', function() {
});
```

### For commonjs/require webpack and rollup
Since the move to ES6 modules. You need to add `.default`
```js
const PromiseMock = require('promise-mock').default;

```

### Testing
```
Expand Down
100 changes: 100 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var Promise = _interopDefault(require('promise-polyfill'));

const root = typeof window === 'undefined' ? global : window;


function PromiseMock() {
Promise.apply(this, arguments);
}
PromiseMock.prototype = Object.create(Promise.prototype);
Object.keys(Promise).forEach(function (key) {
PromiseMock[key] = Promise[key];
});

// Queue of waiting callbacks
PromiseMock.waiting = [];


/**
* Execute a pending Promise
*/
PromiseMock.run = function run(count) {
var runTimes = count ? count : 1;

if (PromiseMock.waiting.length === 0) {
throw new Error('No Promises waiting. Can\'t Promise.run()')
}

while (runTimes > 0 && PromiseMock.waiting.length > 0) {
PromiseMock.waiting.shift()();
runTimes--;
}
};

/**
* Execute all pending Promises
*/
PromiseMock.runAll = function runAll() {
if (PromiseMock.waiting.length === 0) {
throw new Error('No Promises waiting. Can\'t Promise.run()')
}

while (PromiseMock.waiting.length > 0) {
PromiseMock.run();
}
};

PromiseMock._orginal = null;
PromiseMock.install = function install() {
PromiseMock._original = root.Promise;
PromiseMock._originalImmediate = Promise._immediateFn;
// Update the immediate function to push to queue
Promise._immediateFn = function mockImmediateFn(fn) {
PromiseMock.waiting.push(fn);
};

root.Promise = PromiseMock;
};

PromiseMock.uninstall = function uninstall() {
PromiseMock.clear();
if (PromiseMock._original) {
root.Promise = PromiseMock._original;
}

if (PromiseMock._originalImmediate) {
Promise._immediateFn = PromiseMock._originalImmediate;
}
};

/**
* Get the result of a Promise synchronously, throws on Promise reject
* @param {Promise} promise
* @returns {*}
*/
PromiseMock.getResult = function result(promise) {
var result, error;
promise.then(function (promResult) {
result = promResult;
}, function (promError) {
error = promError;
});
PromiseMock.runAll();
if (error) {
throw error;
}
return result;
};

/**
* Clear all pending Promises
*/
PromiseMock.clear = function clear() {
PromiseMock.waiting = [];
};

module.exports = PromiseMock;
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"name": "promise-mock",
"version": "1.1.2",
"version": "2.0.0",
"description": "Promise mock library for synchronous Promise testing",
"main": "index.js",
"main": "lib/index.js",
"module": "src/index.js",
"scripts": {
"test": "JASMINE_CONFIG_PATH=jasmine.json ./node_modules/jasmine/bin/jasmine.js;"
"test": "JASMINE_CONFIG_PATH=jasmine.json ./node_modules/jasmine/bin/jasmine.js;",
"build": "run-p build:**",
"build:cjs": "rollup -i src/index.js -o lib/index.js -f cjs"
},
"repository": {
"type": "git",
Expand All @@ -17,7 +20,9 @@
},
"homepage": "https://github.com/taylorhakes/promise-mock",
"devDependencies": {
"jasmine": "^2.3.1"
"jasmine": "^2.3.1",
"npm-run-all": "^4.1.2",
"rollup": "^0.52.0"
},
"keywords": [
"promise",
Expand Down
5 changes: 3 additions & 2 deletions index.js → src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Promise = require('promise-polyfill'),
root = typeof window === 'undefined' ? global : window;
import Promise from 'promise-polyfill';

const root = typeof window === 'undefined' ? global : window;


function PromiseMock() {
Expand Down
2 changes: 1 addition & 1 deletion tests/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Promise = require('../index');
var Promise = require('../src/index');

describe('Promise mock', function() {
var oldPromise;
Expand Down

0 comments on commit fc2f9ca

Please sign in to comment.