Skip to content

Commit 41b6343

Browse files
Add tile request transformer client tests
1 parent 0d50547 commit 41b6343

2 files changed

Lines changed: 58 additions & 2 deletions

File tree

src/client/javascripts/location-map.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function initMaps(config = {}) {
7575
* OS API request proxy factory
7676
* @param {string} apiPath - the root API path
7777
*/
78-
function makeTileRequestTransformer(apiPath) {
78+
export function makeTileRequestTransformer(apiPath) {
7979
/**
8080
* Proxy OS API requests via our server
8181
* @param {string} url - the request URL

test/client/javascripts/location-map.test.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
formSubmitFactory,
3-
initMaps
3+
initMaps,
4+
makeTileRequestTransformer
45
} from '~/src/client/javascripts/location-map.js'
56

67
describe('Location Maps Client JS', () => {
@@ -179,4 +180,59 @@ describe('Location Maps Client JS', () => {
179180

180181
expect(preventDefault).toHaveBeenCalledTimes(1)
181182
})
183+
184+
describe('Tile request transformer', () => {
185+
const apiPath = '/api'
186+
187+
test('tile request transformer factory returns a transformer function', () => {
188+
const transformer = makeTileRequestTransformer(apiPath)
189+
190+
expect(typeof transformer).toBe('function')
191+
expect(transformer).toHaveLength(2)
192+
})
193+
194+
test('tile request transformer works on api.os.uk requests without an apikey', () => {
195+
const url = 'https://api.os.uk/test.js'
196+
const transformer = makeTileRequestTransformer(apiPath)
197+
const result = transformer(url, 'Script')
198+
199+
expect(result).toEqual({
200+
url: `${apiPath}/map-proxy?url=${encodeURIComponent(url)}`,
201+
headers: {}
202+
})
203+
})
204+
205+
test('tile request transformer does not apply to api.os.uk requests that already have an api key', () => {
206+
const url = 'https://api.os.uk/test.js?key=abcde'
207+
const transformer = makeTileRequestTransformer(apiPath)
208+
const result = transformer(url, 'Script')
209+
210+
expect(result).toEqual({
211+
url,
212+
headers: {}
213+
})
214+
})
215+
216+
test('tile request transformer does not apply to "Style" api.os.uk requests', () => {
217+
const url = 'https://api.os.uk/test.js'
218+
const transformer = makeTileRequestTransformer(apiPath)
219+
const result = transformer(url, 'Style')
220+
221+
expect(result).toEqual({
222+
url,
223+
headers: {}
224+
})
225+
})
226+
227+
test('tile request transformer does not apply to other domain requests', () => {
228+
const url = 'https://esri.os.uk/test.js'
229+
const transformer = makeTileRequestTransformer(apiPath)
230+
const result = transformer(url, 'Script')
231+
232+
expect(result).toEqual({
233+
url,
234+
headers: {}
235+
})
236+
})
237+
})
182238
})

0 commit comments

Comments
 (0)