Add missing RequestStub methods to jasmine-ajax (#37804)

* Add a test to jasmine-ajax for RequestStub.andReturn

* Add tests to jasmine-ajax for the missing RequestStub methods

* Add the missing RequestStub methods to jasmine-ajax

* Add myself to the list of jasmine-ajax contributors

* Fix a linter issue in jasmine-ajax

* Address remarks by @sheetalkamat (#37804)

* Add missing parameter to JasmineAjaxRequest.responseError (#37804)
This commit is contained in:
Julian Gonggrijp 2019-09-03 15:14:03 +02:00 committed by Mine Starks
parent e9e404ac8e
commit 1c92cef26e
3 changed files with 52 additions and 6 deletions

View File

@ -1,6 +1,7 @@
// Type definitions for jasmine-ajax 3.1
// Type definitions for jasmine-ajax 3.3
// Project: https://github.com/jasmine/jasmine-ajax
// Definitions by: Louis Grignon <https://github.com/lgrignon>
// Julian Gonggrijp <https://github.com/jgonggrijp>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
@ -25,7 +26,7 @@ interface JasmineAjaxRequest extends XMLHttpRequest {
respondWith(response: JasmineAjaxResponse): void;
responseTimeout(): void;
responseError(): void;
responseError(options?: JasmineAjaxRequestStubErrorOptions): void;
}
interface JasmineAjaxRequestTracker {
@ -36,7 +37,7 @@ interface JasmineAjaxRequestTracker {
mostRecent(): JasmineAjaxRequest;
at(index: number): JasmineAjaxRequest;
filter(urlToMatch: RegExp): JasmineAjaxRequest[];
filter(urlToMatch: Function): JasmineAjaxRequest[];
filter(urlToMatch: (request: JasmineAjaxRequest) => boolean): JasmineAjaxRequest[];
filter(urlToMatch: string): JasmineAjaxRequest[];
}
@ -48,10 +49,20 @@ interface JasmineAjaxRequestStubReturnOptions {
responseHeaders?: { [key: string]: string };
}
interface JasmineAjaxRequestStubErrorOptions {
status?: number;
statusText?: string;
}
interface JasmineAjaxRequestStub {
data?: string;
method?: string;
url: RegExp | string;
query: string;
data: string;
method: string;
andReturn(options: JasmineAjaxRequestStubReturnOptions): void;
andError(options: JasmineAjaxRequestStubErrorOptions): void;
andTimeout(): void;
andCallFunction(functionToCall: (request: JasmineAjaxRequest) => void): void;
matches(fullUrl: string, data: string, method: string): boolean;
}

View File

@ -1431,6 +1431,42 @@ describe('RequestStub', () => {
expect(stub)['toMatchRequest']('/foo', 'baz=quux&foo=bar');
expect(stub).not['toMatchRequest']('/foo', 'foo=bar');
});
it('has methods', () => {
jasmine.Ajax.stubRequest('/foo').andReturn({
status: 200,
contentType: 'application/json',
responseText: '{"success": true}',
responseHeaders: { 'X-Example': 'a value' },
});
jasmine.Ajax.stubRequest('/bar').andReturn({});
jasmine.Ajax.stubRequest('/baz').andError({
status: 400,
statusText: 'Invalid',
});
jasmine.Ajax.stubRequest('/foobar').andError({});
jasmine.Ajax.stubRequest('/foobaz').andTimeout();
jasmine.Ajax.stubRequest('/barbaz').andCallFunction((xhr) => {
xhr.url === '/barbaz';
xhr.method === 'POST';
xhr.params === {};
xhr.username === 'jane_coder';
xhr.password === '12345';
xhr.requestHeaders === {Accept: 'application/json'};
xhr.data() === {query: 'bananas'};
xhr.respondWith({
status: 200,
contentType: 'application/json',
responseText: '{"success": true}',
responseHeaders: { 'X-Example': 'a value' },
});
xhr.responseTimeout();
xhr.responseError({
status: 400,
statusText: 'Invalid',
});
});
});
});
describe('RequestTracker', () => {

View File

@ -1,7 +1,6 @@
{
"extends": "dtslint/dt.json",
"rules": {
"ban-types": false,
"unified-signatures": false
}
}