Clojurescript/Reagent 单元测试组件输出和行为

Clojurescript/Reagent Unit testing component output and behaviour

我们用了一年左右的react,数据不可变,印象深刻。我们现在想移动到 Clojurescript/Reagent,但是我们需要一种很好的方法来测试我们的代码。对于组件,这就是我们所做的:

  1. 测试组件的输出,取决于我们发送的道具:
  2. 测试组件在发生单击等事件时是否调用正确的函数并使用正确的参数

对于 1 来说:

function renderFFC(filters, search_criterias)
{
    return TestUtils.renderIntoDocument(React.createElement(FilterIntervalNumberComponent,{filter:filters, search_criteria:search_criterias}));
}

describe("IN", function(){
    it("should render search criteria - interval", function() {
        var criterias = {};
        var ffc = renderFFC(filter, criterias);
        expect(ffc.refs[0].getDOMNode().value).toBeNull();
        expect(ffc.refs[1].getDOMNode().value).toBeNull();
     });

对于 2,它会是这样的:

describe("OUT", function(){
    it("should change the values, then click - boolean ", function() {
        //mock function
        set_criteria_and_search = jest.genMockFunction();

        var fbc = renderFBC(filter, {});

        React.addons.TestUtils.Simulate.change(fbc.refs.yes.getDOMNode(),{nativeEvent: {target: {value: true}}});

        expect(controller.set_criteria_and_search.mock.calls)
          .toEqual(
                    [['taxes_applied',{'taxes_applied':[{value:"1"}]}]]
                );
     });

我们使用 facebook Jest 进行测试。

如何使用 Reagent 在 Clojurescript 中做同样的事情,最好是自动进行测试 运行?

检测试剂成分:

测试组件输出。对于一个组件:

 (defn weather-component [city temp country]
  [:div#weather
    [:h2#city {:on-click #(do-something 101)} city ]
    [:h3#temp temp]
    [:h3#contry country]])

测试:

 (deftest weather-component-test-in
  ;;WHEN render component in test
  (let [comp (r/render-component [w/weather-component "Paris" 12]
                             (. js/document (getElementById "test")))]
    ;;ASSERT
    (is (= (d/html (sel1 :#city)) "Paris"))
    (is (= (d/html (sel1 :#temp)) "12"))))

测试组件在发生单击等事件时是否调用正确的函数并使用正确的参数

(deftest weather-component-test-out 
 (let [comp (r/render-component [w/weather-component "London" 0]
                             (. js/document (getElementById "test"))) 
    expected-invocations (atom [])] 
    (with-redefs [weather-app.core/do-something #(swap! expected-invocations conj %)] 
      (sim/click (sel1 :#city) {})
      (is (=[101] @expected-invocations)))))

操纵 dom 和模拟事件:

[cljs-react-test "0.1.3-SNAPSHOT"] 和 dommy (d/...)