Skip to content

Commit

Permalink
test: interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
kokororin committed Aug 13, 2017
1 parent dae4e11 commit 52b6a42
Showing 1 changed file with 72 additions and 17 deletions.
89 changes: 72 additions & 17 deletions test/honokaTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import honoka from '../src/honoka';
import { buildURL } from '../src/utils';
import pkg from '../package.json';

const mockJsonResponse = (url = '*') => {
const mockJsonResponse = (url = '*', method = 'get') => {
fetchMock.mock(url, {
body: { hello: 'world' },
method,
headers: {
'Content-Type': 'application/json'
}
Expand All @@ -17,12 +18,21 @@ describe('honoka', () => {
fetchMock.restore();
});

it('buildURL() can build query strings', () => {
let url = buildURL('http://www.google.com/s', { q: 'honoka' });
it('honoka.buildURL() can build query strings', () => {
const url = buildURL('http://www.google.com/s', { q: 'honoka' });
expect(url).to.equal('http://www.google.com/s?q=honoka');
url = buildURL('http://www.google.com/s', { q: 'honoka', ie: 'UTF-8' });
});

it('honoka.buildURL() can build query strings with two parameters', () => {
const url = buildURL('http://www.google.com/s', {
q: 'honoka',
ie: 'UTF-8'
});
expect(url).to.equal('http://www.google.com/s?q=honoka&ie=UTF-8');
url = buildURL('http://www.google.com/s?q=honoka', { ie: 'UTF-8' });
});

it('honoka.buildURL() can build query strings with a query-given url', () => {
const url = buildURL('http://www.google.com/s?q=honoka', { ie: 'UTF-8' });
expect(url).to.equal('http://www.google.com/s?q=honoka&ie=UTF-8');
});

Expand All @@ -32,32 +42,46 @@ describe('honoka', () => {

it('honoka() should return a body when status is 200', async () => {
mockJsonResponse();
const response = await honoka('http://www.google.com');
expect(response).to.deep.equal({ hello: 'world' });
});

it('honoka.get() should return a body when status is 200', async () => {
mockJsonResponse();
const response = await honoka('http://www.google.com');
expect(response).to.deep.equal({ hello: 'world' });
const data = await honoka('http://www.google.com');
expect(data).to.deep.equal({ hello: 'world' });
});

it('honoka() should build query strings correctly', async () => {
mockJsonResponse('http://www.google.com/s?q=honoka&ie=UTF-8');
const response = await honoka('http://www.google.com/s', {
const data = await honoka('http://www.google.com/s', {
data: {
q: 'honoka',
ie: 'UTF-8'
}
});
expect(response).to.deep.equal({ hello: 'world' });
expect(data).to.deep.equal({ hello: 'world' });
});

it('honoka() should get the body object when fetching a json', async () => {
mockJsonResponse();
const data = await honoka('http://www.google.com');
expect(data.hello).to.equal('world');
});

it('honoka.response should return the fetch response', async () => {
mockJsonResponse();
await honoka('http://www.google.com');
expect(honoka.response.headers.get('Content-Type')).to.equal(
'application/json'
);
});

it('honoka.get() should return a body when status is 200', async () => {
mockJsonResponse();
const data = await honoka('http://www.google.com');
expect(data).to.deep.equal({ hello: 'world' });
});

it('honoka() should throw Error when status >= 400', async () => {
fetchMock.mock('*', { status: 400 });
let err;
try {
const response = await honoka('http://www.google.com');
await honoka('http://www.google.com');
} catch (e) {
err = e;
}
Expand All @@ -71,7 +95,7 @@ describe('honoka', () => {
fetchMock.mock('*', delay);
let err;
try {
const response = await honoka('http://www.google.com', {
await honoka('http://www.google.com', {
timeout: 500
});
} catch (e) {
Expand All @@ -81,4 +105,35 @@ describe('honoka', () => {
throw err;
}).to.throw(/Request timeout/);
});

it('honoka.interceptors.regsiter() should register a request interceptor', async () => {
honoka.interceptors.register({
request: options => {
options.method = 'post';
return options;
}
});
mockJsonResponse('*', 'post');
const data = await honoka('http://www.google.com');
expect(data).to.deep.equal({ hello: 'world' });
});

it('honoka.interceptors.regsiter() should register a response interceptor', async () => {
honoka.interceptors.register({
response: (data, response) => {
response.test = 'test';
return response;
}
});
mockJsonResponse();
await honoka('http://www.google.com');
expect(honoka.response.test).to.equal('test');
});

it('honoka.defaults should work', async () => {
honoka.defaults.baseURL = 'http://www.google.com';
mockJsonResponse('http://www.google.com/s');
const data = await honoka('/s');
expect(data).to.deep.equal({ hello: 'world' });
});
});

0 comments on commit 52b6a42

Please sign in to comment.