Skip to content

Instantly share code, notes, and snippets.

@paulsturgess
Created February 8, 2017 20:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save paulsturgess/f1813e0cee2d39ea3b57ca155ec7fee7 to your computer and use it in GitHub Desktop.
Save paulsturgess/f1813e0cee2d39ea3b57ca155ec7fee7 to your computer and use it in GitHub Desktop.
Test Axios promise with jasmine-ajax
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);
});
});
});
@gaonkar-adi
Copy link

Getting "ReferenceError: getJasmineRequireObj is not defined" while executing the script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment