Skip to content

Commit

Permalink
Merge pull request #33 from fabiothiroki/app-tests
Browse files Browse the repository at this point in the history
add more flow
  • Loading branch information
fabiothiroki committed May 1, 2021
2 parents cf5010e + 46fa6e9 commit f2a511e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 39 deletions.
7 changes: 7 additions & 0 deletions flow-typed/airport.js.flow
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @flow

declare type Airport = {
id: string,
code: string,
name: string,
};
15 changes: 1 addition & 14 deletions src/components/AirportSelector/AirportSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,10 @@ import throttle from "lodash.throttle";
import { useQuery } from "react-query";
import PropTypes from "prop-types";
import getAirportsByTerm from "../../services/Airport/airportService";
import formatAirportResults from "../../services/Airport/airportResponseFormatter";

const throttled = throttle((searchTerm) => getAirportsByTerm(searchTerm), 200);

const formatAirportResults = (results, selectedAirport) => {
const formattedData = [];

if (selectedAirport) {
formattedData.push(selectedAirport);
}

if (results && results.locations) {
return formattedData.concat(results.locations);
}

return formattedData;
};

const AirportSelector = ({ inputLabel, onChange, name }) => {
const [inputValue, setInputValue] = useState("");
const [selectedAirport, setSelectedAirport] = useState(null);
Expand Down
5 changes: 5 additions & 0 deletions src/components/App/tests/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import App from "../App";

test("Render initial state", () => {
expect(App).toBeDefined();
});
12 changes: 0 additions & 12 deletions src/components/Search/tests/Search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,14 @@ test("Form submission", async () => {
expect(props.onSearchSubmitted).toHaveBeenCalledWith({
departureDate: "2020-05-13",
destination: {
active: true,
airport_int_id: 5212,
code: "PRG",
icao: "LKPR",
id: "PRG",
int_id: 5212,
name: "Václav Havel Airport Prague",
slug: "vaclav-havel-airport-prague-prague-czechia",
slug_en: "vaclav-havel-airport-prague-prague-czechia",
},
origin: {
active: true,
airport_int_id: 5212,
code: "PRG",
icao: "LKPR",
id: "PRG",
int_id: 5212,
name: "Václav Havel Airport Prague",
slug: "vaclav-havel-airport-prague-prague-czechia",
slug_en: "vaclav-havel-airport-prague-prague-czechia",
},
returnDate: "2020-05-15",
})
Expand Down
20 changes: 20 additions & 0 deletions src/services/Airport/airportResponseFormatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// @flow

const formatAirportResults = (
results?: { locations?: Array<Airport> },
selectedAirport: Airport
): Airport[] => {
const formattedData = [];

if (selectedAirport) {
formattedData.push(selectedAirport);
}

if (results && results.locations) {
return formattedData.concat(results.locations);
}

return formattedData;
};

export default formatAirportResults;
16 changes: 3 additions & 13 deletions src/services/Airport/test/mockAirportReponse.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
const mockAirportResponse = {
// @flow

const mockAirportResponse: { locations: Array<Airport> } = {
locations: [
{
id: "PRG",
int_id: 5212,
airport_int_id: 5212,
active: true,
code: "PRG",
icao: "LKPR",
name: "Václav Havel Airport Prague",
slug: "vaclav-havel-airport-prague-prague-czechia",
slug_en: "vaclav-havel-airport-prague-prague-czechia",
},
{
id: "BCN",
int_id: 431,
airport_int_id: 431,
active: true,
code: "BCN",
icao: "LEBL",
name: "Barcelona–El Prat",
slug: "barcelona-el-prat-barcelona-spain",
slug_en: "barcelona-el-prat-barcelona-spain",
},
],
};
Expand Down

0 comments on commit f2a511e

Please sign in to comment.