-
Notifications
You must be signed in to change notification settings - Fork 0
/
react.html
138 lines (116 loc) · 4.14 KB
/
react.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>React Demo</title>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/mocha.css">
<style media="screen">
.messageBox {
border: 1px solid;
margin: 1em;
padding: 2em;
}
.error {
border-color: red;
background-color: #eebbbb;
}
.success {
border-color: blue;
background-color: #bbbbee;
}
.hidden {
display: none;
}
</style>
</head>
<body>
<main id="content"></main>
<div id="mocha"></div>
<script src="https://unpkg.com/babel-standalone"></script>
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/react.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/react-dom.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/[email protected]/chai.js"></script>
<script type="text/javascript" src="https://unpkg.com/[email protected]/mocha.js"></script>
<script type="text/babel" data-presets="react">
class NameInput extends React.Component {
constructor(props) {
super(props);
this.state = {
message: {
type: undefined,
body: undefined
}
}
this.buttonClicked = this.buttonClicked.bind(this);
}
buttonClicked(evt) {
let name = this.refs.nameInput.value;
this.setState({
message: {
type: name ? "success" : "error",
body: name ? "Welcome to React, " + name : "Please enter a name"
}
});
}
render(){
let msg = this.state.message;
return (
<div>
<label>Name: <input ref="nameInput" type="text" /></label>
<button id="inputButton" onClick={this.buttonClicked}>click me!</button>
<MessageBox type={msg.type} message={msg.body}/>
</div>
)
}
}
class MessageBox extends React.Component {
render() {
return (
<div className={"messageBox " + (this.props.type || "hidden")}>
{this.props.message}
</div>
)
}
}
ReactDOM.render(<NameInput />, document.getElementById('content'));
</script>
<script type="text/babel" data-presets="react">
const assert = chai.assert;
//only time you will see an error is if an assertion fails
mocha.ui("bdd") //behavior driven development
mocha.reporter("html")
//function provided by mocha
//First argument is name of test, second arg is function to run
describe("Example tests", () => {
it("Proved that math works", () => { //This is one test that asserts 2 different things
assert.equal(5, 3+2, "math works");
assert.notEqual(3+2, 6, "math still works");
});
it("Found our component", () => {
assert.isDefined(MessageBox, "Our MessageBox component is defined.")
});
it("Let us test a function", () => {
//create a function and assert on it
let myName = "Cameron";
const greet = (name) => "Hello, " + name + "!";
assert.include(greet(myName), myName, "Greeting includes name.")
});
});
describe("Document tests", () => {
it("Rendered an empty messageBox", () => {
let msgBox = document.querySelector(".messageBox") //retrieving messageBox class
assert.isDefined(msgBox, "Message box is in the document");
assert.include(msgBox.classList.value, "hidden", "Message box is hidden");
assert.equal(msgBox.innerText, "", "Message box is empty")
});
it("Rendered an error when name is empty", () => {
let msgBox = document.querySelector(".messageBox");
let button = document.querySelector("#inputButton"); //retrive inputButton by ID
button.click()
assert.include(msgBox.classList.value, "error", "Message box type is error");
});
});
mocha.run();
</script>
</body>
</html>