我有反應的應用程序是由創建反應應用程序使用笑話和酶進行測試如何使用Jest在我的測試文件上獲取window.location.pathname?
那麼我怎麼能得到我的測試文件中的window.location.pathname
值?
這是我的規格
import React from 'react';
import MovieDetailsBox from '../../src/components/movie_single/MovieDetailsBox';
import { expect } from 'chai';
import { shallow } from 'enzyme';
import { movie_details } from '../../src/data/movies_details'
import { empty_user } from '../../src/helper/sessionHelper';
jest.mock('react-router');
describe('Test <MovieDetailsBox /> Component',() => {
let movie_details_props = {
movie: {...movie_details}
}
let user_props = {
}
const context = { router: { isActive: (a, b) => true } }
const wrapper = shallow(
<MovieDetailsBox movie={movie_details_props}
user={user_props}
currentPath={'/movie/68'}/>, { context }
)
const movie_data_check = movie_details_props.movie;
it('MovieDetailsBox call correct function on rating box',() => {
wrapper.find('.hidden-xs .rate-movie-action-box button').simulate('click')
if(!empty_user(user_props)){
expect(window.location.pathname).to.equal('/login')
} else {
console.log('have user wow')
}
})
})
但我得到了這是一個錯誤我的控制檯
AssertionError: expected 'blank' to equal '/login'
好像window.location.pathname
回報的空白,而不是當前的URL路徑名上。
那麼我該如何解決這個問題,或者我需要在setupTests.js文件中設置其他任何東西?
謝謝!
你能爲MovieDetailsBox共享代碼? –
這只是一個正常的反應組件從類'MovieDetailsBox擴展組件' –