2016-07-06 111 views
1

我有一個簡單的反應組件,其中有一個onClick事件可以重定向到另一個頁面 - Google搜索引擎(用於測試目的)。使用Mocha,Karma測試window.location.href中的window.location.href

這裏的的jsfiddle鏈接的陣營組成部分 - http://jsfiddle.net/t9dva2f7/

describe('Events', function() { 
    it('should have click event state', function (done) { 
     var events = Events(); 
     ReactTestUtils.renderIntoDocument(events); 
     ReactTestUtils.Simulate.click(events.refs.button.getDOMNode()); 
     //Test to see if the page redirects to correct url? 
     done(); 
    }); 
}); 

如何測試瀏覽器被重定向到正確的網址是什麼?我應該嘲笑window.location.href?或者還有別的嗎?

回答

0

我知道這是一個遲到的反應,但也許它會對別人有幫助。您可以直接測試window.location.href。相反,你存根或用另一個函數包裝它,並檢查包裝器/存根是否被調用。然後你可以自然地檢查通過的URL。