File size: 2,501 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import WhatsYourName from '../src/03-WhatsYourName.js';

describe("03 - What's Your Name?", () => {
  var component;
  var paragraphs;
  var inputs;

  var nameInParagraphEqualsTo = (paragraph, name) => {
    var children = paragraph.props.children;
    var text = _.isArray(children) ? children.join("") : children;
    return _.isEqual(text, `Hello ${ name }`);
  }

  beforeEach( () => {
    var elem = document.createElement('div');
    elem = document.body.appendChild(elem);
    component = React.render(React.createElement(WhatsYourName), elem);
    paragraphs = React.addons.TestUtils.scryRenderedDOMComponentsWithTag(component, 'p');
    inputs = React.addons.TestUtils.scryRenderedDOMComponentsWithTag(component, 'input');
  });

  afterEach( () => {
    React.unmountComponentAtNode(React.findDOMNode(component));
  });

  describe("should complete all tasks", () => {
    describe("Task #1 - greet user", () => {
      it("Should change name in paragraph on name change in input", () =>{
        var input     = _.first(inputs);
        var paragraph = _.first(paragraphs);

        assert.equal(paragraphs.length, 1, "There should be only one `p` element rendered by the component");
        assert.equal(inputs.length, 1, "There should be only one `input` rendered by the component");

        React.addons.TestUtils.Simulate.change(input, {target: { value: "XYZ" } });
        assert.equal(nameInParagraphEqualsTo(paragraph, 'XYZ'), true, "After changing name in input, I should see the change in `p` element's content. In other words: `this.state.name` should change.");

        React.addons.TestUtils.Simulate.change(input, {target: { value: "ZYX" } });
        assert.equal(nameInParagraphEqualsTo(paragraph, 'ZYX'), true, "After changing name in input for the second time, we should see the change in `p` element. In other words: `this.state.name` should change.");
      });
    });

    describe("Task #2 - don't greet user if name wasn't provided", () => {
      it("Should display welcoming message if user didn't provide their name", () => {
        var input     = _.first(inputs);
        var paragraph = _.first(paragraphs);

        React.addons.TestUtils.Simulate.change(input, {target: { value: "" } });
        assert.equal(paragraph.props.children, "Hey there. Enter your name.",
          "If user didn't enter the name (`this.state.name` length is 0), show \"Hey there. Enter your name.\". See the hint in task's description."
        );
      });
    });
  });
});