-
-
Save Austio/6481c44b9b8db1206ac7f2d5cb3a7251 to your computer and use it in GitHub Desktop.
Test Axios promise with jasmine-ajax
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 Service from 'path/to/service'; | |
import 'jasmine-ajax' | |
describe('Service', () => { | |
let request, promise; | |
let instance = Service; | |
let payload = {foo:'bar'}; | |
let path = '/path'; | |
let callback = jasmine.createSpy('callback'); | |
beforeEach(() => { | |
jasmine.Ajax.install(); | |
}) | |
afterEach(() => { | |
jasmine.Ajax.uninstall(); | |
}); | |
describe('get', () => { | |
it('sends a get request and calls the callback with the response', (done) => { | |
promise = instance.get(path, callback); | |
promise.then((result) => { | |
expect(callback).toHaveBeenCalledWith(200, 'foo'); | |
done(); | |
}); | |
setTimeout(function () { | |
request = jasmine.Ajax.requests.mostRecent(); | |
expect(request.url).toBe(path); | |
expect(request.method).toBe('GET'); | |
request.respondWith({ | |
"status": 200, | |
"contentType": 'text/plain', | |
"responseText": 'foo' | |
}) | |
}, 0); | |
}) | |
}); | |
describe('post', () => { | |
it('calls request with the correct args and calls the callback with the response', (done) => { | |
promise = instance.post(path, payload, callback); | |
promise.then((result) => { | |
expect(callback).toHaveBeenCalledWith(200, payload); | |
done(); | |
}); | |
setTimeout(function () { | |
request = jasmine.Ajax.requests.mostRecent(); | |
expect(request.url).toBe(path); | |
expect(request.method).toBe('POST'); | |
expect(request.responseType).toBe('json'); | |
expect(request.data()).toEqual(payload); | |
request.respondWith({ | |
"status": 200, | |
"contentType": 'text/plain', | |
"responseText": JSON.stringify(payload) | |
}) | |
}, 0); | |
}); | |
}); | |
describe('patch', () => { | |
it('calls request with the correct args and calls the callback with the response', (done) => { | |
promise = instance.patch(path, payload, callback); | |
promise.then((result) => { | |
expect(callback).toHaveBeenCalledWith(200, payload); | |
done(); | |
}); | |
setTimeout(function () { | |
var request = jasmine.Ajax.requests.mostRecent(); | |
expect(request.url).toBe(path); | |
expect(request.method).toBe('PATCH'); | |
expect(request.responseType).toBe('json'); | |
expect(request.data()).toEqual(payload); | |
request.respondWith({ | |
"status": 200, | |
"contentType": 'text/plain', | |
"responseText": JSON.stringify(payload) | |
}) | |
}, 0); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment