[IMP] web: use patch for object in tests to avoid edge effects

This commit is contained in:
Christophe Matthieu
2018-02-08 15:46:00 +01:00
parent 1d49d52b3a
commit d288b33648
5 changed files with 109 additions and 77 deletions
+61 -25
View File
@@ -536,47 +536,83 @@ function removeSrcAttribute($el, widget) {
var patches = {};
/**
* Patches a given Class with the given properties.
* Patches a given Class or Object with the given properties.
*
* @param {Class} Klass
* @param {Class|Object} target
* @param {Object} props
*/
function patch (Klass, props) {
function patch (target, props) {
var patchID = _.uniqueId('patch_');
Klass.__patchID = patchID;
target.__patchID = patchID;
patches[patchID] = {
Klass: Klass,
target: target,
otherPatchedProps: [],
ownPatchedProps: [],
};
_.each(props, function (value, key) {
if (Klass.prototype.hasOwnProperty(key)) {
patches[patchID].ownPatchedProps.push({
key: key,
initialValue: Klass.prototype[key],
});
} else {
patches[patchID].otherPatchedProps.push(key);
}
});
Klass.include(props);
if (target.prototype) {
_.each(props, function (value, key) {
if (target.prototype.hasOwnProperty(key)) {
patches[patchID].ownPatchedProps.push({
key: key,
initialValue: target.prototype[key],
});
} else {
patches[patchID].otherPatchedProps.push(key);
}
});
target.include(props);
} else {
_.each(props, function (value, key) {
if (key in target) {
var oldValue = target[key];
patches[patchID].ownPatchedProps.push({
key: key,
initialValue: oldValue,
});
if (typeof value === 'function') {
target[key] = function () {
var oldSuper = this._super;
this._super = oldValue;
var result = value.apply(this, arguments);
if (oldSuper === undefined) {
delete this._super;
} else {
this._super = oldSuper;
}
return result;
};
} else {
target[key] = value;
}
} else {
patches[patchID].otherPatchedProps.push(key);
target[key] = value;
}
});
}
}
/**
* Unpatches a given Class.
* Unpatches a given Class or Object.
*
* @param {Class} Klass
* @param {Class|Object} target
*/
function unpatch(Klass) {
var patchID = Klass.__patchID;
function unpatch(target) {
var patchID = target.__patchID;
var patch = patches[patchID];
_.each(patch.ownPatchedProps, function (p) {
Klass[p.key] = p.initialValue;
});
_.each(patch.otherPatchedProps, function (key) {
delete Klass.prototype[key];
target[p.key] = p.initialValue;
});
if (target.prototype) {
_.each(patch.otherPatchedProps, function (key) {
delete target.prototype[key];
});
} else {
_.each(patch.otherPatchedProps, function (key) {
delete target[key];
});
}
delete patches[patchID];
delete Klass.__patchID;
delete target.__patchID;
}
// Loading static files cannot be properly simulated when their real content is
+12 -13
View File
@@ -1656,18 +1656,18 @@ QUnit.module('Views', {
assert.expect(1);
var instanceNumber = 0;
var initWidget = mixins.ParentedMixin.init;
mixins.ParentedMixin.init = function () {
instanceNumber++;
return initWidget.apply(this, arguments);
};
var destroyWidget = mixins.ParentedMixin.destroy;
mixins.ParentedMixin.destroy = function () {
if (!this.isDestroyed()) {
instanceNumber--;
testUtils.patch(mixins.ParentedMixin, {
init: function () {
instanceNumber++;
return this._super.apply(this, arguments);
},
destroy: function () {
if (!this.isDestroyed()) {
instanceNumber--;
}
return this._super.apply(this, arguments);
}
return destroyWidget.apply(this, arguments);
};
});
var params = {
View: CalendarView,
@@ -1706,8 +1706,7 @@ QUnit.module('Views', {
calendar.destroy();
mixins.ParentedMixin.init = initWidget;
mixins.ParentedMixin.destroy = destroyWidget;
testUtils.unpatch(mixins.ParentedMixin);
});
});
+12 -13
View File
@@ -6342,18 +6342,18 @@ QUnit.module('Views', {
assert.expect(1);
var instanceNumber = 0;
var initWidget = mixins.ParentedMixin.init;
mixins.ParentedMixin.init = function () {
instanceNumber++;
return initWidget.apply(this, arguments);
};
var destroyWidget = mixins.ParentedMixin.destroy;
mixins.ParentedMixin.destroy = function () {
if (!this.isDestroyed()) {
instanceNumber--;
testUtils.patch(mixins.ParentedMixin, {
init: function () {
instanceNumber++;
return this._super.apply(this, arguments);
},
destroy: function () {
if (!this.isDestroyed()) {
instanceNumber--;
}
return this._super.apply(this, arguments);
}
return destroyWidget.apply(this, arguments);
};
});
var params = {
View: FormView,
@@ -6413,8 +6413,7 @@ QUnit.module('Views', {
form.destroy();
mixins.ParentedMixin.init = initWidget;
mixins.ParentedMixin.destroy = destroyWidget;
testUtils.unpatch(mixins.ParentedMixin);
});
});
+12 -13
View File
@@ -2490,18 +2490,18 @@ QUnit.module('Views', {
assert.expect(1);
var instanceNumber = 0;
var initWidget = mixins.ParentedMixin.init;
mixins.ParentedMixin.init = function () {
instanceNumber++;
return initWidget.apply(this, arguments);
};
var destroyWidget = mixins.ParentedMixin.destroy;
mixins.ParentedMixin.destroy = function () {
if (!this.isDestroyed()) {
instanceNumber--;
testUtils.patch(mixins.ParentedMixin, {
init: function () {
instanceNumber++;
return this._super.apply(this, arguments);
},
destroy: function () {
if (!this.isDestroyed()) {
instanceNumber--;
}
return this._super.apply(this, arguments);
}
return destroyWidget.apply(this, arguments);
};
});
var params = {
View: KanbanView,
@@ -2538,8 +2538,7 @@ QUnit.module('Views', {
kanban.destroy();
mixins.ParentedMixin.init = initWidget;
mixins.ParentedMixin.destroy = destroyWidget;
testUtils.unpatch(mixins.ParentedMixin);
});
});
+12 -13
View File
@@ -3251,18 +3251,18 @@ QUnit.module('Views', {
assert.expect(1);
var instanceNumber = 0;
var initWidget = mixins.ParentedMixin.init;
mixins.ParentedMixin.init = function () {
instanceNumber++;
return initWidget.apply(this, arguments);
};
var destroyWidget = mixins.ParentedMixin.destroy;
mixins.ParentedMixin.destroy = function () {
if (!this.isDestroyed()) {
instanceNumber--;
testUtils.patch(mixins.ParentedMixin, {
init: function () {
instanceNumber++;
return this._super.apply(this, arguments);
},
destroy: function () {
if (!this.isDestroyed()) {
instanceNumber--;
}
return this._super.apply(this, arguments);
}
return destroyWidget.apply(this, arguments);
};
});
var params = {
View: ListView,
@@ -3299,8 +3299,7 @@ QUnit.module('Views', {
list.destroy();
mixins.ParentedMixin.init = initWidget;
mixins.ParentedMixin.destroy = destroyWidget;
testUtils.unpatch(mixins.ParentedMixin);
});
});