diff options
| author | Xavier Morel <xmo@openerp.com> | 2012-07-04 13:24:06 +0200 |
|---|---|---|
| committer | Xavier Morel <xmo@openerp.com> | 2012-07-04 13:24:06 +0200 |
| commit | dda83dc1ea343e0f37bb037df773706d4c1375a2 (patch) | |
| tree | 5e1500ac80e3e1087f349a23b4bcd93ee1d5aed4 /addons/web/static/test/testing.js | |
| parent | 039cf85e32c5445981d5706c8efd14c73a8619d3 (diff) | |
[IMP] extract some testing primitives to a dedicated module, migrate search view tests to those
bzr revid: xmo@openerp.com-20120704112406-1lqwahtdo7fnla8o
Diffstat (limited to 'addons/web/static/test/testing.js')
| -rw-r--r-- | addons/web/static/test/testing.js | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/addons/web/static/test/testing.js b/addons/web/static/test/testing.js new file mode 100644 index 00000000000..5b6e27700ab --- /dev/null +++ b/addons/web/static/test/testing.js @@ -0,0 +1,92 @@ +// Test support structures and methods for OpenERP +openerp.testing = (function () { + var xhr = QWeb2.Engine.prototype.get_xhr(); + xhr.open('GET', '/web/static/src/xml/base.xml', false); + xhr.send(null); + var doc = xhr.responseXML; + + var dependencies = { + corelib: [], + coresetup: ['corelib'], + data: ['corelib', 'coresetup'], + dates: [], + formats: ['coresetup', 'dates'], + chrome: ['corelib', 'coresetup'], + views: ['corelib', 'coresetup', 'data', 'chrome'], + search: ['data', 'coresetup', 'formats'], + list: ['views', 'data'], + form: ['data', 'views', 'list', 'formats'], + list_editable: ['list', 'form', 'data'], + }; + + return { + /** + * Function which does not do anything + */ + noop: function () { }, + /** + * Loads 'base.xml' template file into qweb for the provided instance + * + * @param instance openerp instance being initialized, to load the template file in + */ + loadTemplate: function (instance) { + instance.web.qweb.add_template(doc); + }, + /** + * Alter provided instance's ``connection`` attribute to make response + * mockable: + * + * * The ``responses`` parameter can be used to provide a map of (RPC) + * paths (e.g. ``/web/view/load``) to a function returning a response + * to the query. + * * ``instance,connection`` grows a ``responses`` attribute which is + * a map of the same (and is in fact initialized to the ``responses`` + * parameter if one is provided) + * + * Note that RPC requests to un-mocked URLs will be rejected with an + * error message: only explicitly specified urls will get a response. + * + * Mocked connections will *never* perform an actual RPC connection. + * + * @param instance openerp instance being initialized + * @param {Object} [responses] + */ + mockifyRPC: function (instance, responses) { + var connection = instance.connection; + connection.responses = responses || {}; + connection.rpc_function = function (url, payload) { + if (!(url.url in this.responses)) { + return $.Deferred().reject({}, 'failed', + _.str.sprintf("Url %s not found in mock responses", url.url)).promise(); + } + return $.when(this.responses[url.url](payload)); + }; + }, + /** + * Creates an openerp web instance loading the specified module after + * all of its dependencies. + * + * @param {String} module + * @returns OpenERP Web instance + */ + instanceFor: function (module) { + var instance = openerp.init([]); + this._load(instance, module); + return instance; + }, + _load: function (instance, module, loaded) { + if (!loaded) { loaded = []; } + + var deps = dependencies[module]; + if (!deps) { throw new Error("Unknown dependencies for " + module); } + + var to_load = _.difference(deps, loaded); + while (!_.isEmpty(to_load)) { + this._load(instance, to_load[0], loaded); + to_load = _.difference(deps, loaded); + } + openerp.web[module](instance); + loaded.push(module); + } + } +})(); |
