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

window.navigator.userAgent cannot be mocked #585

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

window.navigator.userAgent cannot be mocked #585

elwayman02 opened this issue Sep 30, 2024 · 8 comments

Comments

@elwayman02
Copy link
Contributor

Seeing the following error when upgrading from v0.8.1 to v1.0.2:

Cannot set property userAgent of #<Navigator> which has only a getter

Here's an example of our code:

  setupWindowMock(hooks);

  hooks.beforeEach(function () {
    window.navigator.userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36';
  });
@elwayman02
Copy link
Contributor Author

As a workaround, I found that sinon can be used to get around this issue (though it's not quite ideal, as one would expect setupWindowMock to take care of this already).

Here's an example for anyone else having this issue:

sinon.stub(window.navigator, 'userAgent').get(() => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36');

@simonihmig
Copy link
Owner

Hm, that is weird. There is an explicit test for this here.

Just to make sure, window in your example is not using the global but the imported one, right?

@elwayman02
Copy link
Contributor Author

Yes, there's an explicit import and it worked in 0.8.1 but fails in 1.0.2.

@elwayman02
Copy link
Contributor Author

BTW even though I filed this at the same time as #586, they are occurring independently in two separate addons, not the same project. So these issues have been reproducible for me across multiple projects that were already using ember-window-mock 0.8.1 successfully.

@elwayman02
Copy link
Contributor Author

elwayman02 commented Oct 1, 2024

Here's a full look at what I have (this is a test util in an addon's test-support folder):

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

const MOBILE =
  'Mozilla/5.0 (iPhone; CPU iPhone OS 8_0_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12A366 Safari/600.1.4';

const DESKTOP =
  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36';

export default function setupIsMobile(hooks): void {
  setupWindowMock(hooks);

  hooks.beforeEach(function () {
    this.setIsMobile = (isMobile = true) => {
      if (isMobile) {
        window.navigator.userAgent = MOBILE;
      } else {
        window.navigator.userAgent = DESKTOP;
      }
    };
  });
}

Package versions:
ember-window-mock v1.0.2
ember-auto-import v2.6.3
ember-source v4.12.1
ember-cli v4.12.2
ember-qunit v8.0.2
@embroider/macros v1.16.5

@simonihmig
Copy link
Owner

BTW even though I filed this at the same time as #586, they are occurring independently in two separate addons, not the same project. So these issues have b

Still sounds very much like the same root cause. The only significant change has been the conversion to v2. Which makes ember-auto-import handle the addon. Any chance you could test this with the latest version of that?

@elwayman02
Copy link
Contributor Author

Even with ember-auto-import 2.8.1, the window object does not seem to get proxied by setupWndowMock

@simonihmig
Copy link
Owner

@elwayman02 would you be able to provide a simple reproduction repo?

Still failing to understand why it would not work for you while it is working in this repo's test-app...

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