Files
odoo_source/addons/mail/static/tests/document_thread_window_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

277 lines
10 KiB
JavaScript

odoo.define('mail.documentThreadWindowTests', function (require) {
"use strict";
var mailTestUtils = require('mail.testUtils');
var MessagingMenu = require('mail.systray.MessagingMenu');
var testUtils = require('web.test_utils');
QUnit.module('mail', {}, function () {
QUnit.module('Thread Window', {}, function () {
QUnit.module('Document Thread', {
beforeEach: function () {
var partnerID = 44;
this.data = {
'mail.message': {
fields: {
body: {
string: "Contents",
type: 'html',
},
author_id: {
string: "Author",
type: 'many2one',
relation: 'res.partner',
},
channel_ids: {
string: "Channels",
type: 'many2many',
relation: 'mail.channel',
},
starred: {
string: "Starred",
type: 'boolean',
},
needaction: {
string: "Need Action",
type: 'boolean',
},
needaction_partner_ids: {
string: 'Needaction Partner IDs',
type: 'one2many',
relation: 'res.partner',
},
starred_partner_ids: {
string: "partner ids",
type: 'integer',
}
},
records: [{
id: 1,
author_id: [1, 'Me'],
body: '<p>Some Message on a document</p>',
channel_ids: [],
model: 'some.res.model',
record_name: "Some Record",
res_id: 1,
}, {
author_id: [2, "Demo"],
body: "<p>*MessageOnDocument*</p>",
id: 2,
model: 'some.res.model',
needaction: true,
needaction_partner_ids: [partnerID],
record_name: "Some Record",
res_id: 1,
}],
},
'some.res.model': {
fields: {
message_ids: {string: 'Messages', type: 'one2many'},
},
records: [{
id: 1,
message_ids: [1, 2],
}],
},
initMessaging: {
channel_slots: {
channel_channel: [],
},
},
};
this.session = {
partner_id: partnerID, // so that needaction messages are treated as needactions
};
this.services = mailTestUtils.getMailServices();
},
});
QUnit.test('open a document thread in a thread window', function (assert) {
assert.expect(6);
var messagingMenu = new MessagingMenu();
testUtils.mock.addMockEnvironment(messagingMenu, {
services: this.services,
data: this.data,
session: this.session,
mockRPC: function (route, args) {
if (args.method === 'read') {
assert.deepEqual(args.args, [[1], ['message_ids']],
"should read the message_ids on the document");
}
if (args.method === 'message_format') {
assert.deepEqual(args.args[0], [1],
"should only fetch the unknown message");
}
return this._super.apply(this, arguments);
},
});
messagingMenu.appendTo($('#qunit-fixture'));
// toggle the messaging menu and open the documentThread
testUtils.dom.click(messagingMenu.$('.dropdown-toggle'));
assert.containsOnce(messagingMenu, '.o_mail_preview',
"should display one preview");
testUtils.dom.click(messagingMenu.$('.o_mail_preview'));
assert.strictEqual($('.o_thread_window').length, 1,
"should have open the DocumentThread in a thread window");
assert.strictEqual($('.o_thread_window .o_thread_message').length, 2,
"should have fetched the history of the documentThread");
assert.strictEqual($('.o_thread_window_title').text().trim(), 'Some Record',
"should display the display_name of the record in the header");
messagingMenu.destroy();
});
QUnit.test('expand a document thread window', function (assert) {
assert.expect(4);
var messagingMenu = new MessagingMenu();
testUtils.mock.addMockEnvironment(messagingMenu, {
services: this.services,
data: this.data,
session: this.session,
intercepts: {
do_action: function (ev) {
assert.deepEqual(ev.data.action, {
res_id: 1,
res_model: 'some.res.model',
type: 'ir.actions.act_window',
views: [[false, 'form']],
}, "should open the document in form view");
},
},
});
messagingMenu.appendTo($('#qunit-fixture'));
// toggle the messaging menu and open the documentThread
testUtils.dom.click(messagingMenu.$('.dropdown-toggle'));
assert.containsOnce(messagingMenu, '.o_mail_preview',
"should display one preview");
testUtils.dom.click(messagingMenu.$('.o_mail_preview'));
assert.strictEqual($('.o_thread_window').length, 1,
"should have open the DocumentThread in a thread window");
assert.hasAttrValue($('.o_thread_window .o_thread_window_expand'), 'title',
'Open document', "button should have correct title");
// click to expand
testUtils.dom.click($('.o_thread_window .o_thread_window_expand'));
messagingMenu.destroy();
});
QUnit.test('post messages in a document thread window', function (assert) {
assert.expect(8);
var newMessageBody = 'Some message';
var newMessage = {
id: 6783,
author_id: [this.session.partner_id, 'Me'],
body: newMessageBody,
channel_ids: [],
model: 'some.res.model',
record_name: "Some Record",
res_id: 1,
};
var messagingMenu = new MessagingMenu();
testUtils.mock.addMockEnvironment(messagingMenu, {
services: this.services,
data: this.data,
session: this.session,
mockRPC: function (route, args) {
if (args.method === 'message_post') {
assert.strictEqual(args.model, 'some.res.model',
"should post the message on the correct model");
assert.deepEqual(args.args, [1],
"should post the message on the correct record");
assert.strictEqual(args.kwargs.body, newMessageBody,
"should post correct message");
// add the message to the fake DB
this.data['mail.message'].records.push(newMessage);
this.data['some.res.model'].records[0].message_ids.push(6783);
return $.when(6783);
}
return this._super.apply(this, arguments);
},
});
testUtils.mock.intercept(messagingMenu, 'call_service', function (ev) {
if (ev.data.service === 'local_storage' && ev.data.method === 'setItem' &&
ev.data.args[0] === 'mail.document_threads_last_message') {
assert.deepEqual(ev.data.args[1], newMessage,
"should write sent message in local storage, to share info with other tabs");
}
}, true);
messagingMenu.appendTo($('#qunit-fixture'));
// toggle the messaging menu and open the documentThread
testUtils.dom.click(messagingMenu.$('.dropdown-toggle'));
assert.containsOnce(messagingMenu, '.o_mail_preview',
"should display one preview");
testUtils.dom.click(messagingMenu.$('.o_mail_preview'));
assert.strictEqual($('.o_thread_window').length, 1,
"should have open the DocumentThread in a thread window");
assert.strictEqual($('.o_thread_window .o_thread_message').length, 2,
"should display 2 messages in the thread window");
// post a message
$('.o_thread_window .o_composer_text_field')
.val(newMessageBody)
.trigger($.Event('keydown', {which: $.ui.keyCode.ENTER}));
assert.strictEqual($('.o_thread_window .o_thread_message').length, 3,
"should display 3 messages in the thread window");
messagingMenu.destroy();
});
QUnit.test('open, fold, unfold and close a document thread window', function (assert) {
assert.expect(8);
var messagingMenu = new MessagingMenu();
testUtils.mock.addMockEnvironment(messagingMenu, {
services: this.services,
data: this.data,
session: this.session,
});
testUtils.mock.intercept(messagingMenu, 'call_service', function (ev) {
if (ev.data.service === 'local_storage' && ev.data.method === 'setItem') {
assert.step(ev.data.args);
}
}, true);
messagingMenu.appendTo($('#qunit-fixture'));
// toggle the messaging menu and open the documentThread
testUtils.dom.click(messagingMenu.$('.dropdown-toggle'));
assert.containsOnce(messagingMenu, '.o_mail_preview',
"should display one preview");
testUtils.dom.click(messagingMenu.$('.o_mail_preview'));
assert.strictEqual($('.o_thread_window').length, 1,
"should have open the DocumentThread in a thread window");
assert.strictEqual($('.o_thread_window .o_thread_message').length, 2,
"should display 2 messages in the thread window");
// fold and unfold thread window
testUtils.dom.click($('.o_thread_window .o_thread_window_title'));
testUtils.dom.click($('.o_thread_window .o_thread_window_title'));
// close thread window
testUtils.dom.click($('.o_thread_window .o_thread_window_close'));
assert.verifySteps([
['mail.document_threads_state', {"some.res.model_1": {"name": "Some Record", "windowState": "open"}}],
['mail.document_threads_state', {"some.res.model_1": {"name": "Some Record", "windowState": "folded"}}],
['mail.document_threads_state', {"some.res.model_1": {"name": "Some Record", "windowState": "open"}}],
['mail.document_threads_state', {"some.res.model_1": {"name": "Some Record", "windowState": "closed"}}],
]);
messagingMenu.destroy();
});
});
});
});