-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Stubbing/patching window.location.href
in JSDom 8?
#1388
Comments
So, the issue is that you can't set location in browsers, without navigating. And jsdom is trying to behave like a browser. (Although it doesn't do the navigation part yet.) The way we generally work around this is adding methods like |
Alternately, it might be a better solution to add something like |
Ah yeah, makes sense. An API for either of those sounds great — didn't realize there was already a |
I am also running into with test written for |
Can you explain more how that works, and how you would test that code in browsers? |
@domenic, something like this jest.autoMockOff()
jest.setMock('../lib/window', window)
jest.mock('cookies-js')
jest.mock('query-string')
jest.mock('superagent')
describe(['@utils/auth - When an AJAX response returns:'
].join('')
, function () {
beforeEach(function () {
window.location.href = 'http://quux.default.com'
var queryString = require('query-string')
queryString.__setMockParseReturns([{
'access_token': '1234foo',
'expires_in': '9999'
}])
})
it(['should set a redirect token and goto platform ',
'when the AJAX request returns 401.'
].join('')
, function () {
var superagent = require('superagent')
superagent.__setMockAjaxResponses([
[null, { 'status': 401 }]
])
var href = window.location.href
var auth = require('../index.js')
auth.login(function (res) {})
var Cookies = require('cookies-js')
var config = require.requireActual('../config')
expect(decodeURIComponent(window.location.href)).toBe([
config.loginUrl,
config.loginServiceLogin,
'?client_id=',
config.clientId,
'&response_type=token',
'&redirect_uri=',
config.clientRedirectUri
].join(''))
expect(Cookies.__getMockCookieData()[config.clientId + '_state_locationAfterLogin']).toBe(escape(href))
}) This test would be kind of difficult to do without something like Selenium. But that's kind of what makes |
Thoughts? |
Hi @domenic , Your example sounds promising however I am a failure of imagination implementing it. At present I have the following: /*global global*/
import jsdom from 'jsdom';
const doc = jsdom.jsdom('<!doctype html><html><body></body></html>', {
url: 'http://test/'
});
const win = doc.defaultView;
global.document = doc;
global.window = win;
Object.keys(window).forEach((key) => {
if (!(key in global)) {
global[key] = window[key];
}
});
// All individually output http://test/
jsdom.reconfigureWindow(window, {url: 'http://localhost'});
jsdom.reconfigureWindow(window, {location: 'http://localhost'});
jsdom.reconfigureWindow(global.window, {url: 'http://localhost'});
window.location.href = 'http://localhost';
location.href = 'http://localhost';
console.log('window.location.href', window.location.href); Help?!? |
I'd strongly suggest not copying properties onto the global. Also, I can't run that code in my Node.js, so I can't really help with it. |
Thanks for the follow up. I followed Tero's tutorial http://teropa.info/blog/2015/09/10/full-stack-redux-tutorial.html and he states that for running the test suite as standalone that React needs access to many of these variables (such as navigator) on the global state. I guess there will (should) be a better way of testing React components with JSDom that doesn't litter the global scope, I'll look into that. Even if you cannot run it, if you expect that |
It does not, that was never implemented after all. |
Right, that was just an idea. I guess I can try to work on |
Any updates on this? This would be helpful to mock out any code that references |
Yes, this was implemented recently: https://github.com/tmpvar/jsdom#changing-the-url-of-an-existing-jsdom-window-instance |
Just discovered that, awesome! 👍 |
I've just noticed that content on the link @domenic posted above possibly lives on this one now https://github.com/tmpvar/jsdom#reconfiguring-the-jsdom-with-reconfiguresettings |
We've got some code in use in tests that worked in jsdom 7 but appears not to work in jsdom 8.
Our JSDom setup:
And the test itself:
Adding some logging immediately after setting
window.location.href
shows that the value wasn't actually updated or changed, and other means overriding (e.g. stubbing withsinon
) don't seem to work either since it's implemented via getters/setters rather than an actual property. What's the best way to override this value?It looks like setting is supposed to do something based on https://github.com/tmpvar/jsdom/blob/master/lib/jsdom/living/window/Location-impl.js#L46 but I can't figure out what the final effect of that is supposed to be.
The text was updated successfully, but these errors were encountered: