-
Notifications
You must be signed in to change notification settings - Fork 14
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.document
cannot be mocked / replaced
#99
Comments
Hm, that's weird, didn't know of that behaviour! Wouldn't be opposed to it, if you want to contribute this!? 😉 The API of your example would only allow to mock a property on the root level of Alternatively maybe add some method to any Proxy we create, like |
I also did not know before. 😅
Sure, I'd love to!
Good point. I usually tend to avoid mocking things partially, but I guess especially for
I thought about that too, but rejected it, because it would change the behavior of code that iterates over properties on objects. We also can't guarantee, that users / libraries might not already use the I'd either allow specifying paths, like you suggested, or allow the user to pass the target proxy as the first parameter: mock(window.document, 'querySelector', (selector) => ...); But I think I prefer: mock('document.querySelector', (selector) => ...); |
Another alternative would be a function to retrieve the import window, { getHolder } from 'ember-window-mock';
module('foo', function(hooks) {
hooks.beforeEach(function() {
class DocumentMock extends EventTarget {
// ...
}
getHolder(window).document = new DocumentMock());
});
}); Also tangentially related: I think it would reduce complexity, if we'd remove all the special handling here and just assign these properties to the This would then also allow users to mock objects like |
I think I would prefer the
Agree, good point!
Haven't thought much about the implementation yet. But re: iterating, you could define it as not enumerable I think, or expose that function through the Proxy's get-handler maybe? Just thinking aloud, but I guess it should be doable to hide that function from enumeration!? It would probably make having access to the holder easier, as you could closure over it (is that how you call it, I have no idea!? 😂). Otherwise you would probably have to apply that |
I tried something like this:
But this throws the following error:
The reason being
— StackOverflow
With the current setup, it is not possible to completely mock properties like
document
.Can we maybe add a method / export a function to directly set properties on the
holder
? This way we could do something like:The text was updated successfully, but these errors were encountered: