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

Commit

Permalink
Allow payload response to be a falsey but non-null or undefined value (
Browse files Browse the repository at this point in the history
…#126)

* Allow payload response to be a falsey but non-null or undefined value

* replace integration test with unit tests
  • Loading branch information
SteelBurgher authored and alexlafroscia committed Jul 29, 2016
1 parent badf998 commit b2eb109
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
2 changes: 1 addition & 1 deletion addon/mixins/ajax-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ export default Mixin.create({
* @return {Object | AjaxError} response
*/
handleResponse(status, headers, payload, requestData) {
payload = payload || {};
payload = (payload === null || payload === undefined) ? {} : payload;
const errors = this.normalizeErrorResponse(status, headers, payload);

if (this.isSuccess(status, headers, payload)) {
Expand Down
78 changes: 78 additions & 0 deletions tests/integration/components/ajax-get-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,83 @@ describeComponent(
expect(this.$('.ajax-get li:eq(2)').text()).to.equal('Baz');
});
});

it('clicking Load Data loads data', function() {
const PAYLOAD = [{ title: 'Foo' }, { title: 'Bar' }, { title: 'Baz' }];

this.server.get('/foo', json(200, PAYLOAD), 300);

this.render(hbs`
{{#ajax-get url="/foo" as |data isLoaded|}}
{{#if isLoaded}}
<ul>
{{#each data as |post|}}
<li>{{post.title}}</li>
{{/each}}
</ul>
{{else}}
<button {{action data}}>Load Data</button>
{{/if}}
{{/ajax-get}}
`);

this.$(`.ajax-get button`).click();

return wait().then(() => {
expect(this.$('.ajax-get li:eq(0)').text()).to.equal('Foo');
expect(this.$('.ajax-get li:eq(1)').text()).to.equal('Bar');
expect(this.$('.ajax-get li:eq(2)').text()).to.equal('Baz');
});
});

it('a payload that evaluates falsey but is not null or undefined loads as expected', function() {
const PAYLOAD = 0;

this.server.get('/foo', json(200, PAYLOAD), 300);

this.render(hbs`
{{#ajax-get url="/foo" as |data isLoaded|}}
{{#if isLoaded}}
<p>{{data}}</p>
{{else}}
<button {{action data}}>Load Data</button>
{{/if}}
{{/ajax-get}}
`);

this.$(`.ajax-get button`).click();

return wait().then(() => {
expect(this.$('.ajax-get p').text()).to.equal('0');
});
});

it('clicking Load Data loads data', function() {
const PAYLOAD = [{ title: 'Foo' }, { title: 'Bar' }, { title: 'Baz' }];

this.server.get('/foo', json(200, PAYLOAD), 300);

this.render(hbs`
{{#ajax-get url="/foo" as |data isLoaded|}}
{{#if isLoaded}}
<ul>
{{#each data as |post|}}
<li>{{post.title}}</li>
{{/each}}
</ul>
{{else}}
<button {{action data}}>Load Data</button>
{{/if}}
{{/ajax-get}}
`);

this.$(`.ajax-get button`).click();

return wait().then(() => {
expect(this.$('.ajax-get li:eq(0)').text()).to.equal('Foo');
expect(this.$('.ajax-get li:eq(1)').text()).to.equal('Bar');
expect(this.$('.ajax-get li:eq(2)').text()).to.equal('Baz');
});
});
}
);
19 changes: 19 additions & 0 deletions tests/unit/mixins/ajax-request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,25 @@ describe('Unit | Mixin | ajax request', function() {
expect(req._buildURL('/baz')).to.equal('/baz');
});

it('it doesn\'t reassign payloads which evaluate falsey but are not null or undefined', function() {
const service = new AjaxRequest();

const payloadWithFalseyString = service.handleResponse(200, {}, '');
expect(payloadWithFalseyString).to.be.empty;

const payloadWithFalseyNumber = service.handleResponse(200, {}, 0);
expect(payloadWithFalseyNumber).to.equal(0);

const payloadWithNaN = service.handleResponse(200, {}, NaN);
expect(isNaN(payloadWithNaN)).to.be.ok;

const payloadWithNull = service.handleResponse(200, {}, null);
expect(payloadWithNull).to.be.deep.equal({});

const payloadWithUndefined = service.handleResponse(200, {}, undefined);
expect(payloadWithUndefined).to.be.deep.equal({});
});

describe('JSONP Requests', function() {
it('should make JSONP requests', function() {
this.server.get('/jsonp', function(req) {
Expand Down

0 comments on commit b2eb109

Please sign in to comment.