summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristophe Matthieu <chm@odoo.com>2018-02-08 14:20:00 +0100
committerChristophe Matthieu <chm@odoo.com>2018-02-08 15:46:41 +0100
commit715e602bf04ae5a0c9704c599f2145b2808b31b7 (patch)
tree1346333ed1a32b23b525136539ba37d87640c729
parent3b2127c690959e1e43063b09472028a69a62fcb2 (diff)
[IMP] web: use patch for object in tests to avoid edge effects
-rw-r--r--addons/web/static/tests/helpers/test_utils.js86
-rw-r--r--addons/web/static/tests/views/calendar_tests.js25
-rw-r--r--addons/web/static/tests/views/form_tests.js25
-rw-r--r--addons/web/static/tests/views/kanban_tests.js25
-rw-r--r--addons/web/static/tests/views/list_tests.js25
5 files changed, 109 insertions, 77 deletions
diff --git a/addons/web/static/tests/helpers/test_utils.js b/addons/web/static/tests/helpers/test_utils.js
index fec53fc8ded..6fa75abc27f 100644
--- a/addons/web/static/tests/helpers/test_utils.js
+++ b/addons/web/static/tests/helpers/test_utils.js
@@ -559,47 +559,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
diff --git a/addons/web/static/tests/views/calendar_tests.js b/addons/web/static/tests/views/calendar_tests.js
index df8495b1a18..6869c1bedf2 100644
--- a/addons/web/static/tests/views/calendar_tests.js
+++ b/addons/web/static/tests/views/calendar_tests.js
@@ -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);
});
});
diff --git a/addons/web/static/tests/views/form_tests.js b/addons/web/static/tests/views/form_tests.js
index a9efb9d59f9..89c5a3d6a1d 100644
--- a/addons/web/static/tests/views/form_tests.js
+++ b/addons/web/static/tests/views/form_tests.js
@@ -6344,18 +6344,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,
@@ -6415,8 +6415,7 @@ QUnit.module('Views', {
form.destroy();
- mixins.ParentedMixin.init = initWidget;
- mixins.ParentedMixin.destroy = destroyWidget;
+ testUtils.unpatch(mixins.ParentedMixin);
});
});
diff --git a/addons/web/static/tests/views/kanban_tests.js b/addons/web/static/tests/views/kanban_tests.js
index ed697c32712..00625e1e30c 100644
--- a/addons/web/static/tests/views/kanban_tests.js
+++ b/addons/web/static/tests/views/kanban_tests.js
@@ -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);
});
});
diff --git a/addons/web/static/tests/views/list_tests.js b/addons/web/static/tests/views/list_tests.js
index 115140bb162..4a64e17e881 100644
--- a/addons/web/static/tests/views/list_tests.js
+++ b/addons/web/static/tests/views/list_tests.js
@@ -3229,18 +3229,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,
@@ -3277,8 +3277,7 @@ QUnit.module('Views', {
list.destroy();
- mixins.ParentedMixin.init = initWidget;
- mixins.ParentedMixin.destroy = destroyWidget;
+ testUtils.unpatch(mixins.ParentedMixin);
});
});