Skip to content
This repository has been archived by the owner on Dec 7, 2020. It is now read-only.

Commit

Permalink
feat: POST object rather than query params
Browse files Browse the repository at this point in the history
  • Loading branch information
yeahfro committed Aug 28, 2019
1 parent 1f244de commit cd4c144
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 25 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"moment": "^2.24.0",
"pluralise": "^1.0.1",
"prop-types": "^15.7.2",
"query-string": "^6.8.2",
"react-lines-ellipsis": "^0.14.1",
"react-markdown": "^4.0.8",
"styled-components": "^4.2.0"
Expand Down
33 changes: 26 additions & 7 deletions src/entity-search/EntitySearch.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,35 @@ import EntitySearchWithDataProvider from './EntitySearchWithDataProvider'

const mock = new MockAdapter(axios)
const apiEndpoint = 'http://localhost:3010/v4/dnb/company-search'
const apiEndpointWithParameters = new RegExp(`${apiEndpoint}.+`)

const setupSuccessMocks = (response = fixtures.companySearch) => {
mock.onPost(apiEndpoint).reply(200, response)
mock.onPost(apiEndpointWithParameters).reply(200, response)
const setupSuccessMocks = () => {
mock
.onPost(apiEndpoint, {})
.reply(200, fixtures.companySearch)
mock
.onPost(apiEndpoint, { search_term: 'some other company' })
.reply(200, fixtures.companySearchFilteredByCompanyName)
mock
.onPost(apiEndpoint, { address_postcode: 'BN1 4SE' })
.reply(200, fixtures.companySearchFilteredByPostcode)
}

const setupErrorMocks = () => {
mock.onPost(apiEndpoint).reply(500)
mock.onPost(apiEndpointWithParameters).reply(500)
mock.onPost(apiEndpoint, {}).reply(500)
mock.onPost(apiEndpoint, { search_term: 'some other company' }).reply(500)
mock.onPost(apiEndpoint, { address_postcode: 'BN1 4SE' }).reply(500)
}

const setupNoResultsMocks = () => {
mock
.onPost(apiEndpoint, {})
.reply(200, fixtures.companySearchNoResults)
mock
.onPost(apiEndpoint, { search_term: 'some other company' })
.reply(200, fixtures.companySearchNoResults)
mock
.onPost(apiEndpoint, { address_postcode: 'BN1 4SE' })
.reply(200, fixtures.companySearchNoResults)
}

const EntitySearchForStorybook = ({ previouslySelected, cannotFindLink }) => {
Expand Down Expand Up @@ -130,7 +149,7 @@ storiesOf('EntitySearch', module)
)
})
.add('Data Hub company search with no results', () => {
setupSuccessMocks(fixtures.companySearchNoResults)
setupNoResultsMocks()

return (
<EntitySearchForStorybook />
Expand Down
10 changes: 5 additions & 5 deletions src/entity-search/EntitySearch.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import EntitySearchWithDataProvider from './EntitySearchWithDataProvider'
const mock = new MockAdapter(axios)
const API_ENDPOINT = 'http://localhost:8000/v4/dnb/company-search'
mock
.onPost(API_ENDPOINT)
.onPost(API_ENDPOINT, {})
.reply(200, fixtures.companySearch)
mock
.onPost(`${API_ENDPOINT}?search_term=some%20other%20company`)
.onPost(API_ENDPOINT, { search_term: 'some other company' })
.reply(200, fixtures.companySearchFilteredByCompanyName)
mock
.onPost(`${API_ENDPOINT}?address_postcode=BN1%204SE`)
.onPost(API_ENDPOINT, { address_postcode: 'BN1 4SE' })
.reply(200, fixtures.companySearchFilteredByPostcode)

const flushPromises = () => {
Expand Down Expand Up @@ -263,7 +263,7 @@ describe('EntitySearch', () => {

describe('when the API returns a server error', () => {
beforeAll(() => {
mock.onPost(API_ENDPOINT).reply(500)
mock.onPost(API_ENDPOINT, {}).reply(500)
})

test('should render the component with an error message', async () => {
Expand All @@ -281,7 +281,7 @@ describe('EntitySearch', () => {

describe('when the API returns 0 results', () => {
beforeAll(() => {
mock.onPost(API_ENDPOINT).reply(200, fixtures.companySearchNoResults)
mock.onPost(API_ENDPOINT, {}).reply(200, fixtures.companySearchNoResults)
})

test('should render the component with a "no entities" message', async () => {
Expand Down
4 changes: 1 addition & 3 deletions src/entity-search/data-providers/DnbCompanySearch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
import React from 'react'
import axios from 'axios'
import { compact, join } from 'lodash'
import queryString from 'query-string'

export default (apiEndpoint) => {
return async (filters) => {
const transformed = queryString.stringify(filters)
const { data } = await axios.post(`${apiEndpoint + (transformed ? `?${transformed}` : '')}`)
const { data } = await axios.post(apiEndpoint, filters)

return data.results.map((result) => {
const { dnb_company, datahub_company } = result
Expand Down
9 changes: 0 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10574,15 +10574,6 @@ query-string@^6.2.0:
split-on-first "^1.0.0"
strict-uri-encode "^2.0.0"

query-string@^6.8.2:
version "6.8.2"
resolved "https://registry.yarnpkg.com/query-string/-/query-string-6.8.2.tgz#36cb7e452ae11a4b5e9efee83375e0954407b2f6"
integrity sha512-J3Qi8XZJXh93t2FiKyd/7Ec6GNifsjKXUsVFkSBj/kjLsDylWhnCz4NT1bkPcKotttPW+QbKGqqPH8OoI2pdqw==
dependencies:
decode-uri-component "^0.2.0"
split-on-first "^1.0.0"
strict-uri-encode "^2.0.0"

querystring-es3@^0.2.0:
version "0.2.1"
resolved "https://registry.yarnpkg.com/querystring-es3/-/querystring-es3-0.2.1.tgz#9ec61f79049875707d69414596fd907a4d711e73"
Expand Down

0 comments on commit cd4c144

Please sign in to comment.