How to test url change with Jest

前端 未结 2 1055
一向
一向 2020-12-17 14:52

I have the following function to test

function tradePage() {
  setTimeout(function() {
    window.location.pathname =          


        
相关标签:
2条回答
  • 2020-12-17 15:35

    I found a working method by declaring in the beginning of the test a global variable:

    global.window = { location: { pathname: null } };
    

    and checked this variable like this:

    expect(global.window.location.pathname).toEqual('/new-url');
    

    That worked fine.

    0 讨论(0)
  • 2020-12-17 15:38
    Object.assign(location, { host: "www.newhost.com", pathname: 'file.txt' });
    

    It will update all the location URL object properties (origin, href, hostname, host).

    so the final href property will be https://www.newhost.com/file.txt.

    Do this in a beforeEach block.

    just call console.log(location) to verify the results. Happy coding!

    0 讨论(0)
提交回复
热议问题