From 06b8af3212e2debb7e364e0b429ec6c2db5214e8 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Tue, 11 Aug 2020 08:36:20 +0000 Subject: [REF] web,purchase: SampleServer: mock registry This commit changes the API of the SampleServer's mock registry s.t. one can register a mocked version of a method, whatever the model. The former API required a specific model to be specified, and only applied on this model. However, in order to enable the sample data feature on the cohort view, the 'get_cohort_data' method needs to be mocked, whatever the concerned model. One can now register mocked methods for a specific model only (with key 'model/method') or for every models (with key 'method'). Task ID: 2282196 --- addons/web/static/src/js/views/sample_server.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'addons/web/static/src/js/views/sample_server.js') diff --git a/addons/web/static/src/js/views/sample_server.js b/addons/web/static/src/js/views/sample_server.js index d5732aa979a..d716a565b32 100644 --- a/addons/web/static/src/js/views/sample_server.js +++ b/addons/web/static/src/js/views/sample_server.js @@ -116,8 +116,12 @@ odoo.define('web.SampleServer', function (require) { case 'read': return this._mockRead(params); } - const mock = SampleServer.mockRegistry.get(params.model); - const mockFunction = mock && mock[params.method || params.route]; + // this rpc can't be mocked by the SampleServer itself, so check if there is an handler + // in the mockRegistry: either specific for this model (with key 'model/method'), or + // global (with key 'method') + const method = params.method || params.route; + const mockFunction = SampleServer.mockRegistry.get(`${params.model}/${method}`) || + SampleServer.mockRegistry.get(method); if (mockFunction) { return mockFunction.call(this, params); } @@ -652,12 +656,11 @@ odoo.define('web.SampleServer', function (require) { SampleServer.UnimplementedRouteError = UnimplementedRouteError; - // mockRegistry allows to register mock version of methods or routes for - // specific models, for instance - // SampleServer.mockRegistry.registry('res.partner' { - // some_method: () => 23, - // some_route: () => "abcd", - // }); + // mockRegistry allows to register mock version of methods or routes, + // for all models: + // SampleServer.mockRegistry.add('some_route', () => "abcd"); + // for a specific model (e.g. 'res.partner'): + // SampleServer.mockRegistry.add('res.partner/some_method', () => 23); SampleServer.mockRegistry = new Registry(); return SampleServer; -- cgit v1.2.3