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

Commit

Permalink
Ensure that requestData has the correct method and type (#231)
Browse files Browse the repository at this point in the history
* Install testdouble and testdouble-chai

* Ensure that `requestData` has the correct method and type

Closes #229
  • Loading branch information
alexlafroscia authored Jan 24, 2017
1 parent df2f838 commit a55f328
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 154 deletions.
6 changes: 2 additions & 4 deletions addon/mixins/ajax-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,8 @@ export default Mixin.create({
* @return {Promise} The result of the request
*/
_makeRequest(hash) {
const requestData = {
type: hash.type,
url: hash.url
};
const method = hash.method || hash.type || 'GET';
const requestData = { method, type: method, url: hash.url };

if (isJSONAPIContentType(getHeader(hash.headers, 'Content-Type')) && requestData.type !== 'GET') {
if (typeof hash.data === 'object') {
Expand Down
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"ember-cli-shims": "0.1.3",
"mocha": "~2.2.4",
"chai": "~2.3.0",
"ember-mocha-adapter": "~0.3.1"
"ember-mocha-adapter": "~0.3.1",
"testdouble": "testdouble/testdouble.js#^1.4.2"
}
}
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,28 @@
"coveralls": "^2.11.13",
"ember-cli": "2.10.1",
"ember-cli-app-version": "^2.0.0",
"ember-cli-chai": "^0.2.0",
"ember-cli-chai": "^0.3.2",
"ember-cli-code-coverage": "0.3.8",
"ember-cli-dependency-checker": "^1.3.0",
"ember-cli-eslint": "3.0.1",
"ember-cli-htmlbars": "^1.0.10",
"ember-cli-htmlbars-inline-precompile": "^0.3.1",
"ember-cli-inject-live-reload": "^1.4.1",
"ember-cli-mocha": "0.13.0",
"ember-cli-mocha": "^0.13.1",
"ember-cli-pretender": "0.7.0",
"ember-cli-release": "^0.2.9",
"ember-cli-sri": "^2.1.0",
"ember-cli-test-loader": "^1.1.0",
"ember-cli-testdouble": "0.1.0",
"ember-cli-uglify": "^1.2.0",
"ember-data": "^2.10.0",
"ember-disable-prototype-extensions": "^1.1.0",
"ember-export-application-global": "^1.0.5",
"ember-load-initializers": "^0.5.1",
"ember-resolver": "^2.0.3",
"eslint-plugin-ember-suave": "^1.0.0",
"loader.js": "^4.0.10"
"loader.js": "^4.0.10",
"testdouble-chai": "^0.5.0"
},
"engines": {
"node": ">= 0.12.0"
Expand Down
18 changes: 13 additions & 5 deletions tests/unit/mixins/ajax-request-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, beforeEach, afterEach, it } from 'mocha';
import { expect } from 'chai';
import td from 'testdouble';
import wait from 'ember-test-helpers/wait';

import Ember from 'ember';
Expand All @@ -16,6 +17,7 @@ import {
import Pretender from 'pretender';
import { jsonResponse, jsonFactory } from 'dummy/tests/helpers/json';

const { matchers: { anything, contains: matchContains } } = td;
const { A, typeOf } = Ember;

describe('Unit | Mixin | ajax request', function() {
Expand Down Expand Up @@ -370,19 +372,25 @@ describe('Unit | Mixin | ajax request', function() {
});

it('request with method option makes the correct type of request', function() {
const service = new AjaxRequest();
const url = '/posts/1';
const serverResponse = [200, { 'Content-Type': 'application/json' }, JSON.stringify({})];

this.server.get(url, () => {
throw new Error("Shouldn't make an AJAX request");
});
this.server.post(url, () => serverResponse);

this.server.post(url, () => {
return serverResponse;
});
const service = new AjaxRequest();
const handleResponse = td.function('handle response');
const expectedArguments = [
anything(), anything(), anything(), matchContains({ type: 'POST' })
];
service.handleResponse = handleResponse;
td.when(handleResponse(...expectedArguments)).thenReturn({});

return service.request(url, { method: 'POST' });
return service.request(url, { method: 'POST' }).then(() => {
expect(handleResponse).to.be.calledWith(...expectedArguments);
});
});

describe('explicit host in URL', function() {
Expand Down
Loading

0 comments on commit a55f328

Please sign in to comment.