Created
December 17, 2016 04:06
-
-
Save chengjianhua/08356304f95f477dba6f3cb977a6aa08 to your computer and use it in GitHub Desktop.
the test for ajax which based on axios
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
import chai from 'chai'; | |
import sinon from 'sinon'; | |
import mock from 'mock-require'; | |
import moxios from 'moxios'; | |
mock('v2/models/globals/SessionState', { | |
currentUser: { | |
neoToken: '1234567890' | |
} | |
}); | |
mock('config/api', { | |
baseUrl: '/' | |
}); | |
const ajax = require('v2/utils/ajax').default; | |
const should = chai.should(); | |
describe('Ajax', () => { | |
beforeEach(() => { | |
moxios.install(ajax.instance); | |
}); | |
afterEach(() => { | |
moxios.uninstall(ajax.instance); | |
}); | |
describe('Validate headers', () => { | |
it('should set Authorization header.', (done) => { | |
moxios.stubRequest('/say/hello', { | |
status: 200, | |
responseText: 'hello' | |
}); | |
const onFulfilled = sinon.spy(); | |
ajax({ | |
method: 'get', | |
url: '/say/hello' | |
}) | |
.then(onFulfilled) | |
.catch((error) => { | |
should.not.exist(error); | |
}); | |
moxios.wait(function () { | |
const response = onFulfilled.getCall(0).args[0]; | |
const { request: { headers } } = response; | |
const neoToken = require('v2/models/globals/SessionState').currentUser.neoToken; | |
headers.Authorization.should.equal(`Bearer ${neoToken}`); | |
done(); | |
}); | |
}); | |
}); // describe('Validate headers') | |
describe('Validate arguments[method]', () => { | |
const tests = [ | |
{ method: 'get', expected: 'get' }, | |
{ method: 'Put', expected: 'put' }, | |
{ method: 'Delete', expected: 'delete' }, | |
{ method: 'POST', expected: 'post' } | |
]; | |
tests.forEach(({ method, expected }) => { | |
it(`should return ajax.${expected} when argument[method] is "${method}".`, (done) => { | |
moxios.stubRequest('/hi', { | |
status: 200 | |
}); | |
const onFullfilled = sinon.spy(); | |
ajax({ | |
url: '/hi', | |
method | |
}).then(onFullfilled); | |
moxios.wait(() => { | |
const response = onFullfilled.getCall(0).args[0]; | |
const { request } = response; | |
const { config: { method } } = request; | |
method.should.equal(expected); | |
done(); | |
}); | |
}); | |
}); | |
it('should throw error when request method is not in the specified collection [ get, post, put, delete ].', (done) => { | |
moxios.stubRequest('/hi', { status: 200 }); | |
const onFullfilled = sinon.spy(); | |
const onFailed = sinon.spy(); | |
(function () { | |
ajax({ | |
url: '/hi', | |
method: 'create' | |
}).then(onFullfilled).catch(onFailed); | |
}).should.throw(Error, 'Request method should be one of [ get, post, delete, put ].'); | |
moxios.wait(() => { | |
onFullfilled.called.should.equal(false); | |
onFailed.called.should.equal(false); | |
done(); | |
}); | |
}); | |
it('should throw error when request method is not been set.', (done) => { | |
moxios.stubRequest('/hi', { status: 200 }); | |
const onFullfilled = sinon.spy(); | |
const onFailed = sinon.spy(); | |
(function () { | |
ajax({ | |
url: '/hi' | |
}).then(onFullfilled).catch(onFailed); | |
}).should.throw(Error, 'Request method should be set.'); | |
moxios.wait(() => { | |
onFullfilled.called.should.equal(false); | |
onFailed.called.should.equal(false); | |
done(); | |
}); | |
}); | |
}); // describe('Validate arguments[method]') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment