Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion addon/mixins/ajax-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export default Mixin.create({
request(url, options) {
const hash = this.options(url, options);
return new Promise((resolve, reject) => {
this.raw(url, hash)
this._makeRequest(url, hash)
.then(({ response }) => {
resolve(response);
})
Expand All @@ -217,6 +217,19 @@ export default Mixin.create({
*/
raw(url, options) {
const hash = this.options(url, options);
return this._makeRequest(url, hash);
},

/**
* Shared method to actually make an AJAX request
*
* @method _makeRequest
* @private
* @param {string} url The url to make a request to
* @param {Object} hash The options for the request
* @return {Promise} The result of the request
*/
_makeRequest(url, hash) {
const requestData = {
type: hash.type,
url: hash.url
Expand Down
32 changes: 14 additions & 18 deletions tests/acceptance/ajax-get-test.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,47 @@
import { describe, beforeEach, afterEach, it } from 'mocha';
import { assert, expect } from 'chai';
import { expect } from 'chai';

import destroyApp from 'dummy/tests/helpers/destroy-app';
import startApp from 'dummy/tests/helpers/start-app';

import Pretender from 'pretender';
import { jsonFactory as json } from 'dummy/tests/helpers/json';

const { equal, ok } = assert;

let server, application;

describe('Acceptance | ajax-get component', function() {
beforeEach(function() {
server = new Pretender();
application = startApp();
this.server = new Pretender();
this.application = startApp();
});

afterEach(function() {
server.shutdown();
destroyApp(application);
this.server.shutdown();
destroyApp(this.application);
});

it('waiting for a route with async widget', function() {
it('waits for a route with async widget', function() {
const PAYLOAD = [{ title: 'Foo' }, { title: 'Bar' }, { title: 'Baz' }];

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

visit('/');

andThen(function() {
equal(currentURL(), '/');
ok(find('.ajax-get').length === 1);
expect(currentURL()).to.equal('/');
expect(find('.ajax-get').length).to.equal(1);
});

click('button:contains(Load Data)');

andThen(function() {
equal(find('.ajax-get li:eq(0)').text(), 'Foo');
equal(find('.ajax-get li:eq(1)').text(), 'Bar');
equal(find('.ajax-get li:eq(2)').text(), 'Baz');
expect(find('.ajax-get li:eq(0)').text()).to.equal('Foo');
expect(find('.ajax-get li:eq(1)').text()).to.equal('Bar');
expect(find('.ajax-get li:eq(2)').text()).to.equal('Baz');
});
});

it(`Ajax failure doesn't bubble up to console.` , function() {
it(`catches errors before they bubble to the console` , function() {
const errorMessage = 'Not Found';
server.get('/posts', json(404, errorMessage), 300);
this.server.get('/posts', json(404, errorMessage), 300);

visit('/');

Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/ember-data-integration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('Acceptance | ember data integration', function() {
destroyApp(application);
});

it('ember data adapter uses ember-ajax mixin', function() {
it('can apply the Ember Ajax mixin to an Ember Data adapter', function() {
server.get('api/posts/1', function() {
return jsonResponse(200, {
data: {
Expand Down
17 changes: 7 additions & 10 deletions tests/integration/components/ajax-get-test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { describeComponent, it } from 'ember-mocha';
import { beforeEach, afterEach } from 'mocha';
import { assert } from 'chai';
import { expect } from 'chai';

import Pretender from 'pretender';
import { jsonFactory as json } from 'dummy/tests/helpers/json';
import wait from 'ember-test-helpers/wait';

import hbs from 'htmlbars-inline-precompile';

const { equal } = assert;

let server;
describeComponent(
'ajax-get',
'AjaxGetComponent',
Expand All @@ -19,17 +16,17 @@ describeComponent(
},
function() {
beforeEach(function() {
server = new Pretender();
this.server = new Pretender();
});

afterEach(function() {
server.shutdown();
this.server.shutdown();
});

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

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

this.render(hbs`
{{#ajax-get url="/foo" as |data isLoaded|}}
Expand All @@ -48,9 +45,9 @@ describeComponent(
this.$(`.ajax-get button`).click();

return wait().then(() => {
equal(this.$('.ajax-get li:eq(0)').text(), 'Foo');
equal(this.$('.ajax-get li:eq(1)').text(), 'Bar');
equal(this.$('.ajax-get li:eq(2)').text(), 'Baz');
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');
});
});
}
Expand Down
39 changes: 18 additions & 21 deletions tests/integration/components/async-widget-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@ import hbs from 'htmlbars-inline-precompile';

import { describeComponent, it } from 'ember-mocha';
import { beforeEach, afterEach } from 'mocha';
import { assert } from 'chai';
import { expect } from 'chai';

import AjaxService from 'ember-ajax/services/ajax';
import Pretender from 'pretender';
import { jsonFactory as json } from 'dummy/tests/helpers/json';
import wait from 'ember-test-helpers/wait';

const { deepEqual, equal, throws } = assert;
const {
Component,
Service,
inject,
computed
} = Ember;

import AjaxService from 'ember-ajax/services/ajax';
import Pretender from 'pretender';
import { jsonFactory as json } from 'dummy/tests/helpers/json';
import wait from 'ember-test-helpers/wait';

const PAYLOAD = { posts: [ { id: 1, title: 'hello world' } ] };
let server;

describeComponent(
'async-widget',
Expand All @@ -29,15 +26,15 @@ describeComponent(
},
function() {
beforeEach(function() {
server = new Pretender();
this.server = new Pretender();
});

afterEach(function() {
server.shutdown();
this.server.shutdown();
});

it('service injected in component', function() {
server.get('/posts', json(200, PAYLOAD));
this.server.get('/posts', json(200, PAYLOAD));

const authToken = 'foo';
this.register('service:session', Service.extend({ authToken }));
Expand Down Expand Up @@ -85,14 +82,14 @@ describeComponent(
this.render(hbs`{{async-widget id="async-widget" url="/posts"}}`);
return component.loadData().then(function(response) {
component.set('hello', 'world');
equal(component.get('helloStyle'), 'hello world', 'run loop is not necessary');
deepEqual(receivedHeaders[0], ['authToken', 'foo'], 'token was used session');
deepEqual(response, PAYLOAD, 'recieved PAYLOAD');
expect(component.get('helloStyle')).to.equal('hello world');
expect(receivedHeaders[0]).to.deep.equal(['authToken', 'foo']);
expect(response).to.deep.equal(PAYLOAD);
});
});

it.skip('error thrown in service can be caught in test', function() {
server.post('/posts/1', json(404, { error: 'not found' }), 200);
this.server.post('/posts/1', json(404, { error: 'not found' }), 200);

this.register('service:ajax', AjaxService.extend({
customPOST(url) {
Expand All @@ -113,13 +110,13 @@ describeComponent(
{{/async-widget}}`
);

throws(function() {
expect(function() {
this.$('.async-widget').click();
});
}).to.throw();
});

it('waiting for promises to complete', function() {
server.get('/foo', json(200, { foo: 'bar' }), 300);
this.server.get('/foo', json(200, { foo: 'bar' }), 300);

this.register('component:async-widget', Component.extend({
layout: hbs`{{yield foo}}`,
Expand All @@ -134,11 +131,11 @@ describeComponent(

this.render(hbs`{{#async-widget classNames="async-widget" as |foo|}}Got: {{foo}} for foo{{/async-widget}}`);

equal(this.$('.async-widget').text(), 'Got: foo for foo');
expect(this.$('.async-widget').text()).to.equal('Got: foo for foo');
this.$('.async-widget').click();

return wait().then(() => {
equal(this.$('.async-widget').text(), 'Got: bar for foo');
expect(this.$('.async-widget').text()).to.equal('Got: bar for foo');
});
});
}
Expand Down
Loading