diff --git a/test/honokaTest.js b/test/honokaTest.js index 79f2db0..84d5a5f 100644 --- a/test/honokaTest.js +++ b/test/honokaTest.js @@ -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' } @@ -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'); }); @@ -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; } @@ -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) { @@ -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' }); + }); });