Files
odoo_source/addons/pad/static/tests/pad_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

267 lines
9.0 KiB
JavaScript

odoo.define('pad.pad_tests', function (require) {
"use strict";
var FieldPad = require('pad.pad');
var FormView = require('web.FormView');
var testUtils = require('web.test_utils');
var createView = testUtils.createView;
QUnit.module('pad widget', {
beforeEach: function () {
this.data = {
task: {
fields: {
description: {string: "Description", type: "char"},
},
records: [
{id: 1, description: false},
{id: 2, description: "https://pad.odoo.pad/p/test-03AK6RCJT"},
],
pad_is_configured: function () {
return true;
},
pad_generate_url: function (route, args) {
return {
url:'https://pad.odoo.pad/p/test/' + args.context.object_id
};
},
pad_get_content: function () {
return "we should rewrite this server in haskell";
},
},
};
},
});
QUnit.test('pad widget display help if server not configured', function (assert) {
assert.expect(4);
var form = createView({
View: FormView,
model: 'task',
data: this.data,
arch:'<form>' +
'<sheet>' +
'<group>' +
'<field name="description" widget="pad"/>' +
'</group>' +
'</sheet>' +
'</form>',
res_id: 1,
mockRPC: function (route, args) {
if (args.method === 'pad_is_configured') {
return $.when(false);
}
return this._super.apply(this, arguments);
},
});
assert.ok(form.$('p.oe_unconfigured').is(':visible'),
"help message should be visible");
assert.notOk(form.$('p.oe_pad_content').is(':visible'),
"content should not be visible");
testUtils.form.clickEdit(form);
assert.ok(form.$('p.oe_unconfigured').is(':visible'),
"help message should be visible");
assert.notOk(form.$('p.oe_pad_content').is(':visible'),
"content should not be visible");
form.destroy();
delete FieldPad.prototype.isPadConfigured;
});
QUnit.test('pad widget works, basic case', function (assert) {
assert.expect(5);
var form = createView({
View: FormView,
model: 'task',
data: this.data,
arch:'<form>' +
'<sheet>' +
'<group>' +
'<field name="description" widget="pad"/>' +
'</group>' +
'</sheet>' +
'</form>',
res_id: 1,
mockRPC: function (route, args) {
if (route === 'https://pad.odoo.pad/p/test/1?showChat=false&userName=batman') {
assert.ok(true, "should have an iframe with correct src");
return $.when(true);
}
return this._super.apply(this, arguments);
},
session: {
userName: "batman",
},
});
assert.notOk(form.$('p.oe_unconfigured').is(':visible'),
"help message should not be visible");
assert.ok(form.$('.oe_pad_content').is(':visible'),
"content should be visible");
assert.strictEqual(form.$('.oe_pad_content:contains(This pad will be)').length, 1,
"content should display a message when not initialized");
testUtils.form.clickEdit(form);
assert.containsOnce(form, '.oe_pad_content iframe',
"should have an iframe");
form.destroy();
delete FieldPad.prototype.isPadConfigured;
});
QUnit.test('pad widget works, with existing data', function (assert) {
assert.expect(3);
var contentDef = $.Deferred();
var form = createView({
View: FormView,
model: 'task',
data: this.data,
arch:'<form>' +
'<sheet>' +
'<group>' +
'<field name="description" widget="pad"/>' +
'</group>' +
'</sheet>' +
'</form>',
res_id: 2,
mockRPC: function (route, args) {
if (_.str.startsWith(route, 'http')) {
return $.when(true);
}
var result = this._super.apply(this, arguments);
if (args.method === 'pad_get_content') {
return contentDef.then(_.constant(result));
}
if (args.method === 'write') {
assert.ok('description' in args.args[1],
"should always send the description value");
}
return result;
},
session: {
userName: "batman",
},
});
assert.strictEqual(form.$('.oe_pad_content').text(), "Loading",
"should display loading message");
contentDef.resolve();
assert.strictEqual(form.$('.oe_pad_content').text(), "we should rewrite this server in haskell",
"should display proper value");
testUtils.form.clickEdit(form);
testUtils.form.clickSave(form);
form.destroy();
delete FieldPad.prototype.isPadConfigured;
});
QUnit.test('pad widget is not considered dirty at creation', function (assert) {
assert.expect(2);
var form = createView({
View: FormView,
model: 'task',
data: this.data,
arch:'<form>' +
'<sheet>' +
'<group>' +
'<field name="description" widget="pad"/>' +
'</group>' +
'</sheet>' +
'</form>',
mockRPC: function (route, args) {
if (!args.method) {
return $.when(true);
}
return this._super.apply(this, arguments);
},
session: {
userName: "batman",
},
});
var def = form.canBeDiscarded();
assert.strictEqual($('.modal').length, 0,
"should have no confirmation modal opened");
assert.strictEqual(def.state(), 'resolved',
"can be discarded was succesfully resolved");
form.destroy();
delete FieldPad.prototype.isPadConfigured;
});
QUnit.test('pad widget is not considered dirty at edition', function (assert) {
assert.expect(2);
var form = createView({
View: FormView,
model: 'task',
data: this.data,
arch:'<form>' +
'<sheet>' +
'<group>' +
'<field name="description" widget="pad"/>' +
'</group>' +
'</sheet>' +
'</form>',
res_id: 2,
mockRPC: function (route, args) {
if (!args.method) {
return $.when(true);
}
return this._super.apply(this, arguments);
},
session: {
userName: "batman",
},
});
testUtils.form.clickEdit(form);
var def = form.canBeDiscarded();
assert.strictEqual($('.modal').length, 0,
"should have no confirmation modal opened");
assert.strictEqual(def.state(), 'resolved',
"can be discarded was succesfully resolved");
form.destroy();
delete FieldPad.prototype.isPadConfigured;
});
QUnit.test('record should be discarded properly even if only pad has changed', function (assert) {
assert.expect(1);
var form = createView({
View: FormView,
model: 'task',
data: this.data,
arch:'<form>' +
'<sheet>' +
'<group>' +
'<field name="description" widget="pad"/>' +
'</group>' +
'</sheet>' +
'</form>',
res_id: 2,
mockRPC: function (route, args) {
if (!args.method) {
return $.when(true);
}
return this._super.apply(this, arguments);
},
session: {
userName: "batman",
},
});
testUtils.form.clickEdit(form);
testUtils.form.clickDiscard(form);
assert.strictEqual(form.$('.oe_pad_readonly').text(), this.data.task.pad_get_content(),
"pad content should not have changed");
form.destroy();
delete FieldPad.prototype.isPadConfigured;
});
});