redux测试写入期间的react-router SecurityError

时间:2017-03-09 02:36:52

标签: unit-testing testing redux react-router

我正在完成测试我的操作测试,并且遇到一个测试失败。导致其失败的奇怪之处在于其他测试。

import { browserHistory } from 'react-router';
//Passing action
export function signinUser({ email, password }) {
  return function(dispatch) {
    // Submit email/password to the server
    return axios.post(`${ROOT_URL}/signin`, { email, password })
      .then(response => {
        // If request is good...
        // - Update state to indicate user is authenticated
        dispatch({ type: AUTH_USER });
        // - Save the JWT token
        localStorage.setItem('token', response.data.token);
        localStorage.setItem('refreshToken', response.data.refreshToken);
        // - redirect to the route '/feature'
        browserHistory.push('/feature');
      })
      .catch(() => {
        // If request is bad...
        // - Show an error to the user
        dispatch(authError('Bad Login Info'));
      });
  }
}

//failing action
export function confirmationEmail(token){
  return function(dispatch) {
    return axios.post(`${ROOT_URL}/confirmation`, { token })
      .then(response => {
        //dispatch(emailWasSent(response.data.return_msg));
        // If request is good...
        // - Update state to indicate user is authenticated
        dispatch({ type: AUTH_USER });
        // - Save the JWT token
        localStorage.setItem('token', response.data.token);
        localStorage.setItem('refreshToken', response.data.refreshToken);
        // - redirect to the route '/feature'
        browserHistory.push('/feature');
      })
      .catch(response => {
        console.log(response)
        dispatch(authError(response.data.error));});
  }
}

这两种方法几乎完全相同。这两项测试几乎完全相同

describe('signinUser', () => {

    it('has the correct type and payload', () => {
      var scope = nock(ROOT_URL).post('/signin',function(body) {return { email: 'test@gmail.com', password: "test"}}).reply(200,{ token: "majorbs123" , refreshToken: "bs123"});
      const store = mockStore({});

      return store.dispatch(actions.signinUser('test@gmail.com',"test")).then(() => {
        const act = store.getActions();
        const expectedPayload = { type: AUTH_USER }
        expect(act[0].type).to.equal(expectedPayload.type);
        expect(localStorage.getItem("token")).to.equal("majorbs123");
        expect(localStorage.getItem("refreshToken")).to.equal("bs123");
      })


    });
  });
describe('confirmationEmail', () => {

    it('has the correct type and payload', () => {
      var scope = nock(ROOT_URL).post('/confirmation',function(body) {return { token: 'tokenbs123'}}).reply(200,{ token: "majorbs123" , refreshToken: "bs123"});
      const store = mockStore({});

      return store.dispatch(actions.confirmationEmail("tokenbs123")).then(() => {
        const act = store.getActions();
        const expectedPayload = { type: AUTH_USER }
        expect(act[0].type).to.equal(expectedPayload.type);
        expect(localStorage.getItem("token")).to.equal("majorbs123");
        expect(localStorage.getItem("refreshToken")).to.equal("bs123");
      })


    });
  });

signin的第一个测试没有问题,browserHistory.push没有问题。第二个测试抛出了这个错误堆栈。

SecurityError
    at HistoryImpl._sharedPushAndReplaceState (/home/mikewalters015/client/node_modules/jsdom/lib/jsdom/living/window/History-impl.js:87:15)
    at HistoryImpl.pushState (/home/mikewalters015/client/node_modules/jsdom/lib/jsdom/living/window/History-impl.js:69:10)
    at History.pushState (/home/mikewalters015/client/node_modules/jsdom/lib/jsdom/living/generated/History.js:71:31)
    at /home/mikewalters015/client/node_modules/history/lib/BrowserProtocol.js:87:27
    at updateLocation (/home/mikewalters015/client/node_modules/history/lib/BrowserProtocol.js:82:3)
    at pushLocation (/home/mikewalters015/client/node_modules/history/lib/BrowserProtocol.js:86:10)
    at /home/mikewalters015/client/node_modules/history/lib/createHistory.js:117:15
    at /home/mikewalters015/client/node_modules/history/lib/createHistory.js:90:9
    at next (/home/mikewalters015/client/node_modules/history/lib/AsyncUtils.js:51:7)
    at loopAsync (/home/mikewalters015/client/node_modules/history/lib/AsyncUtils.js:55:3)
    at confirmTransitionTo (/home/mikewalters015/client/node_modules/history/lib/createHistory.js:80:31)
    at transitionTo (/home/mikewalters015/client/node_modules/history/lib/createHistory.js:100:5)
    at Object.push (/home/mikewalters015/client/node_modules/history/lib/createHistory.js:131:12)
    at Object.push (/home/mikewalters015/client/node_modules/history/lib/useBasename.js:73:22)
    at Object.push (/home/mikewalters015/client/node_modules/history/lib/useQueries.js:81:22)
    at /home/mikewalters015/client/src/actions/authActions.js:106:2
    at process._tickCallback (internal/process/next_tick.js:103:7)

它引发了我的循环,因为代码非常相似,抛出问题的行反应路由器推送方法也用于其他方法,并且不会产生任何问题。

0 个答案:

没有答案