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

Injecting Stubs Idea #114

Open
dschinkel opened this issue Nov 1, 2017 · 1 comment
Open

Injecting Stubs Idea #114

dschinkel opened this issue Nov 1, 2017 · 1 comment

Comments

@dschinkel
Copy link

dschinkel commented Nov 1, 2017

So I installed v0.0.3 and noticed someone added a testSpec.js (https://gist.github.com/dschinkel/293e86b3f65f7e4ddfd024980514ebcf) which had exactly what I'd like to do in my tests as a client of the lib.

Difference is obviously testSpec.js has access to the device object core and can directly instantiate it (at least looks like from that testSpec.js), but of course it's read only to users.

So...this is easy, if someone wants to implement it, just export a property that can be set (e.g. stub or whatever name makes sense for this exported value)...that would allow people to set stubs. Then in the core devicejs code, it's probably already doing this in the new PR code? It would just check that property internally to see if it's been set, if so use the stub.window, else, continue on using the real window object. (so it'd just be stub.window || window at the top somewhere in the core code whereever it first starts to reference window essentially, and probably add some logic to handle if some properties aren't stubbed, etc.)

So I envision being able to do something like this in my test:

it('displays alternate subtitle and description for NFL non-game content', () => {
  //...code here and then:

  const window = {
    navigator: {
      userAgent: "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko)"
    }
  };

  device.stub.window = window; // this is where we'd set the stub and devicejs would check that value internally to see if it's a literal or not. If so then devicejs internally would just use the stub instead of the real window object

  console.log(device.mobile) // would return true!

  //... rest of the test code etc.
});

This would be a very welcomed change. Right now I don't know what the last code is so I can't figure out where or how to contribute so if someone wanted to just add that exported value for us, that would be appreciated.

Essentially my test above was passing because the code checking agent was using the global.window (so I was able to stub it using navigator.__defineGetter__('userAgent', () => agent);). But we changed some of our code to use device.js instead. And now because of that I'm no longer able to still stub/fake the window, so now this test won't pass. So until we get a way to inject a stub like that for writing tests, I'm going to have to skip my test in our test suite for now.

@JSteunou
Copy link
Contributor

wrong repo I guess, there is no v0.0.3 of this lib

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

3 participants