-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact.html
120 lines (94 loc) · 3.33 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
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<style>
.bad {color:red}
.good {color:green}
.hidden {display:none}
</style>
<link rel="stylesheet" href="https://unpkg.com/mocha@3.4.2/mocha.css">
</head>
<body>
<main id="content">
</main>
<div id="mocha"></div>
<script type="text/javascript" src="https://unpkg.com/babel-standalone@6.26.0/babel.js"></script>
<script type="text/javascript" src="https://unpkg.com/react@16.0.0/umd/react.production.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/react-dom@16.0.0/umd/react-dom.production.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/chai@4.0.2/chai.js"></script>
<script type="text/javascript" src="https://unpkg.com/mocha@3.4.2/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 ? 'good' : 'bad',
body: name ? 'welcome ' + name : 'Meh'
}
});
}
render() {
let msg = this.state.message;
return (
<div>
<label>Name: <input ref="nameInput"/></label>
<button id="btnInput" onClick={this.buttonClicked}>Click Me!</button>
<MessageBox type={msg.type} message={msg.body}/>
</div>
);
}
}
class MessageBox extends React.Component {
render() {
return (
<div class={"msgbox " + (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;
mocha.ui('bdd');
mocha.reporter('html');
describe("example tests", () => {
it("Prove math works", () => {
assert.equal(5, 3+2, "Math Works!");
})
it("testing a function", () => {
let myname = "dave";
const greet = (name) => "Hello " + name;
assert.include(greet(myname),myname, "Greeting includes name" )
})
});
describe("Document tests", () => {
it('rendered an empty message box', () => {
let msgBox = document.querySelector('.msgbox');
assert.isDefined(msgBox, "Msgbox exists in DOM");
assert.include(msgBox.classList.value, 'hidden', "msgbox is hidden");
assert.equal(msgBox.innerText,"", "is empty")
});
it('rendered an error on empty name', () => {
let msgBox = document.querySelector('.msgbox');
let button = document.querySelector('#btnInput');
button.click();
assert.include(msgBox.classList.value, 'bad', "error class applied");
});
});
mocha.run();
</script>
</body>
</html>