Browse Source

video13: test React component methods.

Frederic G. MARAND 6 years ago
parent
commit
b07e46da12
2 changed files with 17 additions and 0 deletions
  1. 4 0
      src/App.js
  2. 13 0
      src/App.test.js

+ 4 - 0
src/App.js

@@ -41,6 +41,10 @@ class App extends Component {
     this.setState({ lifeCycle: "componentWillReceiveProps" })
   }
 
+  handleStrings(s) {
+    return s === "Hello, world";
+  }
+
   render() {
     return (
       <div className="App">

+ 13 - 0
src/App.test.js

@@ -99,6 +99,19 @@ describe("<App /> shallow rendering", () => {
     expect(App.prototype.componentWillReceiveProps.mock.calls.length).toBe(1);
     expect(w.find(".lifeCycle").text()).toBe("componentWillReceiveProps");
   });
+
+  it("should return correctly from handleStrings()", () => {
+    // instance() provides access to the underlying class implementing the component.
+    const expectations = {
+      "Hello, world": true,
+      "": false,
+    };
+
+    for (const [value, expected] of Object.entries(expectations)) {
+      const actual = wrapper.instance().handleStrings(value);
+      expect(actual).toBe(expected);
+    }
+  });
 });
 
 describe("<App /> mount rendering", () => {