From 7b89bf5e3d62e097cc5f08cee4d3530d08e786bb Mon Sep 17 00:00:00 2001 From: Nairi Date: Wed, 16 May 2018 21:27:15 +0400 Subject: [PATCH] add parseOptions function and tests --- src/http/helpers/fetch.js | 20 +++++--------- src/http/route/index.js | 1 - test/fetch_helper.js | 57 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 16 deletions(-) diff --git a/src/http/helpers/fetch.js b/src/http/helpers/fetch.js index 16d6998..bb4f237 100644 --- a/src/http/helpers/fetch.js +++ b/src/http/helpers/fetch.js @@ -1,4 +1,4 @@ -// import { methods, } from '../configs' +import { methods, } from '../configs' export const queryParams = (params = {}) => { const query = Object.keys(params) @@ -7,19 +7,18 @@ export const queryParams = (params = {}) => { return query } -/* -export const parseOptions = ({ method, headers, url, data, queryData, }) => { - let parsedUrl = url +export const parseOptions = ({ method, headers, url, data, }) => { + let myUrl = url let query, body if (method === methods.GET) { - query = queryParams(queryData) - parsedUrl += `?${query}` + query = queryParams(data) + myUrl += `?${query}` } else { body = JSON.stringify(data) } const options = { - parsedUrl, + url: myUrl, requestOptions: { method, body, @@ -28,10 +27,3 @@ export const parseOptions = ({ method, headers, url, data, queryData, }) => { } return options } - -export const parseResponse = async (response) => { - // TODO :: add error message handling and status codes - const data = await response.json() - return data -} -*/ diff --git a/src/http/route/index.js b/src/http/route/index.js index 781f00d..efa54a9 100644 --- a/src/http/route/index.js +++ b/src/http/route/index.js @@ -226,6 +226,5 @@ export default class Route { fetch() { this[clearParams]() - // TODO :: do request } } diff --git a/test/fetch_helper.js b/test/fetch_helper.js index 0d4cf19..92cd2ef 100644 --- a/test/fetch_helper.js +++ b/test/fetch_helper.js @@ -1,7 +1,8 @@ import 'babel-polyfill' import assert from 'assert' -import { queryParams, } from '../src/http/helpers' +import { queryParams, parseOptions, } from '../src/http/helpers' +import { methods, } from '../src/http/configs' describe('Fetch helper methods', () => { describe('Check queryParams', () => { @@ -15,4 +16,58 @@ describe('Fetch helper methods', () => { assert.equal(queryParams({}), '') }) }) + + describe('Check parseOptions', () => { + it('should return parsed options object for Get method', () => { + const method = methods.GET + const headers = { + headedasdr: 1, + } + const options = { + method, + headers, + url: 'http://localhost:4000', + data: { + d: 1, + a: 2, + }, + } + const expectedRes = { + url: 'http://localhost:4000?d=1&a=2', + requestOptions: { + method, + headers, + body: undefined, + }, + } + const res = parseOptions(options) + assert.deepEqual(res, expectedRes) + }) + it('should return parsed options object for PUT method', () => { + const method = methods.PUT + const headers = { + headedasdr: 444, + } + const data = { + d: 1090, + a: 2, + } + const options = { + method, + headers, + url: 'http://localhost:4000', + data, + } + const expectedRes = { + url: 'http://localhost:4000', + requestOptions: { + method, + headers, + body: JSON.stringify(data), + }, + } + const res = parseOptions(options) + assert.deepEqual(res, expectedRes) + }) + }) })