Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions plugins/search/src/events/fetchSuggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@

export const sanitiseQuery = (value) => value.replace(/[^a-zA-Z0-9\s\-.,]/g, '').trim()

const toRequest = ({ url, options }) => new Request(url, options)

const getRequestConfig = async (ds, query, transformRequest) => {
const defaultRequest = {
url: ds.urlTemplate?.replace('{query}', encodeURIComponent(query)),
options: { method: 'GET' }
}

if (ds.buildRequest) {
return ds.buildRequest(query, () => defaultRequest)
return toRequest(ds.buildRequest(query, () => defaultRequest))
}

if (transformRequest) {
const transformedRequest = await transformRequest(defaultRequest, query)
return transformedRequest
return toRequest(await transformRequest(defaultRequest, query))
}

return defaultRequest
return toRequest(defaultRequest)
}

/**
Expand Down
9 changes: 5 additions & 4 deletions plugins/search/src/events/fetchSuggestions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('fetchSuggestions', () => {
beforeEach(() => {
dispatch.mockClear()
global.fetch = jest.fn()
global.Request = jest.fn((url, options) => ({ url, ...options }))
jest.spyOn(console, 'error').mockImplementation(() => {})
})

Expand All @@ -35,7 +36,7 @@ describe('fetchSuggestions', () => {

const result = await fetchSuggestions('test', datasets, dispatch)

expect(fetch).toHaveBeenCalledWith({ url: '/api?q=test', options: { method: 'GET' } })
expect(fetch).toHaveBeenCalledWith(expect.objectContaining({ url: '/api?q=test', method: 'GET' }))
expect(result.results).toEqual(['a', 'b'])
expect(dispatch).toHaveBeenCalledWith({
type: 'UPDATE_SUGGESTIONS',
Expand Down Expand Up @@ -77,7 +78,7 @@ describe('fetchSuggestions', () => {

const result = await fetchSuggestions('abc', datasets, dispatch)

expect(fetch).toHaveBeenCalledWith({ url: '/custom/abc', options: { method: 'POST' } })
expect(fetch).toHaveBeenCalledWith(expect.objectContaining({ url: '/custom/abc', method: 'POST' }))
expect(result.results).toEqual(['y'])
})

Expand All @@ -101,7 +102,7 @@ describe('fetchSuggestions', () => {

await fetchSuggestions('x', datasets, dispatch, transformRequest)

expect(fetch).toHaveBeenCalledWith({ url: '/t?q=x', options: { method: 'PUT' } })
expect(fetch).toHaveBeenCalledWith(expect.objectContaining({ url: '/t?q=x', method: 'PUT' }))
})

test('handles fetch HTTP error', async () => {
Expand Down Expand Up @@ -206,7 +207,7 @@ describe('fetchSuggestions', () => {

const result = await fetchSuggestions('hi', datasets, dispatch)

expect(fetch).toHaveBeenCalledWith({ url: '/default?q=hi', options: { method: 'GET' } })
expect(fetch).toHaveBeenCalledWith(expect.objectContaining({ url: '/default?q=hi', method: 'GET' }))
expect(result.results).toEqual(['ok'])
})
})
Loading