Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setupWindowMock is not mocking window.location.href #586

Open
elwayman02 opened this issue Sep 30, 2024 · 4 comments
Open

setupWindowMock is not mocking window.location.href #586

elwayman02 opened this issue Sep 30, 2024 · 4 comments

Comments

@elwayman02
Copy link
Contributor

When upgrading from v0.8.1 to v1.0.2, we're experiencing a regression in mocking window.location.href.

import window from 'ember-window-mock';
import { setupWindowMock } from 'ember-window-mock/test-support';

// ...

window.location.href = 'https://www.foobar.com'

When href gets set, the test window in the browser is transitioning to foobar.com, even though the README states that it's expected to do nothing when setting the mock (other than update the value).

@elwayman02
Copy link
Contributor Author

I also tried switching to window.location = 'https://www.foobar.com', based on this comment: https://github.com/simonihmig/ember-window-mock/blob/master/ember-window-mock/src/test-support/-private/window.js#L67

However, doing that throws a different error:

TypeError: Illegal invocation
    at Object.set (<anonymous>)
    at Object.set (webpack://__ember_auto_import__/./node_modules/ember-window-mock/src/index.js?:10:167)
    at Object.<anonymous> (http://localhost:4200/assets/tests.js:346:41)

@simonihmig
Copy link
Owner

I assume this is in a test, that has setupWindowMock(hooks) called in its parent module?

Again here, this has test coverage, so I tend to assume something specific to your usage is in play here?

One thing you can look into is to console.log(window.location). If it's the mocked one, it just show up as a Proxy rather than Location.

@elwayman02
Copy link
Contributor Author

elwayman02 commented Oct 1, 2024

Yes, setupWindowMock(hooks); is called in the parent module, before hooks.beforeEach(). Interestingly, window.location is NOT showing up as a Proxy, and even though I'm calling setupWindowMock, I can't hit a breakpoint inside the method itself.

I inspected setupWindowMock itself and got this:

> _testSupport.setupWindowMock
< ƒ setupWindowMock(hooks=self){hooks.beforeEach(()=>(0,_index_js__WEBPACK_IMPORTED_MODULE_1__._setCurrentHandler)((0,_window_js__WEBPACK_IMPORTED_MODULE_0__.createWindowProxyHandler)()));hooks.afterEach…

Again, this worked fine in 0.8.1 and only stopped working when bumping the major version.

@elwayman02
Copy link
Contributor Author

elwayman02 commented Oct 1, 2024

Here's what I have:

import { module, test } from 'qunit';
import window from 'ember-window-mock';
import { setupWindowMock } from 'ember-window-mock/test-support';

module('Unit | Util | some-util', function (hooks) {
  setupWindowMock(hooks);

  test('injectWalkmeBundle works for other countries', function (assert) {
    window.location.href = 'https://www.foobar.com';
    // At this point, `window.location` is not a proxy
  });

Package versions:
ember-window-mock v1.0.2
ember-auto-import v2.6.3
ember-source v4.12.3
ember-cli v4.12.2
ember-qunit v6.2.0
@embroider/macros v1.16.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants