-
-
Notifications
You must be signed in to change notification settings - Fork 771
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
Add support for React Native window location format Fixes sinonjs/sin… #1363
Conversation
lib/sinon/util/fake_server.js
Outdated
if (typeof window.location !== "undefined") { | ||
// Browsers place location on window | ||
return window.location; | ||
} else if ((typeof window.window !== "undefined") && (typeof window.window.location !== "undefined")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow, that is proper weird, but if this works ... then fine.
window.window.location
? jeez 😸
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, for reasons unknown to me, this does indeed seem to be where location
is stored, in the versions of React Native that I tested against.
lib/sinon/util/fake_server.js
Outdated
if ( typeof window !== "undefined") { | ||
if (typeof window.location !== "undefined") { | ||
// Browsers place location on window | ||
return window.location; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you use return inside if
statements, you can avoid using else
.
See http://rmurphey.com/blog/2012/12/10/js-conditionals
Also, you can unfold this code a bit, so there are fewer levels of indentation == easier to understand.
function getWindowLocation() {
if (typeof window === "undefined") {
// Fallback
return getDefaultWindowLocation();
}
if (typeof window.location !== "undefined") {
// Browsers place location on window
return window.location;
}
if ((typeof window.window !== "undefined") && (typeof window.window.location !== "undefined")) {
// React Native on Android places location on window.window
return window.window.location;
}
return getDefaultWindowLocation();
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your suggestion. I agree, it can be simplified in this way.
Patch suggestions applied. :) |
Thanks for your contributions! |
|
Purpose (TL;DR)
Fix issue #1362 and allows sinon to be used with React Native builds (where the location object is not stored directly on window).
Preserves existing behaviour for all other environments, including fallback object.
How to verify
Check out this branch (see github instructions below)
npm install
import sinon into a React Native project and test on Android and iOS that it does not raise the error
undefined is not an object (evaluating 'wloc.protocol')