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
Showing
4 changed files
with
67 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,67 @@ | ||
import * as utils from '../../src/utils/http.utils' | ||
|
||
describe('http.utils', () => { | ||
let xhr, requests, server | ||
describe('#request ', () => { | ||
let xhr, requests, server | ||
before(function () { | ||
xhr = sinon.useFakeXMLHttpRequest() | ||
requests = [] | ||
xhr.onCreate = function (req) { | ||
requests.push(req) | ||
} | ||
server = sinon.fakeServer.create() | ||
}) | ||
|
||
before(function () { | ||
xhr = sinon.useFakeXMLHttpRequest() | ||
requests = [] | ||
xhr.onCreate = function (req) { | ||
requests.push(req) | ||
} | ||
server = sinon.fakeServer.create() | ||
}) | ||
after(function () { | ||
xhr.restore() | ||
server.restore() | ||
}) | ||
|
||
after(function () { | ||
xhr.restore() | ||
server.restore() | ||
}) | ||
it('should be able to get with success callback', () => { | ||
const then = sinon.spy() | ||
const always = sinon.spy() | ||
utils.request('/some/path') | ||
.then(then) | ||
.always(always) | ||
server.requests[0].respond( | ||
200, | ||
{ 'Content-Type': 'application/json' }, | ||
JSON.stringify([{ id: 1, text: 'Provide examples', done: true }]) | ||
) | ||
sinon.assert.called(then) | ||
sinon.assert.called(always) | ||
}) | ||
|
||
it('should be able to get with success callback', () => { | ||
const then = sinon.spy() | ||
const always = sinon.spy() | ||
utils.getRequest('/some/path') | ||
.then(then) | ||
.always(always) | ||
server.requests[0].respond( | ||
200, | ||
{'Content-Type': 'application/json'}, | ||
JSON.stringify([{id: 1, text: 'Provide examples', done: true}]) | ||
) | ||
sinon.assert.called(then) | ||
sinon.assert.called(always) | ||
}) | ||
it('should be able to post with success callback', () => { | ||
const then = sinon.spy() | ||
const always = sinon.spy() | ||
utils.request('/some/path', 'POST') | ||
.then(then) | ||
.always(always) | ||
server.requests[1].respond( | ||
200, | ||
{ 'Content-Type': 'application/json' }, | ||
JSON.stringify([{ id: 1, text: 'Provide examples', done: true }]) | ||
) | ||
sinon.assert.called(then) | ||
sinon.assert.called(always) | ||
}) | ||
|
||
it('should be able to get with fail callback', () => { | ||
const then = sinon.spy() | ||
const err = sinon.spy() | ||
const always = sinon.spy() | ||
utils.getRequest('/some/path') | ||
.then(then) | ||
.catch(err) | ||
.always(always) | ||
server.requests[1].respond( | ||
500, | ||
{'Content-Type': 'application/json'}, | ||
JSON.stringify([{id: 1, text: 'Provide examples', done: true}]) | ||
) | ||
sinon.assert.called(err) | ||
sinon.assert.called(always) | ||
it('should be able to get with fail callback', () => { | ||
const then = sinon.spy() | ||
const err = sinon.spy() | ||
const always = sinon.spy() | ||
utils.request('/some/path') | ||
.then(then) | ||
.catch(err) | ||
.always(always) | ||
server.requests[2].respond( | ||
500, | ||
{ 'Content-Type': 'application/json' }, | ||
JSON.stringify([{ id: 1, text: 'Provide examples', done: true }]) | ||
) | ||
sinon.assert.called(err) | ||
sinon.assert.called(always) | ||
}) | ||
}) | ||
}) |