-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
996c44a
commit fda0e6f
Showing
6 changed files
with
171 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import Promise from 'promise' | ||
import PromiseMiddleware from '../index' | ||
|
||
describe('applyMiddleware', () => { | ||
const fetcher = () => new Promise(() => {}) | ||
|
||
let MyWrappedFetcher | ||
beforeAll(() => { | ||
MyWrappedFetcher = new PromiseMiddleware(fetcher) | ||
}) | ||
|
||
describe('applyOnRequestMiddleware', () => { | ||
it('adds onRequestMiddleware', () => { | ||
const middleware = () => {} | ||
MyWrappedFetcher.applyOnRequestMiddleware(middleware) | ||
expect(MyWrappedFetcher._middleware.onRequest).toEqual([middleware]) | ||
}) | ||
}) | ||
|
||
describe('applyOnSuccessMiddleware', () => { | ||
it('adds onSuccessMiddleware', () => { | ||
const middleware = () => {} | ||
MyWrappedFetcher.applyOnSuccessMiddleware(middleware) | ||
expect(MyWrappedFetcher._middleware.onSuccess).toEqual([middleware]) | ||
}) | ||
}) | ||
|
||
describe('applyOnErrorMiddleware', () => { | ||
it('adds onErrorMiddleware', () => { | ||
const middleware = () => {} | ||
MyWrappedFetcher.applyOnErrorMiddleware(middleware) | ||
expect(MyWrappedFetcher._middleware.onError).toEqual([middleware]) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import Promise from 'promise' | ||
import sinon, { assert } from 'sinon' | ||
|
||
import PromiseMiddleware from '../index' | ||
|
||
|
||
describe('callMiddlware', () => { | ||
const sandbox = sinon.sandbox.create() | ||
const fetcher = () => new Promise(() => {}) | ||
let MyWrappedFetcher | ||
|
||
beforeAll(() => { | ||
MyWrappedFetcher = new PromiseMiddleware(fetcher) | ||
}) | ||
|
||
afterEach(() => { | ||
sandbox.restore() | ||
}) | ||
|
||
it('returns a finished state object if no middleware is given', () => { | ||
expect(MyWrappedFetcher._callMiddleware([])).toEqual({ finished: true }) | ||
}) | ||
|
||
it('calls the first middleware, giving the eventProperties and a "stop" cb', () => { | ||
const eventProperties = {} | ||
const middleware = [sandbox.stub().callsFake((eventProperties, stop) => { | ||
expect(eventProperties).toEqual(eventProperties) | ||
expect(stop).toBeInstanceOf(Function) | ||
})] | ||
|
||
MyWrappedFetcher._callMiddleware(middleware, eventProperties) | ||
assert.calledOnce(middleware[0]) | ||
}) | ||
|
||
it('stops middleware execution if any of the middleware call "stop"', () => { | ||
const middlewareThatWillBeCalled = [sandbox.spy(), sandbox.spy()] | ||
const middlewareThatWillNotBeCalled = [sandbox.spy(), sandbox.spy()] | ||
const middlewareThatStopsExecution = (eventProperties, stop) => { | ||
stop() | ||
} | ||
|
||
const middleware = [...middlewareThatWillBeCalled, middlewareThatStopsExecution, ...middlewareThatWillNotBeCalled] | ||
|
||
MyWrappedFetcher._callMiddleware(middleware) | ||
|
||
middlewareThatWillBeCalled.forEach(middleware => { | ||
assert.calledOnce(middleware) | ||
}) | ||
|
||
middlewareThatWillNotBeCalled.forEach(middleware => { | ||
assert.notCalled(middleware) | ||
}) | ||
}) | ||
|
||
it('returns a stopped status if any middlware call "stop"', () => { | ||
const middlewareThatStopsExecution = (eventProperties, stop) => { | ||
stop() | ||
} | ||
|
||
const result = MyWrappedFetcher._callMiddleware([middlewareThatStopsExecution]) | ||
expect(result).toEqual({ wasStopped: true }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import Promise from 'promise' | ||
import sinon, { assert } from 'sinon' | ||
|
||
import PromiseMiddleware from '../index' | ||
|
||
|
||
describe('onEvent', () => { | ||
const sandbox = sinon.sandbox.create() | ||
const fetcher = () => new Promise(() => {}) | ||
let MyWrappedFetcher | ||
|
||
beforeAll(() => { | ||
MyWrappedFetcher = new PromiseMiddleware(fetcher) | ||
}) | ||
|
||
afterEach(() => { | ||
sandbox.restore() | ||
}) | ||
|
||
describe('onRequest', () => { | ||
it('runs onRequest middlware passing in the event properties', () => { | ||
sandbox.spy(MyWrappedFetcher, '_callMiddleware') | ||
|
||
const middleware = () => {} | ||
MyWrappedFetcher._middleware.onRequest = [middleware] | ||
|
||
const eventProperties = {} | ||
MyWrappedFetcher._onRequest(eventProperties) | ||
|
||
assert.calledOnce(MyWrappedFetcher._callMiddleware) | ||
assert.calledWith(MyWrappedFetcher._callMiddleware, [middleware], eventProperties) | ||
}) | ||
}) | ||
|
||
describe('onSuccess', () => { | ||
it('runs onSuccess middlware passing in the event properties', () => { | ||
sandbox.spy(MyWrappedFetcher, '_callMiddleware') | ||
|
||
const middleware = () => {} | ||
MyWrappedFetcher._middleware.onSuccess = [middleware] | ||
|
||
const eventProperties = {} | ||
MyWrappedFetcher._onSuccess(eventProperties) | ||
|
||
assert.calledOnce(MyWrappedFetcher._callMiddleware) | ||
assert.calledWith(MyWrappedFetcher._callMiddleware, [middleware], eventProperties) | ||
}) | ||
}) | ||
|
||
describe('onError', () => { | ||
it('runs onError middlware passing in the event properties', () => { | ||
sandbox.spy(MyWrappedFetcher, '_callMiddleware') | ||
|
||
const middleware = () => {} | ||
MyWrappedFetcher._middleware.onError = [middleware] | ||
|
||
const eventProperties = {} | ||
MyWrappedFetcher._onError(eventProperties) | ||
|
||
assert.calledOnce(MyWrappedFetcher._callMiddleware) | ||
assert.calledWith(MyWrappedFetcher._callMiddleware, [middleware], eventProperties) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters