diff options
| author | lul-odoo <lul@odoo.com> | 2019-06-27 10:23:17 +0200 |
|---|---|---|
| committer | Yannick Tivisse <yti@odoo.com> | 2019-06-27 10:24:40 +0200 |
| commit | 1c99c251c9a1e7e6642efae302ac3b83e18e0d9b (patch) | |
| tree | 3e586218dc03e7143cb0cf2ef816533f2f593646 /addons/web/static/src | |
| parent | 7ea21adabe007eff6b5b69aa451d756dff54442b (diff) | |
[IMP] web: Add QUnit tests for the SwitchCompanyMenu
Purpose
=======
This commit tests some behaviors with multi company
mode after changes brought by f847a46.
Specification
=============
Ensure the robustness of the switch company menu for
multiple cases like:
- Bar rendering
- Switch to another company in mono/multi environment
- Toggle another company in mono/multi environment
- Remove a company in mono/multi environment
The widget itself has been slighty modified to make
it more testable:
- Reading from session is now in the `willStart` function
instead of `init` to allow mocking the session (in tests,
`init` is executed before the mock is applied).
- In the event handlers, don't use `bbq` to retrieve
current allowed company ids. `bbq` is not easily nor
cleanly patched.
- When reading allowed company ids from session, cast
ids from string to integer. They are everywhere casted
except there.
and some linting
Diffstat (limited to 'addons/web/static/src')
| -rw-r--r-- | addons/web/static/src/js/widgets/switch_company_menu.js | 33 | ||||
| -rw-r--r-- | addons/web/static/src/xml/base.xml | 8 |
2 files changed, 21 insertions, 20 deletions
diff --git a/addons/web/static/src/js/widgets/switch_company_menu.js b/addons/web/static/src/js/widgets/switch_company_menu.js index 62e2a27f3d0..75bc918768b 100644 --- a/addons/web/static/src/js/widgets/switch_company_menu.js +++ b/addons/web/static/src/js/widgets/switch_company_menu.js @@ -25,21 +25,25 @@ var SwitchCompanyMenu = Widget.extend({ * @override */ init: function () { - var self = this; this._super.apply(this, arguments); this.isMobile = config.device.isMobile; this._onSwitchCompanyClick = _.debounce(this._onSwitchCompanyClick, 1500, true); - this.allowed_company_ids = String(session.user_context.allowed_company_ids).split(','); - this.user_companies = session.user_companies.allowed_companies; + }, - var hash = $.bbq.getState() - if (!hash.cids || hash.cids === undefined) { - hash.cids = String(session.user_companies.current_company[0]); - } - this.current_company = parseInt(hash.cids.split(',')[0]); + /** + * @override + */ + willStart: function () { + var self = this; + this.allowed_company_ids = String(session.user_context.allowed_company_ids) + .split(',') + .map(function (id) {return parseInt(id);}); + this.user_companies = session.user_companies.allowed_companies; + this.current_company = this.allowed_company_ids[0]; this.current_company_name = _.find(session.user_companies.allowed_companies, function (company) { return company[0] === self.current_company; })[1]; + return this._super.apply(this, arguments); }, //-------------------------------------------------------------------------- @@ -52,11 +56,10 @@ var SwitchCompanyMenu = Widget.extend({ */ _onSwitchCompanyClick: function (ev) { ev.stopPropagation(); - var dropdownItem = $(ev.currentTarget).parent() - var dropdownMenu = dropdownItem.parent() + var dropdownItem = $(ev.currentTarget).parent(); + var dropdownMenu = dropdownItem.parent(); var companyID = dropdownItem.data('company-id'); - var hash = $.bbq.getState() - var allowed_company_ids = _.map(hash.cids.split(','), function(company_id) {return parseInt(company_id);}); + var allowed_company_ids = this.allowed_company_ids; if (dropdownItem.find('.fa-square-o').length) { // 1 enabled company: Stay in single company mode if (this.allowed_company_ids.length === 1) { @@ -81,11 +84,9 @@ var SwitchCompanyMenu = Widget.extend({ */ _onToggleCompanyClick: function (ev) { ev.stopPropagation(); - var dropdownItem = $(ev.currentTarget).parent() - var dropdownMenu = dropdownItem.parent() + var dropdownItem = $(ev.currentTarget).parent(); var companyID = dropdownItem.data('company-id'); - var hash = $.bbq.getState() - var allowed_company_ids = _.map(hash.cids.split(','), function(company_id) {return parseInt(company_id);}); + var allowed_company_ids = this.allowed_company_ids; var current_company_id = allowed_company_ids[0]; if (dropdownItem.find('.fa-square-o').length) { allowed_company_ids.push(companyID); diff --git a/addons/web/static/src/xml/base.xml b/addons/web/static/src/xml/base.xml index e34d017b1a4..878a9cfaef5 100644 --- a/addons/web/static/src/xml/base.xml +++ b/addons/web/static/src/xml/base.xml @@ -1469,21 +1469,21 @@ <div role="menuitem" href="#" class="dropdown-item d-flex py-0 px-0" data-menu="company" t-att-data-company-id="company[0]"> <div class="ml-auto pl-3 pr-3 border border-top-0 border-left-0 border-bottom-0 toggle_company o_py"> <span style="height: 2rem;"> - <t t-if="widget.allowed_company_ids.includes(company[0].toString())"> + <t t-if="widget.allowed_company_ids.includes(company[0])"> <i class="fa fa-fw fa-check-square pt-2"></i> </t> - <t t-if="!widget.allowed_company_ids.includes(company[0].toString())"> + <t t-if="!widget.allowed_company_ids.includes(company[0])"> <i class="fa fa-fw fa-square-o pt-2"></i> </t> </span> </div> <div class="d-flex flex-grow-1 align-items-center py-0 log_into pl-3 o_py" t-att-style="company[0] === widget.current_company ? 'background-color: lightgrey;' : ''"> - <t t-if="widget.allowed_company_ids.includes(company[0].toString())"> + <t t-if="widget.allowed_company_ids.includes(company[0])"> <span class='mr-3 company_label'> <t t-esc="company[1]"/> </span> </t> - <t t-if="!widget.allowed_company_ids.includes(company[0].toString())"> + <t t-if="!widget.allowed_company_ids.includes(company[0])"> <span class='mr-3 company_label text-muted'> <t t-esc="company[1]"/> </span> |
