簡體   English   中英

Clojurescript /試劑單元測試組件的輸出和行為

[英]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進行測試。

如何在Clojurescript中使用Reagent做相同的事情,最好自動運行測試?

測試試劑成分:

測試組件輸出。 對於組件:

 (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 / ...)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM