Skip to content

Commit

Permalink
Test list verb, add type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
ctimmerm committed Dec 25, 2018
1 parent 221f61a commit 027a821
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
1 change: 1 addition & 0 deletions test/basics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe('MockAdapter basics', function() {
expect(mock.onDelete).to.be.a('function');
expect(mock.onPatch).to.be.a('function');
expect(mock.onOptions).to.be.a('function');
expect(mock.onList).to.be.a('function');
});

it('mocks requests', function() {
Expand Down
27 changes: 19 additions & 8 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { AxiosAdapter, AxiosInstance, AxiosRequestConfig } from "axios";
import { AxiosAdapter, AxiosInstance, AxiosRequestConfig } from 'axios';

type CallbackResponseSpecFunc = (config: AxiosRequestConfig) => any[] | Promise<any[]>;
type CallbackResponseSpecFunc = (
config: AxiosRequestConfig
) => any[] | Promise<any[]>;

type ResponseSpecFunc = (statusOrCallback: number | CallbackResponseSpecFunc, data?: any, headers?: any) => MockAdapter;
type ResponseSpecFunc = (
statusOrCallback: number | CallbackResponseSpecFunc,
data?: any,
headers?: any
) => MockAdapter;

interface RequestHandler {
reply: ResponseSpecFunc;
replyOnce: ResponseSpecFunc;
timeoutOnce: ResponseSpecFunc;
networkErrorOnce: ResponseSpecFunc;

passThrough(): void;
networkError(): void;
timeout(): void;
passThrough(): MockAdapter;
networkError(): MockAdapter;
timeout(): MockAdapter;
}

interface MockAdapterOptions {
Expand All @@ -22,15 +28,19 @@ interface MockAdapterOptions {
interface RequestDataMatcher {
[index: string]: any;
params?: {
[index: string]: any,
[index: string]: any;
};
}

interface HeadersMatcher {
[header: string]: string;
}

type RequestMatcherFunc = (matcher?: string | RegExp, body?: string | RequestDataMatcher, headers?: HeadersMatcher) => RequestHandler;
type RequestMatcherFunc = (
matcher?: string | RegExp,
body?: string | RequestDataMatcher,
headers?: HeadersMatcher
) => RequestHandler;

declare class MockAdapter {
constructor(axiosInstance: AxiosInstance, options?: MockAdapterOptions);
Expand All @@ -45,6 +55,7 @@ declare class MockAdapter {
onHead: RequestMatcherFunc;
onDelete: RequestMatcherFunc;
onPatch: RequestMatcherFunc;
onList: RequestMatcherFunc;
onAny: RequestMatcherFunc;
}

Expand Down
45 changes: 24 additions & 21 deletions types/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';

const instance = axios.create();
const mock = new MockAdapter(instance);
Expand All @@ -10,7 +10,7 @@ namespace AllowsConstructing {

namespace AllowsConstructingWithOptions {
new MockAdapter(instance, {
delayResponse: 2000,
delayResponse: 2000
});
}

Expand All @@ -33,6 +33,7 @@ namespace SupportsAllHttpVerbs {
mock.onHead;
mock.onDelete;
mock.onPatch;
mock.onList;
}

namespace SupportsAnyVerb {
Expand All @@ -44,29 +45,29 @@ namespace AllowsVerbOnlyMatcher {
}

namespace AllowsUrlMatcher {
mock.onGet("/foo");
mock.onGet('/foo');
}

namespace AllowsUrlRegExpMatcher {
mock.onGet(/\/fo+/);
}

namespace AllowsStringBodyMatcher {
mock.onPatch("/foo", "bar");
mock.onPatch('/foo', 'bar');
}

namespace AllowsBodyMatcher {
mock.onGet("/foo", {
mock.onGet('/foo', {
id: 4,
name: "foo",
name: 'foo'
});
}

namespace AllowsParameterMatcher {
mock.onGet("/foo", {
mock.onGet('/foo', {
params: {
searchText: "John",
},
searchText: 'John'
}
});
}

Expand All @@ -87,27 +88,29 @@ namespace SupportsTimeout {
}

namespace AllowsFunctionReply {
mock.onGet().reply((config) => {
return [
200,
{ data: "foo" },
{ RequestedURL: config.url },
];
mock.onGet().reply(config => {
return [200, { data: 'foo' }, { RequestedURL: config.url }];
});
}

namespace AllowsPromiseReply {
mock.onGet().reply((config) => {
mock.onGet().reply(config => {
return Promise.resolve([
200,
{ data: "bar" },
{ RequestedURL: config.url },
{ data: 'bar' },
{ RequestedURL: config.url }
]);
});
}

namespace SupportsChanining {
mock
.onGet("/users").reply(200, [/* users */])
.onGet("/posts").reply(200, [/* posts */]);
.onGet('/users')
.reply(200, [
/* users */
])
.onGet('/posts')
.reply(200, [
/* posts */
]);
}

0 comments on commit 027a821

Please # to comment.