Skip to content

Commit

Permalink
fix: send data as body for the request
Browse files Browse the repository at this point in the history
  • Loading branch information
jepser committed Aug 17, 2018
1 parent 43824d5 commit c6b9fac
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 30 deletions.
2 changes: 2 additions & 0 deletions src/create-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const clientConstructor = ({ token, ...options }) => {
request: (args) => {
const {
url,
data,
headers: argsHeaders = {},
...otherArgs
} = args
Expand All @@ -22,6 +23,7 @@ export const clientConstructor = ({ token, ...options }) => {

return fetch(`${API_BASE_URL}${url}`, {
...requestParameters,
body: JSON.stringify(data),
headers: {
...headers,
...argsHeaders,
Expand Down
4 changes: 2 additions & 2 deletions src/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export default http => ({
}
})

const getForms = (http, { page, page_size, search } = {}) => {
const getForms = (http, { page, pageSize, search } = {}) => {
return http.request({
method: 'get',
url: `/forms`,
page,
page_size,
page_size: pageSize,
search
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/typeform.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import webhooks from './webhooks'

export const createClient = (args = {}) => {
if (args.token === undefined) {
throw 'Token is missing'
throw new Error('Token is missing')
}

const http = clientConstructor(args)
Expand Down
4 changes: 2 additions & 2 deletions src/webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ const createOrUpdateWebhook = (
{ uid, tag, url, enable = false }
) => {
if (url === undefined) {
throw `Please provide an url for ${tag}`
throw new Error(`Please provide an url for ${tag}`)
}
if (tag === undefined) {
throw `Please provide a tag name for the webhook`
throw new Error(`Please provide a tag name for the webhook`)
}
return http.request({
method: 'put',
Expand Down
10 changes: 6 additions & 4 deletions tests/unit/forms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,26 @@ test('getForm sets get method', () => {

test('updateForm sends the correct UID and data', () => {
formsRequest.update({
uid: 'abc123', data: {
uid: 'abc123',
data: {
title: 'hola'
}
})
const bodyParsed = JSON.parse(fetch.mock.calls[0][1].body)
expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/forms/abc123`)
expect(fetch.mock.calls[0][1].data.title).toBe('hola')
expect(bodyParsed.title).toBe('hola')
})

test('updateForm sets patch method in request by default', () => {
formsRequest.update({
uid: 'abc123', data: {
uid: 'abc123',
data: {
title: 'hola'
}
})
expect(fetch.mock.calls[0][1].method).toBe('patch')
})


test('updateForm sets put method in request when override option is set', () => {
formsRequest.update({
uid: 'abc123',
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/images.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ test('adding an image pass the required values', () => {
expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/images`)
expect(fetch.mock.calls[0][1].method).toBe('post')

const imageData = fetch.mock.calls[0][1].data
expect(imageData).toEqual({
const imageData = fetch.mock.calls[0][1].body
expect(imageData).toEqual(JSON.stringify({
image: 'bGRqZmxzZGpmbHNoZmtoc2RrZmpoc2tqZA==',
media_type: 'image/gif',
file_name: 'newimage.gif'
})
file_name: 'newimage.gif',
media_type: 'image/gif'
}))
})

test('deleting an image sets the correct method and id', () => {
Expand Down
14 changes: 6 additions & 8 deletions tests/unit/teams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,20 @@ test('addMember will set the proper method', () => {

test('if a member is sent as string it will work as expected', () => {
teamsRequest.addMembers({ members: '[email protected]' })
expect(fetch.mock.calls[0][1].data).toEqual([
expect(fetch.mock.calls[0][1].body).toEqual(JSON.stringify([
{
op: 'add',
path: '/members',
value: {
email: '[email protected]'
}
}
])
]))
})

test('it will support array or multiple members at a time', () => {
teamsRequest.addMembers({ members: ['[email protected]', '[email protected]'] })
expect(fetch.mock.calls[0][1].data.length).toEqual(2)
expect(fetch.mock.calls[0][1].data).toEqual([
expect(fetch.mock.calls[0][1].body).toEqual(JSON.stringify([
{
op: 'add',
path: '/members',
Expand All @@ -53,28 +52,27 @@ test('it will support array or multiple members at a time', () => {
email: '[email protected]'
}
}
])
]))
})

test('if no members or incorrect format defined throws', () => {
expect(() => teamsRequest.addMembers({ members: {} })).toThrow()
})

//removeMember
test('removeMember will set the proper method', () => {
teamsRequest.removeMembers({ members: ['[email protected]'] })
expect(fetch.mock.calls[0][1].method).toBe('delete')
})

test('if a member is sent as string it will work as expected', () => {
teamsRequest.removeMembers({ members: '[email protected]' })
expect(fetch.mock.calls[0][1].data).toEqual([
expect(fetch.mock.calls[0][1].body).toEqual(JSON.stringify([
{
op: 'remove',
path: '/members',
value: {
email: '[email protected]'
}
}
])
]))
})
8 changes: 6 additions & 2 deletions tests/unit/webhooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ test('Create a new webhooks has the correct path, method and url', () => {
url: 'http://test.com',
enable: true
})

const bodyParsed = JSON.parse(fetch.mock.calls[0][1].body)
expect(fetch.mock.calls[0][1].method).toBe('put')
expect(fetch.mock.calls[0][0]).toBe(`${API_BASE_URL}/forms/2/webhooks/test`)
expect(fetch.mock.calls[0][1].data.url).toBe('http://test.com')
expect(bodyParsed.url).toBe('http://test.com')
})

test('Create a new webhooks requires a url', () => {
Expand All @@ -41,7 +43,9 @@ test('update a new webhooks sends the correct payload', () => {
tag: 'test',
url: 'http://example.com'
})
expect(fetch.mock.calls[0][1].data.url).toBe('http://example.com')

const bodyParsed = JSON.parse(fetch.mock.calls[0][1].body)
expect(bodyParsed.url).toBe('http://example.com')
expect(fetch.mock.calls[0][1].method).toBe('put')
})

Expand Down
16 changes: 10 additions & 6 deletions tests/unit/workspaces.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ test(`add a member to a workscape has the correct payload`, () => {
id: 2,
members: ['[email protected]', '[email protected]']
})
expect(fetch.mock.calls[0][1].data).toEqual([
const body = fetch.mock.calls[0][1].body
expect(body).toEqual(JSON.stringify([
{
op: 'add',
path: '/members',
Expand All @@ -63,25 +64,28 @@ test(`add a member to a workscape has the correct payload`, () => {
email: '[email protected]'
}
}
])
expect(fetch.mock.calls[0][1].data.length).toEqual(2)
]))
expect(JSON.parse(body).length).toEqual(2)
})

test(`remove a member to a workscape has the correct payload`, () => {
workspacesRequest.removeMembers({
id: 2,
members: ['[email protected]']
})
expect(fetch.mock.calls[0][1].data).toEqual([

const body = fetch.mock.calls[0][1].body

expect(body).toEqual(JSON.stringify([
{
op: 'remove',
path: '/members',
value: {
email: '[email protected]'
}
}
])
expect(fetch.mock.calls[0][1].data.length).toEqual(1)
]))
expect(JSON.parse(body).length).toEqual(1)
})

test(`Deleting a workscape has the correct path and method`, () => {
Expand Down

0 comments on commit c6b9fac

Please sign in to comment.