Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
Hotels: Fix relay/unused-fields in HotelDetailScreen
Browse files Browse the repository at this point in the history
Refactor countBookingPrice function into HotelDetailScreen since it is
not used elsewhere anymore
  • Loading branch information
tbergquist-godaddy committed Jan 15, 2019
1 parent e36beb4 commit fecf7bb
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 136 deletions.
47 changes: 40 additions & 7 deletions app/hotels/src/singleHotel/HotelDetailScreen.js
@@ -1,7 +1,5 @@
// @flow

/* eslint-disable relay/unused-fields */
// TODO: Fix ^^
import * as React from 'react';
import { ScrollView, View, Platform } from 'react-native';
import {
Expand Down Expand Up @@ -32,7 +30,11 @@ import {
type HotelsContextState,
type ApiProvider,
} from '../HotelsContext';
import countBookingPrice from './bookNow/countBookingPrice';

type Price = {|
+amount: number,
+currency: string,
|};

type PropsWithContext = {|
...Props,
Expand Down Expand Up @@ -86,10 +88,7 @@ export class HotelDetailScreen extends React.Component<

componentDidUpdate(prevProps: PropsWithContext) {
if (!isEqual(prevProps.selected, this.props.selected)) {
const price = countBookingPrice(
this.props.availableHotel?.availableRooms,
this.props.selected,
);
const price = this.countBookingPrice();
if (price != null) {
this.props.setPrice(price);
}
Expand All @@ -100,6 +99,40 @@ export class HotelDetailScreen extends React.Component<
this.props.reset();
}

countBookingPrice = (): ?Price => {
const availableRooms = this.props.availableHotel?.availableRooms;
const selected = this.props.selected;

if (!availableRooms) {
return null;
}
const positiveSelections = Object.entries(selected).filter(
(selection: any) => selection[1] > 0,
);

if (positiveSelections.length === 0) {
return null;
}

const amount = positiveSelections
.map((selection: any) => {
const [id, count] = selection;

const room = availableRooms.find(room => room?.id === id);
return room?.incrementalPrice?.[count - 1]?.amount;
})
.reduce((a, b) => a + b, 0);

const currency =
availableRooms.find(room => room?.id === positiveSelections[0][0])
?.incrementalPrice?.[0]?.currency ?? '';

return {
amount,
currency,
};
};

isNarrowLayout = () => {
return this.props.width < Device.DEVICE_THRESHOLD;
};
Expand Down
12 changes: 6 additions & 6 deletions app/hotels/src/singleHotel/HotelDetailScreenContext.js
Expand Up @@ -31,24 +31,24 @@ type Props = {|
+guestCount: number,
|};

type Money = {|
amount: number,
currency: string,
type Price = {|
+amount: number,
+currency: string,
|};

type State = {|
selected: {
[string]: number, // originalId: count
},
price: Money,
price: Price,
maxPersons: number,
numberOfRooms: number,
+getPersonCount: () => number,
+actions: {|
+selectRoom: (id: string, maxPersons: number) => void,
+deselectRoom: (id: string, maxPersons: number) => void,
+reset: () => void,
+setPrice: (price: Money) => void,
+setPrice: (price: Price) => void,
|},
|};

Expand Down Expand Up @@ -107,7 +107,7 @@ export class Provider extends React.Component<Props, State> {
: this.state.maxPersons;
};

setPrice = (price: Money) => {
setPrice = (price: Price) => {
this.setState({ price });
};

Expand Down
102 changes: 102 additions & 0 deletions app/hotels/src/singleHotel/__tests__/HotelDetailScreen.test.js
@@ -0,0 +1,102 @@
// @flow

import * as React from 'react';
import renderer from 'react-test-renderer';

import { HotelDetailScreen } from '../HotelDetailScreen';

const $fragmentRefs: any = null;
const availableRooms = [
{
$fragmentRefs,
id: 'aaa',
incrementalPrice: [
{
amount: 500,
currency: 'EUR',
},
{
amount: 1000,
currency: 'EUR',
},
],
},
{
$fragmentRefs,
id: 'bbb',
incrementalPrice: [
{
amount: 333,
currency: 'EUR',
},
{
amount: 666,
currency: 'EUR',
},
],
},
];

const getComponent = (availableRooms, selected) => {
// $FlowExpectedError: Passing just props needed to perform test
return new HotelDetailScreen({
availableHotel: {
availableRooms,
},
selected,
});
};

describe('countBookingPrice', () => {
it('returns null when availableRooms are not provided', () => {
const NullComponent = getComponent(null, { aaa: 1 });

const UndefinedComponent = getComponent(null, { aaa: 1 });

expect(NullComponent.countBookingPrice()).toBe(null);
expect(UndefinedComponent.countBookingPrice()).toBe(null);
});

it('returns null when no room is selected', () => {
const ComponentA = getComponent(availableRooms, {});
const ComponentB = getComponent(availableRooms, { aaa: 0, bbb: 0 });

expect(ComponentA.countBookingPrice()).toBe(null);
expect(ComponentB.countBookingPrice()).toBe(null);
});

it('returns sum of selected rooms', () => {
const Component = getComponent(availableRooms, { aaa: 2, bbb: 1 });

expect(Component.countBookingPrice()).toEqual({
amount: 1333,
currency: 'EUR',
});
});

it('takes a currency from the first selected room', () => {
const czechRooms = [
{
$fragmentRefs,
id: 'aaa',
incrementalPrice: [],
},
{
$fragmentRefs,
id: 'bbb',
incrementalPrice: [
{
amount: 123,
currency: 'CZK',
},
],
},
];
const Component = getComponent(czechRooms, { bbb: 1 });

expect(Component.countBookingPrice()).toEqual({
amount: 123,
currency: 'CZK',
});
});
});

This file was deleted.

47 changes: 0 additions & 47 deletions app/hotels/src/singleHotel/bookNow/countBookingPrice.js

This file was deleted.

0 comments on commit fecf7bb

Please sign in to comment.