How to do it...

Let's write some stories. We can start with the <RegionsTable> component, which is quite simple: it doesn't include any actions, and just displays data. We can write two cases: when an empty list of regions is provided, and when a non-empty one is given. We don't have to think too much about the needed fake data, because we can reuse what we wrote for our unit tests! Consider the following code:

// Source file: src/regionsStyledApp/regionsTable.story.js

/* @flow */

import React from "react";
import { storiesOf } from "@storybook/react-native";

import { Centered } from "../../storybook/centered";
import { RegionsTable } from "./regionsTable.component";

const fakeDeviceData = {
isTablet: false,
isPortrait: true,
height: 1000,
width: 720,
scale: 1,
fontScale: 1
};

storiesOf("RegionsTable", module)
.addDecorator(getStory => <Centered>{getStory()}</Centered>)
.add("with no regions", () => (
<RegionsTable deviceData={fakeDeviceData} list={[]} />
))
.add("with some regions", () => (
<RegionsTable
deviceData={fakeDeviceData}
list={[
{
countryCode: "UY",
regionCode: "10",
regionName: "Montevideo"
},
{
countryCode: "UY",
regionCode: "9",
regionName: "Maldonado"
},
{
countryCode: "UY",
regionCode: "5",
regionName: "Cerro Largo"
}
]}
/>
));

Adding a decorator to center the displayed component is just for clarity: the necessary <Centered> code is simple, and needs a little of the styling we saw in the previous chapter:

// Source file: storybook/centered.js

/* @flow */

import React from "react";
import { View, StyleSheet } from "react-native";
import PropTypes from "prop-types";

const centerColor = "white";
const styles = StyleSheet.create({
centered: {
flex: 1,
backgroundColor: centerColor,
alignItems: "center",
justifyContent: "center"
}
});

export class Centered extends React.Component<{ children: node }> {
static propTypes = {
children: PropTypes.node.isRequired
};

render() {
return <View style={styles.centered}>{this.props.children}</View>;
}
}

Now, setting up stories for <CountrySelect> is more interesting, because we have actions. We'll provide two to the component: one when the user clicks on it to select a country, and an other for the getCountries() callback that the component will use to get the list of countries:

// Source file: src/regionsStyledApp/countrySelect.story.js

/* @flow */

import React from "react";
import { storiesOf } from "@storybook/react-native";
import { action } from "@storybook/addon-actions";

import { Centered } from "../../storybook/centered";
import { CountrySelect } from "./countrySelect.component";

const fakeDeviceData = {
isTablet: false,
isPortrait: true,
height: 1000,
width: 720,
scale: 1,
fontScale: 1
};

storiesOf("CountrySelect", module)
.addDecorator(getStory => <Centered>{getStory()}</Centered>)
.add("with no countries yet", () => (
<CountrySelect
deviceData={fakeDeviceData}
loading={true}
currentCountry={""}
onSelect={action("click:country")}
getCountries={action("call:getCountries")}
list={[]}
/>
))
.add("with three countries", () => (
<CountrySelect
deviceData={fakeDeviceData}
currentCountry={""}
loading={false}
onSelect={action("click:country")}
getCountries={action("call:getCountries")}
list={[
{
countryCode: "UY",
countryName: "Uruguay"
},
{
countryCode: "AR",
countryName: "Argentina"
},
{
countryCode: "BR",
countryName: "Brazil"
}
]}
/>
));

We are all set now; let's see how this works.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset