Skip to content

Commit

Permalink
add parseOptions function and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nairihar committed May 16, 2018
1 parent 2231537 commit 7b89bf5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
20 changes: 6 additions & 14 deletions src/http/helpers/fetch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import { methods, } from '../configs'
import { methods, } from '../configs'

export const queryParams = (params = {}) => {
const query = Object.keys(params)
Expand All @@ -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,
Expand All @@ -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
}
*/
1 change: 0 additions & 1 deletion src/http/route/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,5 @@ export default class Route {

fetch() {
this[clearParams]()
// TODO :: do request
}
}
57 changes: 56 additions & 1 deletion test/fetch_helper.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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)
})
})
})

0 comments on commit 7b89bf5

Please sign in to comment.