官术网_书友最值得收藏!

Adding an entrypoint

React applications are composed of a hierarchy of components that are rendered at the root. Our application entrypoint should render this root component.

I tend to not test-drive my entrypoint, because any test that loads our entire application can become quite brittle as we add in more and more dependencies into it. In Section 4, Acceptance Testing with BDD, we'll look at using acceptance tests to write some tests that will cover the entrypoint.

Since we aren't test-driving it, we follow a couple of general rules:

  • Keep it as brief as possible

  • Only use it to instantiate dependencies for your root component, and to call ReactDOM.render.

Before we run our app, we'll need some sample data. Create a file named src/sampleData.js and fill it with the following:

const today = new Date();

const at = hours => today.setHours(hours, 0);

export const sampleAppointments = [
{ startsAt: at(9), customer: { firstName: 'Charlie' } },
{ startsAt: at(10), customer: { firstName: 'Frankie' } },
{ startsAt: at(11), customer: { firstName: 'Casey' } },
{ startsAt: at(12), customer: { firstName: 'Ashley' } },
{ startsAt: at(13), customer: { firstName: 'Jordan' } },
{ startsAt: at(14), customer: { firstName: 'Jay' } },
{ startsAt: at(15), customer: { firstName: 'Alex' } },
{ startsAt: at(16), customer: { firstName: 'Jules' } },
{ startsAt: at(17), customer: { firstName: 'Stevie' } }
];
The GitHub repository contains a more complete set of sample data. You can use this by pulling the same file, src/sampleData.js, from the tag extracting-helpers.

This list also doesn't need to be test-driven, for a couple of reasons:

  • It's a list of static data with no behavior.
  • This module will be removed once we begin using our back-end API to pull data.
Test-driven development is often a pragmatic choice. Sometimes, not test-driving is the right thing to do.

Create a new file src/index.js and enter the following:

import React from 'react';
import ReactDOM from 'react-dom';
import { AppointmentsDayView } from './Appointment';
import { sampleAppointments } from './sampleData';

ReactDOM.render(
<AppointmentsDayView appointments={sampleAppointments} />,
document.getElementById('root')
);

That's all you'll need.

主站蜘蛛池模板: 门源| 仙桃市| 开化县| 赤城县| 福海县| 板桥市| 陵川县| 靖安县| 奉化市| 梨树县| 柏乡县| 华蓥市| 日喀则市| 革吉县| 离岛区| 渭南市| 乌恰县| 垫江县| 墨竹工卡县| 蒙山县| 景洪市| 丰顺县| 富阳市| 宁晋县| 海南省| 烟台市| 兴国县| 柳河县| 枝江市| 岐山县| 安图县| 城口县| 漾濞| 仪征市| 荣昌县| 鲁甸县| 亚东县| 嘉峪关市| 平湖市| 德清县| 东宁县|