0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00
ghost/core/client/tests/integration/services/ajax-test.js
Kevin Ansfield 260963e6b1 Replace jQuery-based uploader.js with ember components
no issue
- adds `gh-image-uploader` that handles image uploads in a fully ember fashion and with no dependency on `uploader.js`
- adds `gh-image-uploader-with-preview` that can fully replace the old `gh-uploader`
- replace uses of `gh-uploader` in PSM & TSM with `gh-image-uploader-with-preview`
- updates the editor preview image handling to use the new `gh-image-uploader-with-preview` component
- updates the image upload modal to use `gh-image-uploader` (utilises the `saveButton=false` flag which means the preview has to be handled externally to avoid auto-replacement when typing a URL)
- removes all old `uploader.js` related code
- adds custom `RequestEntityTooLargeError` and `UnsupportedMediaTypeError` errors to our `ajax` service
2016-04-05 12:03:20 +01:00

131 lines
3.8 KiB
JavaScript

import { expect } from 'chai';
import {
describeModule,
it
} from 'ember-mocha';
import Pretender from 'pretender';
import {AjaxError, UnauthorizedError} from 'ember-ajax/errors';
import {RequestEntityTooLargeError, UnsupportedMediaTypeError} from 'ghost/services/ajax';
function stubAjaxEndpoint(server, response = {}, code = 500) {
server.get('/test/', function () {
return [
code,
{'Content-Type': 'application/json'},
JSON.stringify(response)
];
});
}
describeModule(
'service:ajax',
'Integration: Service: ajax',
{
integration: true
},
function () {
let server;
beforeEach(function () {
server = new Pretender();
});
afterEach(function () {
server.shutdown();
});
it('correctly parses single message response text', function (done) {
let error = {message: 'Test Error'};
stubAjaxEndpoint(server, error);
let ajax = this.subject();
ajax.request('/test/').then(() => {
expect(false).to.be.true();
}).catch((error) => {
expect(error.errors).to.equal('Test Error');
done();
});
});
it('correctly parses single error response text', function (done) {
let error = {error: 'Test Error'};
stubAjaxEndpoint(server, error);
let ajax = this.subject();
ajax.request('/test/').then(() => {
expect(false).to.be.true();
}).catch((error) => {
expect(error.errors).to.equal('Test Error');
done();
});
});
it('correctly parses multiple error messages', function (done) {
let error = {errors: ['First Error', 'Second Error']};
stubAjaxEndpoint(server, error);
let ajax = this.subject();
ajax.request('/test/').then(() => {
expect(false).to.be.true();
}).catch((error) => {
expect(error.errors).to.deep.equal(['First Error', 'Second Error']);
done();
});
});
it('returns default error object for non built-in error', function (done) {
stubAjaxEndpoint(server, {});
let ajax = this.subject();
ajax.request('/test/').then(() => {
expect(false).to.be.true;
}).catch((error) => {
expect(error).to.be.instanceOf(AjaxError);
done();
});
});
it('returns known error object for built-in errors', function (done) {
stubAjaxEndpoint(server, '', 401);
let ajax = this.subject();
ajax.request('/test/').then(() => {
expect(false).to.be.true;
}).catch((error) => {
expect(error).to.be.instanceOf(UnauthorizedError);
done();
});
});
it('returns RequestEntityTooLargeError object for 413 errors', function (done) {
stubAjaxEndpoint(server, {}, 413);
let ajax = this.subject();
ajax.request('/test/').then(() => {
expect(false).to.be.true;
}).catch((error) => {
expect(error).to.be.instanceOf(RequestEntityTooLargeError);
done();
});
});
it('returns UnsupportedMediaTypeError object for 415 errors', function (done) {
stubAjaxEndpoint(server, {}, 415);
let ajax = this.subject();
ajax.request('/test/').then(() => {
expect(false).to.be.true;
}).catch((error) => {
expect(error).to.be.instanceOf(UnsupportedMediaTypeError);
done();
});
});
}
);