Files
odoo_source/addons/web/static/test/chrome/search_tests.js
T
Géry Debongnie 905e01921f [REF] web, *: redesign all JS views
This commit introduce a full redesign of all JS views.  We started
basically from scratch.  The goal was to unify all the various views
under a common framework, to make them testable, to make then usable in
different conditions (in studio, or in the frontend), and to make our
lives easier.

Some important points are:
- we introduced new coding guidelines (camelCase, 80 chars width, ...)
- we have a brand new testing framework (still QUnit based)
- kanban view moved to the web addon
- calendar view (formerly web_calender) moved to web as well
- the tree view was removed
- all new code should be documented

We hope that this code is the start of a new era for the Odoo web
client, we want to have a high quality codebase, well documented, well
tested, well designed.

Work done by the framework team: mostly aab, ged, chm, dmo, qsm
2017-04-11 19:44:38 +02:00

172 lines
6.6 KiB
JavaScript

odoo.define('web.search_tests', function (require) {
"use strict";
var SearchView = require('web.SearchView');
QUnit.module('chrome', {}, function () {
QUnit.module('Search View');
QUnit.test('Adding a facet to the query creates a facet and a value', function (assert) {
assert.expect(3);
var query = new SearchView.SearchQuery();
var field = {};
query.add({
category: 'Foo',
field: field,
values: [{label: 'Value', value: 3}]
});
var facet = query.at(0);
assert.strictEqual(facet.get('category'), 'Foo');
assert.strictEqual(facet.get('field'), field);
assert.deepEqual(facet.get('values'), [{label: 'Value', value: 3}]);
});
QUnit.test('Adding two facets', function (assert) {
assert.expect(3);
var query = new SearchView.SearchQuery();
query.add([
{ category: 'Foo', field: {}, values: [{label: 'Value', value: 3}] },
{ category: 'Bar', field: {}, values: [{label: 'Value 2', value: 4}] }
]);
assert.strictEqual(query.length, 2);
assert.strictEqual(query.at(0).values.length, 1);
assert.strictEqual(query.at(1).values.length, 1);
});
QUnit.test('If a facet already exists, add values to it', function (assert) {
assert.expect(2);
var query = new SearchView.SearchQuery();
var field = {};
query.add({category: 'A', field: field, values: [{label: 'V1', value: 0}]});
query.add({category: 'A', field: field, values: [{label: 'V2', value: 1}]});
assert.strictEqual(query.length, 1, "adding an existing facet should merge new values into old facet");
var facet = query.at(0);
assert.deepEqual(facet.get('values'), [
{label: 'V1', value: 0},
{label: 'V2', value: 1}
]);
});
QUnit.test('Facet being implicitly changed should trigger change, not add', function (assert) {
assert.expect(2);
var query = new SearchView.SearchQuery();
var field = {}, added = false, changed = false;
query.add({category: 'A', field: field, values: [{label: 'V1', value: 0}]});
query.on('add', function () { added = true; })
.on('change', function () { changed = true; });
query.add({category: 'A', field: field, values: [{label: 'V2', value: 1}]});
assert.ok(!added, "query.add adding values to a facet should not trigger an add");
assert.ok(changed, "query.add adding values to a facet should not trigger a change");
});
QUnit.test('Toggling a facet, value which does not exist should add it', function (assert) {
assert.expect(3);
var query = new SearchView.SearchQuery();
var field = {};
query.toggle({category: 'A', field: field, values: [{label: 'V1', value: 0}]});
assert.strictEqual(query.length, 1, "Should have created a single facet");
var facet = query.at(0);
assert.strictEqual(facet.values.length, 1, "Facet should have a single value");
assert.deepEqual(facet.get('values'), [{label: 'V1', value: 0}],
"Facet's value should match input");
});
QUnit.test('Toggling a facet which exists with a value which does not should add the value to the facet', function (assert) {
assert.expect(3);
var field = {};
var query = new SearchView.SearchQuery();
query.add({category: 'A', field: field, values: [{label: 'V1', value: 0}]});
query.toggle({category: 'A', field: field, values: [{label: 'V2', value: 1}]});
assert.strictEqual(query.length, 1, "Should have edited the existing facet");
var facet = query.at(0);
assert.strictEqual(facet.values.length, 2, "Should have added the value to the existing facet");
assert.deepEqual(facet.get('values'), [
{label: 'V1', value: 0},
{label: 'V2', value: 1}
]);
});
QUnit.test('Toggling a facet which exists with a value which does as well should remove the value from the facet', function (assert) {
assert.expect(3);
var field = {};
var query = new SearchView.SearchQuery();
query.add({category: 'A', field: field, values: [{label: 'V1', value: 0}]});
query.add({category: 'A', field: field, values: [{label: 'V2', value: 1}]});
query.toggle({category: 'A', field: field, values: [{label: 'V2', value: 1}]});
assert.strictEqual(query.length, 1, 'Should have the same single facet');
var facet = query.at(0);
assert.strictEqual(facet.values.length, 1, "Should only have one value left in the facet");
assert.deepEqual(facet.get('values'), [
{label: 'V1', value: 0}
]);
});
QUnit.test('Toggling off the last value of a facet should remove the facet', function (assert) {
assert.expect(1);
var field = {};
var query = new SearchView.SearchQuery();
query.add({category: 'A', field: field, values: [{label: 'V1', value: 0}]});
query.toggle({category: 'A', field: field, values: [{label: 'V1', value: 0}]});
assert.strictEqual(query.length, 0, 'Should have removed the facet');
});
QUnit.test('Intermediate emptiness should not remove the facet', function (assert) {
assert.expect(3);
var field = {};
var query = new SearchView.SearchQuery();
query.add({category: 'A', field: field, values: [{label: 'V1', value: 0}]});
query.toggle({category: 'A', field: field, values: [
{label: 'V1', value: 0},
{label: 'V2', value: 1}
]});
assert.strictEqual(query.length, 1, 'Should not have removed the facet');
var facet = query.at(0);
assert.strictEqual(facet.values.length, 1, "Should have one value");
assert.deepEqual(facet.get('values'), [
{label: 'V2', value: 1}
]);
});
QUnit.test('Reseting with multiple facets should still work to load defaults', function (assert) {
assert.expect(3);
var query = new SearchView.SearchQuery();
var field = {};
query.reset([
{category: 'A', field: field, values: [{label: 'V1', value: 0}]},
{category: 'A', field: field, values: [{label: 'V2', value: 1}]}]);
assert.strictEqual(query.length, 1, 'Should have created a single facet');
assert.strictEqual(query.at(0).values.length, 2, 'the facet should have merged two values');
assert.deepEqual(query.at(0).get('values'), [
{label: 'V1', value: 0},
{label: 'V2', value: 1}
]);
});
});
});