2016-05-06 28 views
8

nedeniyle "yönlendirici tanımlanmadı" atar Tepki yönlendirici kullanan bir Tepki bileşenine yönelik bir test yazıyorum. Chai ve Chai-jQuery ile Mocha kullanıyorum.Mocha-Chai, "Yönlendirici yönlendirici bileşeni"

Testlerim, bir bileşenin reaktif yönlendiriciden bir bileşene (ör. Bağlantı) içeri aktarılmasına kadar iyi çalışıyor. Bu noktada, aşağıdaki hatayı alıyorum:

ReferenceError: navigator is not defined 
    at Object.supportsHistory (/Users/nico/google-drive/code/agathos/client/node_modules/history/lib/DOMUtils.js:61:12) 

ben tepki-önyükleme için v0.29.3 güncellenen kadar tepki-bootstrap ile benzer bir hatayı alıyordum. Ben tepki-v2.4.0 (ve tarih v2.1.1) en son sürümü var. Ama sorun devam ediyor.

Bulduğum tek çözüm, node_modules/history/lib/DOMUtils: navigator'un window.navigator içine değiştirilmesidir. Bu bir hack olsa da, iyi bir çözüm değil.

Sorunun yanıt yönlendirici ile olduğunu düşünüyorum, ancak bir çözümüm yok.

Sadece burada, benim test-helper.js benim.

import jquery from 'jquery'; 
import React from 'react'; 
import ReactDOM from 'react-dom'; 
import TestUtils from 'react-addons-test-utils'; 
import jsdom from 'jsdom'; 
import chai, { expect } from 'chai'; 
import chaiJquery from 'chai-jquery'; 
import { Provider } from 'react-redux'; 
import { createStore } from 'redux'; 
import reducers from './reducers'; 

// set up a testing environment to run like a browser in the command line 
// create a fake browser and html doc 
global.document = jsdom.jsdom('<!doctype html><html><body></body></html>'); 
global.window = global.document.defaultView; 
// prevent jquery defaulting to the dom by giving it access to the global.window 
const $ = jquery(window); 

// build renderComponent function that should render a React class 
function renderComponent(ComponentClass, props = {}, appState = {}) { 
    const componentInstance = TestUtils.renderIntoDocument(
    <Provider store={createStore(reducers, appState)}> 
     <ComponentClass {...props} /> 
    </Provider> 
); 
    // produces html 
    return $(ReactDOM.findDOMNode(componentInstance)); 
} 

//build a helper for simulating events 
// $.fn allows you to add a custom function to your jquery library 
$.fn.simulate = function(eventName, value) { 
    if (value) { 
    // `this` allows you to access the object appended to 
    // `val()` is a jquery function that sets the value of selected html element 
    this.val(value); 
    } 
    // the [] are object method selectors, which allow you to access e.g. Simulate.change 
    TestUtils.Simulate[eventName](this[0]); 
}; 

// set up chai jquery 
chaiJquery(chai, chai.util, $); 

export {renderComponent, expect}; 

cevap

23

O react-routernavigator küresel kapsamda olduğunu varsayar gibi görünüyor.

Eğer test kurulum aşamasında küresel kapsama navigator eklemek gerekir, bu hatayı gidermek için:

global.navigator = { 
    userAgent: 'node.js' 
}; 
+0

bir cazibe gibi çalıştı! Teşekkür ederim. – Valjas

İlgili konular