Files
odoo_source/addons/web/static/tests/widgets/dropdown_menu_tests.js
T
Géry Debongnie abf32b8b21 [REF] *: update js test suite to use helpers
This rev. introduces robust helpers to use in the JS tests suite to
interact with DOM and components, and starts using them (almost)
everywhere.

All the helpers are exposed though testUtils.js.

There are 2 kinds of helpers:

1. Assertions
-------------
 * assert.containsNone, containsN, containsOnce check that the DOM
   (or a specific part of the DOM) contains a `selector`. It
   generates a correct error message automatically.
    ex: assert.strictEqual(form.$('.o_form_editable'), 1, "msg");
        -> assert.containsOnce(form, '.o_form_editable');
 * assert.isVisible, isNotVisible check that the DOM has an element
   visible or not. They also check that the element is actually in
   the DOM (before most tests didn't verify this).
 * assert.hasClass, doesNotHaveClass, hasAttrVAlue, check specific
   properties of a DOM element, and also validate that it is
   applied on a single existing DOM element (before most tests
   didn't verify this).
    ex: assert.notOk(form.$('button').hasClass('btn-primary'));
       -> assert.doesNotHaveClass(form.$('button'), 'btn-primary');

2. Utilities
------------
The goal of the utilities is to centralize the definition of many
standard components and interactions, ensuring that when we
refactor the JS framework, we do not need to change all the tests.

Existing mock utilities (addMockEnvironment, intercept, path,
patchDate, unpatch and fieldsViewGet) are moved to
'testUtils.mock.*'.
Existing DOM utilities are moved to 'testUtils.dom.*'.
New dom utilities are created for opendDatePicker, click,
clickFirst and clickLast. Helper `click` verifies that there is
exactly 1 element visible in the DOM you click on, `clickFirst`
and `clickLast` verify that there are more than one element on the
DOM.
 ex: form.$('button').click();
     -> testUtils.dom.click(form.$('button'));

New Form utilities: (testUtils.form.*)
 clickEdit, clickSave, clickCreate, clickDiscard, all clicks on
 the control panel buttons of the form.
 `reload` reloads the form data.

New modal, graph, kanban and pivot utils (testUtils.pivot.*,
testUtils.kanban.*, etc.).

New fields utils: (testUtils.fields.*)
 * editInput, editSelect: allow to change the value of a field,
   using a selector to identify it. They validate that the input
   exists and trigger the change event automatically.
 * editAndTrigger: allow to modify a field and trigger specific
   events after the value change
 * many2one (testUtils.fields.many2one.*)
   clickOpenDropdown, clickHighlightedItem, clickItem,
   searchAndClickItem: use a field name instead of a selector and
   do all the complex mechanism to open, filter and highlight
   many2one fields.

Joint work with aab, dam, ged, mge, svs and vsc.
2018-11-19 11:24:28 +00:00

207 lines
9.3 KiB
JavaScript

odoo.define('web.dropdown_menu_tests', function (require) {
"use strict";
var DropdownMenu = require('web.DropdownMenu');
var testUtils = require('web.test_utils');
function createDropdownMenu(dropdownTitle, groups, params) {
params = params || {};
var target = params.debug ? document.body : $('#qunit-fixture');
var menu = new DropdownMenu(null, dropdownTitle, groups);
testUtils.mock.addMockEnvironment(menu, params);
menu.appendTo(target);
return menu;
}
QUnit.module('Web', {
beforeEach: function () {
this.items = [
{
isActive: false,
isOpen: false,
description: 'Some Item',
itemId: 1,
groupId: 1,
},
{
isActive: true,
description: 'Some other Item',
itemId: 2,
options: [],
isRemovable: true,
groupId: 2,
},
];
this.dropdownHeader = {
title: "Menu",
icon: "fa fa-bars",
};
},
}, function () {
QUnit.module('DropdownMenu');
QUnit.test('simple rendering', function (assert) {
assert.expect(2);
var dropdownMenu = createDropdownMenu(this.dropdownHeader, this.items);
assert.containsN(dropdownMenu, '.dropdown-divider, .dropdown-item, .dropdown-item-text', 4, 'should have 4 elements counting the dividers');
assert.strictEqual(dropdownMenu.$('.dropdown-divider, .dropdown-item, .dropdown-item-text').eq(1).text().trim(), 'Some Item',
'first element should have "Some Item" description');
dropdownMenu.destroy();
});
QUnit.test('click on an item should toggle item', function (assert) {
assert.expect(9);
var eventNumber = 0;
var dropdownMenu = createDropdownMenu(this.dropdownHeader, this.items, {
intercepts: {
menu_item_toggled: function (ev) {
eventNumber++;
assert.strictEqual(ev.data.itemId, 1);
if (eventNumber === 1) {
assert.strictEqual(ev.data.isActive, true);
} else {
assert.strictEqual(ev.data.isActive, false);
}
},
},
});
testUtils.dom.click(dropdownMenu.$('button:first'));
assert.doesNotHaveClass(dropdownMenu.$('.o_menu_item:first > .dropdown-item'), 'selected');
testUtils.dom.click(dropdownMenu.$('.o_menu_item a').first());
assert.hasClass(dropdownMenu.$('.o_menu_item:first > .dropdown-item'), 'selected');
assert.ok(dropdownMenu.$('.o_menu_item:first').is(':visible'),
'item should still be visible');
testUtils.dom.click(dropdownMenu.$('.o_menu_item a').first());
assert.doesNotHaveClass(dropdownMenu.$('.o_menu_item:first > .dropdown-item'), 'selected');
assert.ok(dropdownMenu.$('.o_menu_item:first').is(':visible'),
'item should still be visible');
dropdownMenu.destroy();
});
QUnit.test('click on an item should not change url', function (assert) {
assert.expect(0);
var dropdownMenu = createDropdownMenu(this.dropdownHeader, this.items);
testUtils.dom.click(dropdownMenu.$('.o_dropdown_toggler_btn'));
dropdownMenu.$el.click(function () {
// we do not want a click to get out and change the url, for example
throw new Error('No click should get out of the dropdown menu');
});
testUtils.dom.click(dropdownMenu.$('.o_menu_item a').first());
dropdownMenu.destroy();
});
QUnit.test('options rendering', function (assert) {
assert.expect(3);
this.items[0].options = [{optionId: 1, description: "First Option", groupId: 1}, {optionId: 2, description: "Second Option", groupId: 1}];
var dropdownMenu = createDropdownMenu(this.dropdownHeader, this.items);
// open dropdown
testUtils.dom.click(dropdownMenu.$('button:first'));
assert.containsN(dropdownMenu, '.dropdown-divider, .dropdown-item, .dropdown-item-text', 4);
// open options menu
testUtils.dom.click(dropdownMenu.$('span.fa-caret-right'));
assert.containsN(dropdownMenu, '.dropdown-divider, .dropdown-item, .dropdown-item-text', 7);
// close options menu
testUtils.dom.click(dropdownMenu.$('span.fa-caret-down'));
assert.containsN(dropdownMenu, '.dropdown-divider, .dropdown-item, .dropdown-item-text', 4);
dropdownMenu.destroy();
});
QUnit.test('close menu closes also submenu', function (assert) {
assert.expect(2);
this.items[0].options = [{optionId: 1, description: "First Option"}, {optionId: 2, description: "Second Option"}];
var dropdownMenu = createDropdownMenu(this.dropdownHeader, this.items);
testUtils.dom.click(dropdownMenu.$('button:first'));
// open options menu
testUtils.dom.click(dropdownMenu.$('span.fa-caret-right'));
assert.containsN(dropdownMenu, '.dropdown-divider, .dropdown-item, .dropdown-item-text', 7);
testUtils.dom.click(dropdownMenu.$('button:first'));
testUtils.dom.click(dropdownMenu.$('button:first'));
assert.containsN(dropdownMenu, '.dropdown-divider, .dropdown-item, .dropdown-item-text', 4);
dropdownMenu.destroy();
});
QUnit.test('click on an option should toggle options and item states properly', function (assert) {
assert.expect(22);
this.items[0].options = [{optionId: 1, description: "First Option"}, {optionId: 2, description: "Second Option"}];
var eventNumber = 0;
var dropdownMenu = createDropdownMenu(this.dropdownHeader, this.items, {
intercepts: {
menu_item_toggled: function (ev) {
eventNumber++;
if (eventNumber === 1) {
assert.strictEqual(ev.data.itemId, 1);
assert.strictEqual(ev.data.isActive, true);
assert.strictEqual(ev.data.optionId, 1);
} else {
assert.strictEqual(ev.data.itemId, 1);
assert.strictEqual(ev.data.isActive, false);
assert.strictEqual(ev.data.optionId, false);
}
},
item_option_changed: function (ev) {
if (eventNumber === 1) {
assert.strictEqual(ev.data.itemId, 1);
assert.strictEqual(ev.data.isActive, true);
assert.strictEqual(ev.data.optionId, 2);
}
},
},
});
// open dropdown menu
testUtils.dom.click(dropdownMenu.$('button:first'));
// open options menu
testUtils.dom.click(dropdownMenu.$('span.fa-caret-right'));
assert.containsN(dropdownMenu, '.dropdown-divider, .dropdown-item, .dropdown-item-text', 7);
// Don't forget there is a hidden li.divider element at first place among children
assert.doesNotHaveClass(dropdownMenu.$('.o_menu_item:nth-child(2) > .dropdown-item'), 'selected');
assert.doesNotHaveClass(dropdownMenu.$('.o_item_option:nth-child(2) > .dropdown-item'), 'selected');
assert.doesNotHaveClass(dropdownMenu.$('.o_item_option:nth-child(3) > .dropdown-item'), 'selected');
testUtils.dom.click(dropdownMenu.$('.o_item_option:first'));
assert.hasClass(dropdownMenu.$('.o_menu_item:nth-child(2) > .dropdown-item'), 'selected');
assert.hasClass(dropdownMenu.$('.o_item_option:nth-child(2) > .dropdown-item'), 'selected');
assert.doesNotHaveClass(dropdownMenu.$('.o_item_option:nth-child(3) > .dropdown-item'), 'selected');
testUtils.dom.click(dropdownMenu.$('.o_item_option:nth-child(3)'));
assert.hasClass(dropdownMenu.$('.o_menu_item:nth-child(2) > .dropdown-item'), 'selected');
assert.doesNotHaveClass(dropdownMenu.$('.o_item_option:nth-child(2) > .dropdown-item'), 'selected');
assert.hasClass(dropdownMenu.$('.o_item_option:nth-child(3) > .dropdown-item'), 'selected');
testUtils.dom.click(dropdownMenu.$('.o_item_option:nth-child(3)'));
assert.doesNotHaveClass(dropdownMenu.$('.o_menu_item:nth-child(2) > .dropdown-item'), 'selected');
assert.doesNotHaveClass(dropdownMenu.$('.o_item_option:nth-child(2) > .dropdown-item'), 'selected');
assert.doesNotHaveClass(dropdownMenu.$('.o_item_option:nth-child(3) > .dropdown-item'), 'selected');
dropdownMenu.destroy();
});
QUnit.test('trash an item should be possible', function (assert) {
assert.expect(3);
var dropdownMenu = createDropdownMenu(this.dropdownHeader, this.items, {
intercepts: {
menu_item_deleted: function (ev) {
assert.strictEqual(ev.data.itemId, 2);
},
},
});
testUtils.dom.click(dropdownMenu.$('button:first'));
assert.containsN(dropdownMenu, '.dropdown-divider, .dropdown-item, .dropdown-item-text', 4);
testUtils.dom.click(dropdownMenu.$('span.o_trash_button'));
assert.containsN(dropdownMenu, '.dropdown-divider, .dropdown-item, .dropdown-item-text', 2);
dropdownMenu.destroy();
});
});
});