-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor tests to remain 100% coverage with node 7
- Loading branch information
1 parent
f757bb1
commit 036b2b3
Showing
7 changed files
with
120 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export class XMLHttpRequestFactory { | ||
constructor() { | ||
this.shouldFail = false; | ||
this.readyState = 4; | ||
} | ||
|
||
onreadystatechange() {} | ||
open(httpVerb, url) { | ||
if (url === '/path/to/success') { | ||
this.shouldFail = false; | ||
} | ||
|
||
if (url === '/path/to/error') { | ||
this.shouldFail = true; | ||
} | ||
|
||
if (url === '/path/to/not/ready') { | ||
this.shouldFail = false; | ||
this.readyState = 0; | ||
} | ||
} | ||
send() { | ||
if (this.shouldFail === false) { | ||
this.onreadystatechange(); | ||
} | ||
} | ||
} |
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,36 +1,22 @@ | ||
import {loadTemplate} from '../dataProvider'; | ||
|
||
// Add a simple XMLHttpRequest mock which already succeeds to test functionality of the dataProvider | ||
|
||
const initXMLHttpReqestMock = shouldFail => { | ||
class XMLHttpRequestMock { | ||
constructor() { | ||
this.readyState = shouldFail === true ? 0 : 4; | ||
} | ||
onreadystatechange() {} | ||
open() {} | ||
send() { | ||
this.onreadystatechange(); | ||
} | ||
} | ||
window.XMLHttpRequest = XMLHttpRequestMock; | ||
|
||
}; | ||
jest.mock('../httpRequestFactory.js'); | ||
|
||
describe('loadTemplate()', () => { | ||
test('invokes successCallback with template string if template is loaded', () => { | ||
initXMLHttpReqestMock(false); | ||
const spy = jest.fn(); | ||
loadTemplate('', spy); | ||
|
||
loadTemplate('/path/to/success', spy); | ||
expect(spy.mock.calls.length).toBe(1); | ||
}); | ||
|
||
test('does not invoke successCallback if template could not get loaded', () => { | ||
initXMLHttpReqestMock(true); | ||
const spy = jest.fn(); | ||
loadTemplate('', spy); | ||
loadTemplate('/path/to/error', spy); | ||
expect(spy.mock.calls.length).toBe(0); | ||
}); | ||
|
||
test('does not invoke successCallback if template loading is not ready', () => { | ||
const spy = jest.fn(); | ||
loadTemplate('/path/to/not/ready', spy); | ||
expect(spy.mock.calls.length).toBe(0); | ||
}); | ||
}); |
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 @@ | ||
export const XMLHttpRequestFactory = window.XMLHttpRequest; |
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