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

Commit

Permalink
fix: tests for type information
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-north committed Mar 22, 2019
1 parent c178c5e commit bcc97da
Show file tree
Hide file tree
Showing 13 changed files with 785 additions and 207 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ tmp/

# TypeScript Declarations
types/
type-tests/
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ language: node_js
node_js:
# we recommend testing addons with the same minimum supported node version as Ember CLI
# so that your addon works for all apps
- "6"
- '6'

sudo: false
dist: trusty
Expand All @@ -15,6 +15,7 @@ cache:
yarn: true
directories:
- $HOME/.npm
- $HOME/.dts

env:
global:
Expand Down Expand Up @@ -46,6 +47,10 @@ jobs:
include:
- stage: lint
script: yarn lint:js
- stage: lint
name: 'type information tests'
node_js: 10
script: ./node_modules/.bin/dtslint type-tests
- stage: lint
script: yarn commitlint-travis
- stage: lint
Expand Down
1 change: 0 additions & 1 deletion addon/-private/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export default class AJAXPromise<T> extends Promise<T> {
) => void,
label?: string
) {
// @ts-ignore
super(executor, label);
}

Expand Down
134 changes: 73 additions & 61 deletions addon/mixins/ajax-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,75 +285,87 @@ export default Mixin.create({

const promise = new AJAXPromise<RawResponse>((resolve, reject) => {
jqXHR
.done((payload, textStatus, jqXHR) => {
const response = this.handleResponse(
jqXHR.status,
parseResponseHeaders(jqXHR.getAllResponseHeaders()),
payload,
requestData
);

if (isAjaxError(response)) {
const rejectionParam: RawErrorResponse = {
payload,
textStatus,
jqXHR,
response
};
run.join(null, reject, rejectionParam);
} else {
const resolutionParam: RawResponse = {
payload,
textStatus,
jqXHR,
response
};
run.join(null, resolve, resolutionParam);
}
})
.fail((jqXHR, textStatus, errorThrown) => {
runInDebug(function() {
const message = `The server returned an empty string for ${
requestData.type
} ${
requestData.url
}, which cannot be parsed into a valid JSON. Return either null or {}.`;
const validJSONString = !(
textStatus === 'parsererror' && jqXHR.responseText === ''
);

warn(message, validJSONString, {
id: 'ds.adapter.returned-empty-string-as-JSON'
});
});

const payload =
this.parseErrorResponse(jqXHR.responseText) || errorThrown;
let response;

if (textStatus === 'timeout') {
response = new TimeoutError();
} else if (textStatus === 'abort') {
response = new AbortError();
} else {
response = this.handleResponse<typeof payload>(
.done(
(
payload: any,
textStatus: JQuery.Ajax.SuccessTextStatus,
jqXHR: JQuery.jqXHR
) => {
const response = this.handleResponse(
jqXHR.status,
parseResponseHeaders(jqXHR.getAllResponseHeaders()),
payload,
requestData
);

if (isAjaxError(response)) {
const rejectionParam: RawErrorResponse = {
payload,
textStatus,
jqXHR,
response
};
run.join(null, reject, rejectionParam);
} else {
const resolutionParam: RawResponse = {
payload,
textStatus,
jqXHR,
response
};
run.join(null, resolve, resolutionParam);
}
}
)
.fail(
(
jqXHR: JQuery.jqXHR,
textStatus: JQuery.Ajax.TextStatus,
errorThrown: string
) => {
runInDebug(function() {
const message = `The server returned an empty string for ${
requestData.type
} ${
requestData.url
}, which cannot be parsed into a valid JSON. Return either null or {}.`;
const validJSONString = !(
textStatus === 'parsererror' && jqXHR.responseText === ''
);

warn(message, validJSONString, {
id: 'ds.adapter.returned-empty-string-as-JSON'
});
});

const rejectionParam: RawErrorResponse = {
payload,
textStatus,
jqXHR,
errorThrown,
response
};
const payload =
this.parseErrorResponse(jqXHR.responseText) || errorThrown;
let response;

if (textStatus === 'timeout') {
response = new TimeoutError();
} else if (textStatus === 'abort') {
response = new AbortError();
} else {
response = this.handleResponse<typeof payload>(
jqXHR.status,
parseResponseHeaders(jqXHR.getAllResponseHeaders()),
payload,
requestData
);
}

run.join(null, reject, rejectionParam);
})
const rejectionParam: RawErrorResponse = {
payload,
textStatus,
jqXHR,
errorThrown,
response
};

run.join(null, reject, rejectionParam);
}
)
.always(() => {
pendingRequestCount = pendingRequestCount - 1;
});
Expand Down
2 changes: 1 addition & 1 deletion addon/utils/ajax.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import jQuery from 'jquery';
import * as jQuery from 'jquery';

const ajax =
typeof FastBoot === 'undefined' ? jQuery.ajax : FastBoot.require('najax');
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,14 @@
"@types/ember": "^3.0.24",
"@types/ember-data": "^3.1.1",
"@types/ember-mocha": "^0.14.4",
"@types/ember-test-helpers": "^1.0.3",
"@types/ember-testing-helpers": "^0.0.3",
"@types/ember__test-helpers": "^0.7.5",
"@types/mocha": "^5.2.5",
"@types/node": "^10.3.3",
"@types/rsvp": "^4.0.2",
"broccoli-asset-rev": "^2.7.0",
"chai": "^4.1.2",
"debug": "^3.1.0",
"dtslint": "~0.4.0",
"ember-cli": "~3.1.4",
"ember-cli-chai": "^0.5.0",
"ember-cli-dependency-checker": "^2.1.1",
Expand Down Expand Up @@ -93,6 +92,7 @@
"husky": "^1.0.0-rc.9",
"lint-staged": "^7.2.0",
"loader.js": "^4.7.0",
"natives": "^1.1.6",
"prettier": "^1.13.5",
"standard-version": "^4.4.0",
"testdouble": "^3.8.1",
Expand Down Expand Up @@ -138,5 +138,8 @@
"ember-source",
"loader.js"
]
},
"toolchain": {
"node": "8.15.1"
}
}
1 change: 1 addition & 0 deletions type-tests/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TypeScript Version: 2.8
44 changes: 44 additions & 0 deletions type-tests/tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Ember from 'ember';
import AjaxService from 'ember-ajax/services/ajax';

// eslint-ignore-next-line
declare global {
const FastBoot: any;
}

Ember.Route.extend({
ajax: Ember.inject.service('ajax'),
model() {
return this.get('ajax').request('/posts');
}
});

Ember.Controller.extend({
ajax: Ember.inject.service('ajax'),
actions: {
sendRequest() {
this.get('ajax').headers; // $ExpectType Headers | undefined

return this.get('ajax').request('/posts', {
method: 'POST',
data: {
foo: 'bar'
}
});
}
}
});

AjaxService.extend({
session: Ember.inject.service(),
headers: Ember.computed('session.authToken', {
get() {
const headers: { [k: string]: string | undefined } = {};
const authToken = this.get('session.authToken');
if (authToken) {
headers['auth-token'] = authToken;
}
return headers;
}
})
});
22 changes: 22 additions & 0 deletions type-tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6", "dom"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noEmit": true,
"downlevelIteration": true,
"types": [],

// If the library is an external module (uses `export`), this allows your test file to import "mylib" instead of "./index".
// If the library is global (cannot be imported via `import` or `require`), leave this out.
"baseUrl": ".",
"paths": {
"ember-ajax": ["../addon"],
"ember-ajax/*": ["../addon/*"]
}
},
"files": ["tests.ts"]
}
7 changes: 7 additions & 0 deletions type-tests/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "dtslint/dtslint.json", // Or "dtslint/dt.json" if on DefinitelyTyped
"rules": {
"semicolon": false,
"indent": false
}
}
6 changes: 0 additions & 6 deletions types/ember-data.d.ts

This file was deleted.

4 changes: 4 additions & 0 deletions types/ember-data/types/registries/model.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// see https://github.com/typed-ember/ember-cli-typescript#fixing-the-ember-data-error-ts2344-problem
export default interface ModelRegistry {
[key: string]: any;
}
Loading

0 comments on commit bcc97da

Please sign in to comment.