Created
February 8, 2017 20:51
-
-
Save paulsturgess/f1813e0cee2d39ea3b57ca155ec7fee7 to your computer and use it in GitHub Desktop.
Test Axios promise with jasmine-ajax
This file contains hidden or 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
Getting "ReferenceError: getJasmineRequireObj is not defined" while executing the script.