Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiothiroki committed May 1, 2021
1 parent f2a511e commit 0a40f23
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
12 changes: 12 additions & 0 deletions flow-typed/flight.js.flow
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @flow

declare type Flight = {
id: string,
flyFrom: string,
cityFrom: string,
cityTo: string,
flyTo: string,
price: string,
departureDate: string,
arrivalDate: string,
};
8 changes: 3 additions & 5 deletions src/components/AirportSelector/AirportSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import formatAirportResults from "../../services/Airport/airportResponseFormatte

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

const AirportSelector = ({ inputLabel, onChange, name }) => {
const AirportSelector = ({ inputLabel, onChange, nameIdentifier }) => {
const [inputValue, setInputValue] = useState("");
const [selectedAirport, setSelectedAirport] = useState(null);

Expand All @@ -29,9 +29,7 @@ const AirportSelector = ({ inputLabel, onChange, name }) => {
}}
onChange={(_e, airport) => {
setSelectedAirport(airport);
if (onChange) {
onChange(name, airport);
}
onChange(nameIdentifier, airport);
}}
value={selectedAirport}
/>
Expand All @@ -41,7 +39,7 @@ const AirportSelector = ({ inputLabel, onChange, name }) => {
AirportSelector.propTypes = {
inputLabel: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
name: PropTypes.string.isRequired,
nameIdentifier: PropTypes.string.isRequired,
};

export default AirportSelector;
13 changes: 12 additions & 1 deletion src/components/AirportSelector/tests/AirPortSelector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ const server = setupServer(
)
);

const props = {
inputLabel: "For",
nameIdentifier: "airport",
onChange: jest.fn(),
};

const renderComponent = () => {
const queryClient = new QueryClient();
const wrapper = (
<QueryClientProvider client={queryClient}>
<AirportSelector inputLabel="For" name="airport" onChange={jest.fn()} />
<AirportSelector {...props} />
</QueryClientProvider>
);

Expand Down Expand Up @@ -62,6 +68,11 @@ test("select autocomplete suggestion", async () => {
userEvent.click(suggestion);

expect(input).toHaveValue("Václav Havel Airport Prague");
expect(props.onChange).toBeCalledWith("airport", {
code: "PRG",
id: "PRG",
name: "Václav Havel Airport Prague",
});
});

test("clear autocomplete suggestion", async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/Search/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const Search = ({ onSearchSubmitted }) => {
rules={{ required: true }}
render={() => (
<AirportSelector
name={FORM_FIELDS.ORIGIN}
nameIdentifier={FORM_FIELDS.ORIGIN}
inputLabel="From"
onChange={setValue}
/>
Expand All @@ -65,7 +65,7 @@ const Search = ({ onSearchSubmitted }) => {
rules={{ required: true }}
render={() => (
<AirportSelector
name={FORM_FIELDS.DESTINATION}
nameIdentifier={FORM_FIELDS.DESTINATION}
inputLabel="To"
onChange={setValue}
/>
Expand Down
11 changes: 1 addition & 10 deletions src/services/Flight/flightResponseFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,7 @@ const flightResponseFormatter = (flights: {
dTimeUTC: number,
aTimeUTC: number,
}>,
}): ?Array<{
id: string,
flyFrom: string,
cityFrom: string,
cityTo: string,
flyTo: string,
price: string,
departureDate: string,
arrivalDate: string,
}> => {
}): ?Array<Flight> => {
if (!flights) {
return null;
}
Expand Down

0 comments on commit 0a40f23

Please sign in to comment.