mirror of
https://github.com/axios/axios.git
synced 2026-04-13 02:51:56 +08:00
101 lines
2.2 KiB
JavaScript
101 lines
2.2 KiB
JavaScript
describe('instance', function () {
|
|
beforeEach(function () {
|
|
jasmine.Ajax.install();
|
|
});
|
|
|
|
afterEach(function () {
|
|
jasmine.Ajax.uninstall();
|
|
});
|
|
|
|
it('should have the same methods as default instance', function () {
|
|
var instance = axios.create();
|
|
|
|
for (var prop in axios) {
|
|
if ([
|
|
'Axios',
|
|
'create',
|
|
'Cancel',
|
|
'CancelToken',
|
|
'isCancel',
|
|
'all',
|
|
'spread',
|
|
'default'].indexOf(prop) > -1) {
|
|
continue;
|
|
}
|
|
expect(typeof instance[prop]).toBe(typeof axios[prop]);
|
|
}
|
|
});
|
|
|
|
it('should make an http request without verb helper', function (done) {
|
|
var instance = axios.create();
|
|
|
|
instance('/foo');
|
|
|
|
getAjaxRequest().then(function (request) {
|
|
expect(request.url).toBe('/foo');
|
|
done();
|
|
});
|
|
});
|
|
|
|
it('should make an http request', function (done) {
|
|
var instance = axios.create();
|
|
|
|
instance.get('/foo');
|
|
|
|
getAjaxRequest().then(function (request) {
|
|
expect(request.url).toBe('/foo');
|
|
done();
|
|
});
|
|
});
|
|
|
|
it('should use instance options', function (done) {
|
|
var instance = axios.create({ timeout: 1000 });
|
|
|
|
instance.get('/foo');
|
|
|
|
getAjaxRequest().then(function (request) {
|
|
expect(request.timeout).toBe(1000);
|
|
done();
|
|
});
|
|
});
|
|
|
|
it('should have defaults.headers', function () {
|
|
var instance = axios.create({
|
|
baseURL: 'https://api.example.com'
|
|
});
|
|
|
|
expect(typeof instance.defaults.headers, 'object');
|
|
expect(typeof instance.defaults.headers.common, 'object');
|
|
});
|
|
|
|
it('should have interceptors on the instance', function (done) {
|
|
axios.interceptors.request.use(function (config) {
|
|
config.foo = true;
|
|
return config;
|
|
});
|
|
|
|
var instance = axios.create();
|
|
instance.interceptors.request.use(function (config) {
|
|
config.bar = true;
|
|
return config;
|
|
});
|
|
|
|
var response;
|
|
instance.get('/foo').then(function (res) {
|
|
response = res;
|
|
});
|
|
|
|
getAjaxRequest().then(function (request) {
|
|
request.respondWith({
|
|
status: 200
|
|
});
|
|
|
|
setTimeout(function () {
|
|
expect(response.config.foo).toEqual(undefined);
|
|
expect(response.config.bar).toEqual(true);
|
|
done();
|
|
}, 100);
|
|
});
|
|
});
|
|
});
|