- Mastering React Test:Driven Development
- Daniel Irvine
- 344字
- 2021-06-24 14:45:06
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' } }
];
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.
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.
- C++ Primer習(xí)題集(第5版)
- Azure IoT Development Cookbook
- PHP程序設(shè)計(jì)(慕課版)
- R語(yǔ)言數(shù)據(jù)可視化之美:專(zhuān)業(yè)圖表繪制指南
- Visual Basic程序設(shè)計(jì)教程
- SEO智慧
- 智能搜索和推薦系統(tǒng):原理、算法與應(yīng)用
- Java Web從入門(mén)到精通(第2版)
- C++ System Programming Cookbook
- Java程序設(shè)計(jì)基礎(chǔ)(第6版)
- 人人都能開(kāi)發(fā)RPA機(jī)器人:UiPath從入門(mén)到實(shí)戰(zhàn)
- Node.js應(yīng)用開(kāi)發(fā)
- C語(yǔ)言從入門(mén)到精通(微視頻精編版)
- HTML 5與CSS 3權(quán)威指南(第4版·上冊(cè))
- C#開(kāi)發(fā)之道