Skip to content

Commit

Permalink
Merge pull request #535 from simonihmig/fix-sinon
Browse files Browse the repository at this point in the history
Fix sinon.stub on window object with latest sinon
  • Loading branch information
simonihmig committed Jun 13, 2023
2 parents 9a807c5 + 5d19fdc commit 23c0d5d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
9 changes: 9 additions & 0 deletions addon-test-support/-private/mock/proxy.js
Expand Up @@ -33,6 +33,15 @@ export default function proxyFactory(original) {
delete target[prop];
return true;
},
getOwnPropertyDescriptor(target, property) {
return (
Reflect.getOwnPropertyDescriptor(holder, property) ??
Reflect.getOwnPropertyDescriptor(target, property)
);
},
defineProperty(target, property, attributes) {
return Reflect.defineProperty(holder, property, attributes);
},
});

return proxy;
Expand Down
9 changes: 9 additions & 0 deletions addon-test-support/-private/window.js
Expand Up @@ -69,6 +69,15 @@ export const mockProxyHandler = {
delete target[prop];
return true;
},
getOwnPropertyDescriptor(target, property) {
return (
Reflect.getOwnPropertyDescriptor(holder, property) ??
Reflect.getOwnPropertyDescriptor(target, property)
);
},
defineProperty(target, property, attributes) {
return Reflect.defineProperty(holder, property, attributes);
},
};

export function reset() {
Expand Down
8 changes: 8 additions & 0 deletions addon/index.js
Expand Up @@ -23,6 +23,8 @@ if (DEBUG) {
set: Reflect.set,
has: Reflect.has,
deleteProperty: Reflect.deleteProperty,
getOwnPropertyDescriptor: Reflect.getOwnPropertyDescriptor,
defineProperty: Reflect.defineProperty,
};

let currentHandler = doNothingHandler;
Expand All @@ -44,6 +46,12 @@ if (DEBUG) {
deleteProperty() {
return currentHandler.deleteProperty(...arguments);
},
getOwnPropertyDescriptor() {
return currentHandler.getOwnPropertyDescriptor(...arguments);
},
defineProperty() {
return currentHandler.defineProperty(...arguments);
},
};

exportedWindow = new Proxy(window, proxyHandler);
Expand Down
6 changes: 2 additions & 4 deletions tests/unit/sinon-test.js
Expand Up @@ -18,14 +18,12 @@ module('sinon', function (hooks) {
assert.true(window.testFn.calledOnce);
});

// TODO: https://github.com/simonihmig/ember-window-mock/issues/478
test.skip('it can stub window.confirm', function (assert) {
test('it can stub window.confirm', function (assert) {
sinon.stub(window, 'confirm').returns(true);
assert.true(window.confirm(), 'window.confirm can be stubbed');
});

// this fails inside sinon with `Attempted to wrap undefined property fetch as function`, as it does some weird things that seem to not play nicely with our Proxies
test.skip('it can spy on window.fetch', function (assert) {
test('it can spy on window.fetch', function (assert) {
sinon.spy(window, 'fetch');
window.fetch();
assert.true(window.fetch.calledOnce, 'window.fetch can be spied on');
Expand Down

0 comments on commit 23c0d5d

Please sign in to comment.