Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Fix issues with the isString helper
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlafroscia committed Mar 27, 2017
1 parent 723dd66 commit 86ab8d0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 21 deletions.
2 changes: 1 addition & 1 deletion addon/-private/utils/is-string.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default function isString() {
export default function isString(object) {
return typeof object === 'string';
}
30 changes: 14 additions & 16 deletions addon/mixins/legacy/normalize-error-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,21 @@ export default Mixin.create({
};
}
});
} else if (isString(payload)) {
return [
{
status: `${status}`,
title: payload
}
];
} else {
if (isString(payload)) {
return [
{
status: `${status}`,
title: payload
}
];
} else {
return [
{
status: `${status}`,
title: payload.title || 'The backend responded with an error',
detail: payload
}
];
}
return [
{
status: `${status}`,
title: payload.title || 'The backend responded with an error',
detail: payload
}
];
}
}
});
2 changes: 1 addition & 1 deletion tests/unit/-private/promise-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, beforeEach } from 'mocha';
import { describe, it } from 'mocha';
import { expect } from 'chai';

import AJAXPromise from 'ember-ajax/-private/promise';
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/-private/utils/is-string-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, it } from 'mocha';
import { expect } from 'chai';
import isString from 'ember-ajax/-private/utils/is-string';

describe('-private/utils/is-string', function() {
it('detects when something is a string', function() {
expect(isString('foo')).to.be.ok;
});

describe('detecting something is not a string', function() {
it('handles a number', function() {
expect(isString(3)).not.to.be.ok;
});

it('handles `null`', function() {
expect(isString(null)).not.to.be.ok;
});

it('handles `undefined`', function() {
expect(isString(undefined)).not.to.be.ok;
});
});
});
16 changes: 13 additions & 3 deletions tests/unit/mixins/legacy/normalize-error-response-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import LegacyNormalizeErrorResponseMixin from 'ember-ajax/mixins/legacy/normaliz

const { Object: EmberObject } = Ember;
const AjaxRequest = EmberObject.extend(LegacyNormalizeErrorResponseMixin);
const service = new AjaxRequest();

describe('Unit | Mixin | legacy/normalize error response', function() {
it('formats the error response according to the legacy format', function() {
const service = new AjaxRequest();

it('handles JSON:API formatted error objects', function() {
const jsonApiError = service.normalizeErrorResponse(400, {}, {
errors: [
{ status: 400, title: 'Foo' },
Expand All @@ -20,7 +19,9 @@ describe('Unit | Mixin | legacy/normalize error response', function() {
{ status: '400', title: 'Foo' },
{ status: '400', title: 'Foo' }
]);
});

it('handles an object with an array of error strings', function() {
const payloadWithErrorStrings = service.normalizeErrorResponse(400, {}, {
errors: [
'This is an error',
Expand All @@ -31,7 +32,9 @@ describe('Unit | Mixin | legacy/normalize error response', function() {
{ status: '400', title: 'This is an error' },
{ status: '400', title: 'This is another error' }
]);
});

it('handles an array of error objects', function() {
const payloadArrayOfObjects = service.normalizeErrorResponse(400, {}, [
{ status: 400, title: 'Foo' },
{ status: 400, title: 'Bar' }
Expand All @@ -40,23 +43,30 @@ describe('Unit | Mixin | legacy/normalize error response', function() {
{ status: '400', title: 'Foo', detail: { status: 400, title: 'Foo' } },
{ status: '400', title: 'Bar', detail: { status: 400, title: 'Bar' } }
]);
});

it('handles an array of strings', function() {
const payloadArrayOfStrings = service.normalizeErrorResponse(400, {}, [
'Foo', 'Bar'
]);
expect(payloadArrayOfStrings).to.deep.equal([
{ status: '400', title: 'Foo' },
{ status: '400', title: 'Bar' }
]);
});

it('handles a string', function() {
const payloadIsString = service.normalizeErrorResponse(400, {}, 'Foo');

expect(payloadIsString).to.deep.equal([
{
status: '400',
title: 'Foo'
}
]);
});

it('handles an arbitrary object', function() {
const payloadIsObject = service.normalizeErrorResponse(400, {}, {
title: 'Foo'
});
Expand Down

0 comments on commit 86ab8d0

Please sign in to comment.