diff --git a/addon/mixins/ajax-request.js b/addon/mixins/ajax-request.js index 851400a7..e0d08ab1 100644 --- a/addon/mixins/ajax-request.js +++ b/addon/mixins/ajax-request.js @@ -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)) { diff --git a/tests/integration/components/ajax-get-test.js b/tests/integration/components/ajax-get-test.js index 24cd1911..4ac1788f 100644 --- a/tests/integration/components/ajax-get-test.js +++ b/tests/integration/components/ajax-get-test.js @@ -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}} + + {{else}} + + {{/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}} +

{{data}}

+ {{else}} + + {{/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}} + + {{else}} + + {{/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'); + }); + }); } ); diff --git a/tests/unit/mixins/ajax-request-test.js b/tests/unit/mixins/ajax-request-test.js index 68646795..199675f8 100644 --- a/tests/unit/mixins/ajax-request-test.js +++ b/tests/unit/mixins/ajax-request-test.js @@ -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) {