-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added helpers for defer and custom event
- Loading branch information
Showing
7 changed files
with
126 additions
and
20 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
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
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
module.exports = (hello) => { | ||
window.alert(hello); | ||
const serviceCall = require('./service'); | ||
|
||
return '$'; | ||
module.exports = { | ||
handledPromise: () => serviceCall.get() | ||
.then(response => Object.assign({ success: true }, response)) | ||
.catch(response => Object.assign({ fail: true }, response)), | ||
unhandledPromise: () => serviceCall.get(), | ||
}; |
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 @@ | ||
module.exports = { get: () => Promise.resolve(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,88 @@ | ||
const mockService = { get: jest.fn() }; | ||
|
||
jest.mock('../service', () => mockService); | ||
|
||
const { defer } = require('../../../../src/index'); | ||
const util = require('..'); | ||
|
||
beforeEach(() => { | ||
window.alert = jest.fn(); | ||
describe('When handled promise is being called', () => { | ||
let deferred; | ||
let result; | ||
|
||
beforeEach(() => { | ||
deferred = defer(); | ||
|
||
mockService.get.mockReturnValue(deferred.promise); | ||
|
||
result = util.handledPromise(); | ||
}); | ||
|
||
describe('When call is successful', () => { | ||
let response; | ||
|
||
beforeEach(() => { | ||
response = { hello: 'world' }; | ||
|
||
deferred.resolve(response); | ||
}); | ||
|
||
it('should result with success', () => { | ||
expect(result).resolves.toEqual(Object.assign({ success: true }, response)); | ||
}); | ||
}); | ||
|
||
describe('When call is failed', () => { | ||
let response; | ||
|
||
beforeEach(() => { | ||
response = { world: 'hello' }; | ||
|
||
deferred.reject(response); | ||
}); | ||
|
||
it('should result with success', () => { | ||
expect(result).resolves.toEqual(Object.assign({ fail: true }, response)); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('When util is being called', () => { | ||
const message = 'qwerty'; | ||
describe('When unhandled promise is being called', () => { | ||
let deferred; | ||
let result; | ||
|
||
beforeEach(() => { | ||
result = util(message); | ||
deferred = defer(); | ||
|
||
mockService.get.mockReturnValue(deferred.promise); | ||
|
||
result = util.unhandledPromise(); | ||
}); | ||
|
||
it('should alert with message', () => { | ||
expect(window.alert).toBeCalledWith(message); | ||
describe('When call is successful', () => { | ||
let response; | ||
|
||
beforeEach(() => { | ||
response = { hello: 'world' }; | ||
|
||
deferred.resolve(response); | ||
}); | ||
|
||
it('should result with success', () => { | ||
expect(result).resolves.toEqual(response); | ||
}); | ||
}); | ||
|
||
it('should return dollar sign', () => { | ||
expect(result).toBe('$'); | ||
describe('When call is failed', () => { | ||
let response; | ||
|
||
beforeEach(() => { | ||
response = { world: 'hello' }; | ||
|
||
deferred.reject(response); | ||
}); | ||
|
||
it('should result with success', () => { | ||
expect(result).rejects.toEqual(response); | ||
}); | ||
}); | ||
}); |