Skip to content
Draft
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
12 changes: 8 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,20 @@ const fetchData = async (link) => {
}
};

const fetchPostCode = async(postcode) => {
const fetchPostCode = async (postcode) => {
try {
const response = await axios.get('https://api.postcodes.io/postcodes/' + postcode);
const encoded = encodeURIComponent(postcode);
const response = await axios.get(
'https://api.postcodes.io/postcodes/' + encoded
);
if (response.data.status === 200) {
return [response.data.result.longitude, response.data.result.latitude];
}
} catch (e) {
console.log(e);
return 'failed!'
return 'failed!';
}
}
};

function ready(fn) {
if (document.readyState !== 'loading') {
Expand Down Expand Up @@ -343,3 +346,4 @@ function App () {
}

export default App;
export { fetchPostCode };
17 changes: 16 additions & 1 deletion src/App.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { render, screen, waitFor, waitForElementToBeRemoved } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import App from './App';
import App, { fetchPostCode } from './App';
import axios from 'axios';

jest.mock('axios');
Expand Down Expand Up @@ -134,3 +134,18 @@ test("Status filter can exclude some values", async () => {
expect(referenceElement1.parentElement.style.display).toEqual("none");
// expect(referenceElement2.parentElement.style.display).not.toEqual("none");
});

test('fetchPostCode encodes the postcode in the request URL', async () => {
axios.get.mockResolvedValue({
data: {
status: 200,
result: { longitude: 1, latitude: 2 },
},
});

await fetchPostCode('EC1A 1BB');

expect(axios.get).toHaveBeenCalledWith(
'https://api.postcodes.io/postcodes/' + encodeURIComponent('EC1A 1BB')
);
});