无法在Jest(Babel-Jest)酶中使用窗口模拟URL更改

时间:2019-01-16 23:45:42

标签: javascript reactjs jestjs enzyme

我一直想弄清楚如何在Jest中更改URL。我已经搜索了github请求和stackoverflow以获得解决方案,但是没有找到任何有效的方法。也许我在这里做错了什么...

到目前为止,我已经尝试过:

无济于事..我的窗口变得不确定,所以我根本无法读取网址...

我正在使用: “ babel-jest”:“ ^ 22.4.3”, “ enzyme”:“ ^ 3.3.0”, “ enzyme-adapter-react-16”:“ ^ 1.1.1”,

NavDrawer.test.jsx

import React from 'react';
import { configure, mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import MemoryRouter from 'react-router-dom/MemoryRouter';
import NavDrawer from '../../../src/shared/components/NavDrawer';

configure({ adapter: new Adapter() });
// prettier-ignore
describe('<NavDrawer />', () => {
  let wrapper;
  beforeEach(() => {
    wrapper = mount((
      <MemoryRouter>
        <NavDrawer />
      </MemoryRouter>
    )).find('PersistentDrawer');
  });
  it('renders AppBar component and Drawer component', () => {
    expect(wrapper.exists('AppBar')).toEqual(true);
    expect(wrapper.exists('Drawer')).toEqual(true);
  });

  describe('when a user clicks the hamburger button', () => {
    beforeEach(() => {
      wrapper.find('IconButton').first().simulate('click');
    });
    it('should set state open to true', () => {
      expect(wrapper.instance().state.open).toEqual(true);
    });

    describe('when user click on nav item', () => {
      let nestedList;
      let security;        
      beforeEach(() => {
        nestedList = wrapper.find('NestedList');
        security = nestedList.find('ListItem').at(3);
        security.simulate('click');        
        history.replaceState({}, '', '/security');
      });

      it('should update url to match route', () => {
        expect(window).to.equal('/security');
      });

      it('should change App bar title in NavDrawer to `Security Dashboard`', () => {
        expect(wrapper.find('Typography').first().text()).toBe('Security Dashboard');
      });

      it('should set state open back to false', () => {
        expect(wrapper.instance().state.open).toEqual(false);
      });
    });
  });
});

package.json

"jest": {
    "verbose": true,
    "testURL": "http://localhost",
    "testEnvironment": "jsdom",
    "moduleNameMapper": {
      "\\.(css|less|scss|sass)$": "identity-obj-proxy"
    },
    "setupFiles": [
      "raf/polyfill"
    ],
    "coverageDirectory": "./coverage/",
    "transform": {
      "^.+\\.(js|jsx)$": "babel-jest",
      "^.+\\.(css|scss|less)$": "jest-css-modules"
    },
    "transformIgnorePatterns": [
      "node_modules/(?!(@core)/)"
    ],
    "globals": {
      "NODE_ENV": "test",
      "window": {}
    },
    "moduleFileExtensions": [
      "js",
      "jsx"
    ],
    "moduleDirectories": [
      "node_modules"
    ]
  },

0 个答案:

没有答案