/
pivot_tests.js
1217 lines (1020 loc) · 44.1 KB
/
pivot_tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
odoo.define('web.pivot_tests', function (require) {
"use strict";
var core = require('web.core');
var PivotView = require('web.PivotView');
var testUtils = require('web.test_utils');
var _t = core._t;
var createView = testUtils.createView;
QUnit.module('Views', {
beforeEach: function () {
this.data = {
partner: {
fields: {
foo: {string: "Foo", type: "integer"},
bar: {string: "bar", type: "boolean"},
date: {string: "Date", type: "date", store: true},
product_id: {string: "Product", type: "many2one", relation: 'product', store: true},
non_stored_m2o: {string: "Non Stored M2O", type: "many2one", relation: 'product'},
customer: {string: "Customer", type: "many2one", relation: 'customer', store: true},
},
records: [
{
id: 1,
foo: 12,
bar: true,
date: '2016-12-14',
product_id: 37,
customer: 1,
}, {
id: 2,
foo: 1,
bar: true,
date: '2016-10-26',
product_id: 41,
customer: 2,
}, {
id: 3,
foo: 17,
bar: true,
date: '2016-12-15',
product_id: 41,
customer: 2,
}, {id: 4,
foo: 2,
bar: false,
date: '2016-04-11',
product_id: 41,
customer: 1,
},
]
},
product: {
fields: {
name: {string: "Product Name", type: "char"}
},
records: [{
id: 37,
display_name: "xphone",
}, {
id: 41,
display_name: "xpad",
}]
},
customer: {
fields: {
name: {string: "Customer Name", type: "char"}
},
records: [{
id: 1,
display_name: "First",
}, {
id: 2,
display_name: "Second",
}]
},
};
}
}, function () {
QUnit.module('PivotView');
QUnit.test('simple pivot rendering', function (assert) {
assert.expect(3);
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot string="Partners">' +
'<field name="foo" type="measure"/>' +
'</pivot>',
mockRPC: function (route, args) {
assert.strictEqual(args.kwargs.lazy, false,
"the read_group should be done with the lazy=false option");
return this._super.apply(this, arguments);
},
});
assert.ok(pivot.$el.hasClass('o_enable_linking'),
"root node should have classname 'o_enable_linking'");
assert.strictEqual(pivot.$('td.o_pivot_cell_value:contains(32)').length, 1,
"should contain a pivot cell with the sum of all records");
pivot.destroy();
});
QUnit.test('pivot rendering with widget', function (assert) {
assert.expect(1);
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot string="Partners">' +
'<field name="foo" type="measure" widget="float_time"/>' +
'</pivot>',
});
assert.strictEqual(pivot.$('td.o_pivot_cell_value:contains(32:00)').length, 1,
"should contain a pivot cell with the sum of all records");
pivot.destroy();
});
QUnit.test('pivot view without "string" attribute', function (assert) {
assert.expect(1);
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot>' +
'<field name="foo" type="measure"/>' +
'</pivot>',
});
// this is important for export functionality.
assert.strictEqual(pivot.title, _t("Untitled"), "should have a valid title");
pivot.destroy();
});
QUnit.test('clicking on a cell triggers a do_action', function (assert) {
assert.expect(2);
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot>' +
'<field name="product_id" type="row"/>' +
'<field name="foo" type="measure"/>' +
'</pivot>',
intercepts: {
do_action: function (ev) {
assert.deepEqual(ev.data.action, {
context: {someKey: true, userContextKey: true},
domain: [['product_id', '=', 37]],
name: 'Partners',
res_model: 'partner',
target: 'current',
type: 'ir.actions.act_window',
view_mode: 'list',
view_type: 'list',
views: [[false, 'list'], [2, 'form']],
}, "should trigger do_action with the correct args");
},
},
session: {
user_context: {userContextKey: true},
},
viewOptions: {
action: {
views: [[2, 'form'], [5, 'kanban'], [false, 'list'], [false, 'pivot']],
},
context: {someKey: true, search_default_test: 3},
title: 'Partners',
}
});
assert.ok(pivot.$el.hasClass('o_enable_linking'),
"root node should have classname 'o_enable_linking'");
pivot.$('.o_pivot_cell_value:contains(12)').click(); // should trigger a do_action
pivot.destroy();
});
QUnit.test('pivot view with disable_linking="True"', function (assert) {
assert.expect(2);
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot disable_linking="True">' +
'<field name="foo" type="measure"/>' +
'</pivot>',
intercepts: {
do_action: function () {
assert.ok(false, "should not trigger do_action");
},
},
});
assert.notOk(pivot.$el.hasClass('o_enable_linking'),
"root node should not have classname 'o_enable_linking'");
assert.strictEqual(pivot.$('.o_pivot_cell_value').length, 1,
"should have one cell");
pivot.$('.o_pivot_cell_value').click(); // should not trigger a do_action
pivot.destroy();
});
QUnit.test('pivot view grouped by date field', function (assert) {
assert.expect(2);
var data = this.data;
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot>' +
'<field name="date" interval="month" type="col"/>' +
'<field name="foo" type="measure"/>' +
'</pivot>',
mockRPC: function (route, params) {
var wrong_fields = _.filter(params.kwargs.fields, function (field) {
return !(field in data.partner.fields);
});
assert.ok(!wrong_fields.length, 'fields given to read_group should exist on the model');
return this._super.apply(this, arguments);
},
});
pivot.destroy();
});
QUnit.test('without measures, pivot view uses __count by default', function (assert) {
assert.expect(2);
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot></pivot>',
mockRPC: function (route, args) {
if (args.method === 'read_group') {
assert.deepEqual(args.kwargs.fields, ['__count'],
"should make a read_group with no valid fields");
}
return this._super(route, args);
}
});
var $countMeasure = pivot.$buttons.find('li[data-field=__count]');
assert.ok($countMeasure.hasClass('selected'), "The count measure should be activated");
pivot.destroy();
});
QUnit.test('pivot view can be reloaded', function (assert) {
assert.expect(4);
var readGroupCount = 0;
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot></pivot>',
mockRPC: function (route, args) {
if (args.method === 'read_group') {
readGroupCount++;
}
return this._super(route, args);
}
});
assert.strictEqual(pivot.$('td.o_pivot_cell_value:contains(4)').length, 1,
"should contain a pivot cell with the number of all records");
assert.strictEqual(readGroupCount, 1, "should have done 1 rpc");
pivot.update({domain: [['foo', '>', 10]]});
assert.strictEqual(pivot.$('td.o_pivot_cell_value:contains(2)').length, 1,
"should contain a pivot cell with the number of remaining records");
assert.strictEqual(readGroupCount, 2, "should have done 2 rpcs");
pivot.destroy();
});
QUnit.test('pivot view grouped by many2one field', function (assert) {
assert.expect(3);
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot>' +
'<field name="product_id" type="row"/>' +
'<field name="foo" type="measure"/>' +
'</pivot>',
});
assert.strictEqual(pivot.$('.o_pivot_header_cell_opened').length, 1,
"should have one opened header");
assert.strictEqual(pivot.$('.o_pivot_header_cell_closed:contains(xphone)').length, 1,
"should display one header with 'xphone'");
assert.strictEqual(pivot.$('.o_pivot_header_cell_closed:contains(xpad)').length, 1,
"should display one header with 'xpad'");
pivot.destroy();
});
QUnit.test('basic folding/unfolding', function (assert) {
assert.expect(7);
var rpcCount = 0;
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot>' +
'<field name="product_id" type="row"/>' +
'<field name="foo" type="measure"/>' +
'</pivot>',
mockRPC: function () {
rpcCount++;
return this._super.apply(this, arguments);
},
});
assert.strictEqual(pivot.$('tbody tr').length, 3,
"should have 3 rows: 1 for the opened header, and 2 for data");
// click on the opened header to close it
pivot.$('.o_pivot_header_cell_opened').click();
assert.strictEqual(pivot.$('tbody tr').length, 1, "should have 1 row");
// click on closed header to open dropdown
pivot.$('tbody .o_pivot_header_cell_closed').click();
assert.strictEqual(pivot.$('ul.o_pivot_field_menu > li[data-field="date"]').length, 1,
"should have the date field as proposition");
assert.strictEqual(pivot.$('.o_field_selection li[data-field="product_id"]').length, 1,
"should have the product_id field as proposition");
assert.strictEqual(pivot.$('.o_field_selection li[data-field="non_stored_m2o"]').length, 0,
"should not have the non_stored_m2o field as proposition");
pivot.$('ul.o_pivot_field_menu > li[data-field="date"] a').click();
assert.strictEqual(pivot.$('tbody tr').length, 4,
"should have 4 rows: one for header, 3 for data");
assert.strictEqual(rpcCount, 3,
"should have done 3 rpcs (initial load) + open header with different groupbys");
pivot.destroy();
});
QUnit.test('more folding/unfolding', function (assert) {
assert.expect(1);
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot>' +
'<field name="product_id" type="row"/>' +
'<field name="foo" type="measure"/>' +
'</pivot>',
});
// open dropdown to zoom into first row
pivot.$('tbody .o_pivot_header_cell_closed').first().click();
// click on date by day
pivot.$('ul.o_pivot_field_menu > li[data-field="date"] a[data-interval="day"]').click();
// open dropdown to zoom into second row
pivot.$('tbody td.o_pivot_header_cell_closed:eq(1)').first().click();
assert.strictEqual(pivot.$('tbody tr').length, 7,
"should have 7 rows (1 for total, 1 for xphone, 1 for xpad, 4 for data)");
pivot.destroy();
});
QUnit.test('pivot view can be flipped', function (assert) {
assert.expect(3);
var rpcCount = 0;
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot>' +
'<field name="product_id" type="row"/>' +
'</pivot>',
mockRPC: function () {
rpcCount++;
return this._super.apply(this, arguments);
},
});
assert.strictEqual(pivot.$('tbody tr').length, 3,
"should have 3 rows: 1 for the open header, and 2 for data");
rpcCount = 0;
pivot.$buttons.find('.o_pivot_flip_button').click();
assert.strictEqual(rpcCount, 0, "should not have done any rpc");
assert.strictEqual(pivot.$('tbody tr').length, 1,
"should have 1 rows: 1 for the main header");
pivot.destroy();
});
QUnit.test('can toggle extra measure', function (assert) {
assert.expect(8);
var rpcCount = 0;
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot>' +
'<field name="product_id" type="row"/>' +
'<field name="foo" type="measure"/>' +
'</pivot>',
mockRPC: function () {
rpcCount++;
return this._super.apply(this, arguments);
},
});
assert.strictEqual(pivot.$('.o_pivot_cell_value').length, 3,
"should have 3 cells: 1 for the open header, and 2 for data");
assert.ok(!pivot.$buttons.find('li[data-field=__count]').hasClass('selected'),
"the __count measure should not be selected");
rpcCount = 0;
pivot.$buttons.find('li[data-field=__count] a').click();
assert.ok(pivot.$buttons.find('li[data-field=__count]').hasClass('selected'),
"the __count measure should be selected");
assert.strictEqual(pivot.$('.o_pivot_cell_value').length, 6,
"should have 6 cells: 2 for the open header, and 4 for data");
assert.strictEqual(rpcCount, 2,
"should have done 2 rpcs to reload data");
pivot.$buttons.find('li[data-field=__count] a').click();
assert.ok(!pivot.$buttons.find('li[data-field=__count]').hasClass('selected'),
"the __count measure should not be selected");
assert.strictEqual(pivot.$('.o_pivot_cell_value').length, 3,
"should have 3 cells: 1 for the open header, and 2 for data");
assert.strictEqual(rpcCount, 2,
"should not have done any extra rpcs");
pivot.destroy();
});
QUnit.test('no content helper when no active measure', function (assert) {
assert.expect(4);
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot string="Partners">' +
'</pivot>',
});
assert.strictEqual(pivot.$('.oe_view_nocontent').length, 0,
"should not have a no_content_helper");
assert.strictEqual(pivot.$('table').length, 1,
"should have a table in DOM");
pivot.$buttons.find('li[data-field=__count] a').click();
assert.strictEqual(pivot.$('.oe_view_nocontent').length, 1,
"should have a no_content_helper");
assert.strictEqual(pivot.$('table').length, 0,
"should not have a table in DOM");
pivot.destroy();
});
QUnit.test('no content helper when no data', function (assert) {
assert.expect(4);
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot string="Partners">' +
'</pivot>',
});
assert.strictEqual(pivot.$('.oe_view_nocontent').length, 0,
"should not have a no_content_helper");
assert.strictEqual(pivot.$('table').length, 1,
"should have a table in DOM");
pivot.update({domain: [['foo', '=', 12345]]});
assert.strictEqual(pivot.$('.oe_view_nocontent').length, 1,
"should have a no_content_helper");
assert.strictEqual(pivot.$('table').length, 0,
"should not have a table in DOM");
pivot.destroy();
});
QUnit.test('no content helper when no data, part 2', function (assert) {
assert.expect(1);
this.data.partner.records = [];
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot string="Partners"></pivot>',
});
assert.strictEqual(pivot.$('.oe_view_nocontent').length, 1,
"should have a no_content_helper");
pivot.destroy();
});
QUnit.test('no content helper when no data, part 3', function (assert) {
assert.expect(4);
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot string="Partners"></pivot>',
viewOptions: {
domain: [['foo', '=', 12345]]
},
});
assert.strictEqual(pivot.$('.oe_view_nocontent').length, 1,
"should have a no_content_helper");
pivot.update({domain: [['foo', '=', 12345]]});
assert.strictEqual(pivot.$('.oe_view_nocontent').length, 1,
"should still have a no_content_helper");
pivot.update({domain: []});
assert.strictEqual(pivot.$('.oe_view_nocontent').length, 0,
"should not have a no_content_helper");
// tries to open a field selection menu, to make sure it was not
// removed from the dom.
pivot.$('.o_pivot_header_cell_closed').first().click();
assert.strictEqual(pivot.$('ul.o_pivot_field_menu').length, 1,
"the field selector menu exists");
pivot.destroy();
});
QUnit.test('tries to restore previous state after domain change', function (assert) {
assert.expect(5);
var rpcCount = 0;
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot>' +
'<field name="product_id" type="row"/>' +
'<field name="foo" type="measure"/>' +
'</pivot>',
mockRPC: function () {
rpcCount++;
return this._super.apply(this, arguments);
},
});
assert.strictEqual(pivot.$('.o_pivot_cell_value').length, 3,
"should have 3 cells: 1 for the open header, and 2 for data");
assert.strictEqual(pivot.$('.o_pivot_measure_row:contains(Foo)').length, 1,
"should have 1 row for measure Foo");
pivot.update({domain: [['foo', '=', 12345]]});
rpcCount = 0;
pivot.update({domain: []});
assert.equal(rpcCount, 2, "should have reloaded data");
assert.strictEqual(pivot.$('.o_pivot_cell_value').length, 3,
"should still have 3 cells: 1 for the open header, and 2 for data");
assert.strictEqual(pivot.$('.o_pivot_measure_row:contains(Foo)').length, 1,
"should still have 1 row for measure Foo");
pivot.destroy();
});
QUnit.test('can be grouped with the update function', function (assert) {
assert.expect(4);
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot>' +
'<field name="foo" type="measure"/>' +
'</pivot>',
});
assert.strictEqual(pivot.$('.o_pivot_cell_value').length, 1,
"should have only 1 cell");
assert.strictEqual(pivot.$('tbody tr').length, 1,
"should have 1 rows");
pivot.update({groupBy: ['product_id']});
assert.strictEqual(pivot.$('.o_pivot_cell_value').length, 3,
"should have 3 cells");
assert.strictEqual(pivot.$('tbody tr').length, 3,
"should have 3 rows");
pivot.destroy();
});
QUnit.test('can sort data in a column by clicking on header', function (assert) {
assert.expect(3);
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot>' +
'<field name="foo" type="measure"/>' +
'<field name="product_id" type="row"/>' +
'</pivot>',
});
assert.strictEqual($('td.o_pivot_cell_value').text(), "321220",
"should have proper values in cells (total, result 1, result 2");
pivot.$('th.o_pivot_measure_row').click();
assert.strictEqual($('td.o_pivot_cell_value').text(), "322012",
"should have proper values in cells (total, result 2, result 1");
pivot.$('th.o_pivot_measure_row').click();
assert.strictEqual($('td.o_pivot_cell_value').text(), "321220",
"should have proper values in cells (total, result 1, result 2");
pivot.destroy();
});
QUnit.test('can expand all rows', function (assert) {
assert.expect(7);
var nbReadGroups = 0;
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot>' +
'<field name="foo" type="measure"/>' +
'<field name="product_id" type="row"/>' +
'</pivot>',
mockRPC: function (route, args) {
if (args.method === 'read_group') {
nbReadGroups++;
}
return this._super.apply(this, arguments);
},
});
assert.strictEqual(nbReadGroups, 2, "should have done 2 read_group RPCS");
assert.strictEqual(pivot.$('td.o_pivot_cell_value').text(), "321220",
"should have proper values in cells (total, result 1, result 2)");
// expand on date:days, product
nbReadGroups = 0;
pivot.update({groupBy: ['date:days', 'product_id']});
assert.strictEqual(nbReadGroups, 3, "should have done 3 read_group RPCS");
assert.strictEqual(pivot.$('tbody tr').length, 8,
"should have 7 rows (total + 3 for December and 2 for October and April)");
// collapse the last two rows
pivot.$('.o_pivot_header_cell_opened').last().click();
pivot.$('.o_pivot_header_cell_opened').last().click();
assert.strictEqual(pivot.$('tbody tr').length, 6,
"should have 6 rows now");
// expand all
nbReadGroups = 0;
pivot.$buttons.find('.o_pivot_expand_button').click();
assert.strictEqual(nbReadGroups, 3, "should have done 3 read_group RPCS");
assert.strictEqual(pivot.$('tbody tr').length, 8,
"should have 8 rows again");
pivot.destroy();
});
QUnit.test('expand all with a delay', function (assert) {
assert.expect(3);
var def;
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot>' +
'<field name="foo" type="measure"/>' +
'<field name="product_id" type="row"/>' +
'</pivot>',
mockRPC: function (route, args) {
var result = this._super.apply(this, arguments);
if (args.method === 'read_group') {
return $.when(def).then(_.constant(result));
}
return result;
},
});
// expand on date:days, product
pivot.update({groupBy: ['date:days', 'product_id']});
assert.strictEqual(pivot.$('tbody tr').length, 8,
"should have 7 rows (total + 3 for December and 2 for October and April)");
// collapse the last two rows
pivot.$('.o_pivot_header_cell_opened').last().click();
pivot.$('.o_pivot_header_cell_opened').last().click();
assert.strictEqual(pivot.$('tbody tr').length, 6,
"should have 6 rows now");
// expand all
def = $.Deferred();
pivot.$buttons.find('.o_pivot_expand_button').click();
def.resolve();
assert.strictEqual(pivot.$('tbody tr').length, 8,
"should have 8 rows again");
pivot.destroy();
});
QUnit.test('can download a file', function (assert) {
assert.expect(1);
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot>' +
'<field name="date" interval="month" type="col"/>' +
'<field name="foo" type="measure"/>' +
'</pivot>',
session: {
get_file: function (args) {
assert.strictEqual(args.url, '/web/pivot/export_xls',
"should call get_file with correct parameters");
args.complete();
},
},
});
pivot.$buttons.find('.o_pivot_download').click();
pivot.destroy();
});
QUnit.test('can download a file without data', function (assert) {
assert.expect(1);
this.data.partner.records = [];
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot>' +
'<field name="date" interval="month" type="col"/>' +
'<field name="foo" type="measure"/>' +
'</pivot>',
session: {
get_file: function (args) {
assert.strictEqual(args.url, '/web/pivot/export_xls',
"should call get_file with correct parameters");
args.complete();
},
},
});
pivot.$buttons.find('.o_pivot_download').click();
pivot.destroy();
});
QUnit.test('getContext correctly returns measures and groupbys', function (assert) {
assert.expect(3);
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot>' +
'<field name="date" interval="day" type="col"/>' +
'<field name="foo" type="measure"/>' +
'</pivot>',
});
assert.deepEqual(pivot.getContext(), {
pivot_column_groupby: ['date:day'],
pivot_measures: ['foo'],
pivot_row_groupby: [],
}, "context should be correct");
// expand header on field customer
pivot.$('thead .o_pivot_header_cell_closed:nth(1)').click();
pivot.$('ul.o_pivot_field_menu > li[data-field="customer"] a').click();
assert.deepEqual(pivot.getContext(), {
pivot_column_groupby: ['date:day', 'customer'],
pivot_measures: ['foo'],
pivot_row_groupby: [],
}, "context should be correct");
// expand row on field product_id
pivot.$('tbody .o_pivot_header_cell_closed').first().click();
pivot.$('ul.o_pivot_field_menu > li[data-field="product_id"] a').click();
assert.deepEqual(pivot.getContext(), {
pivot_column_groupby: ['date:day', 'customer'],
pivot_measures: ['foo'],
pivot_row_groupby: ['product_id'],
}, "context should be correct");
pivot.destroy();
});
QUnit.test('correctly remove pivot_ keys from the context', function (assert) {
assert.expect(5);
this.data.partner.fields.amount = {string: "Amount", type: "float"};
// Equivalent to loading with default filter
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot>' +
'<field name="date" interval="day" type="col"/>' +
'<field name="amount" type="measure"/>' +
'</pivot>',
viewOptions: {
context: {
pivot_measures: ['foo'],
pivot_column_groupby: ['customer'],
pivot_row_groupby: ['product_id'],
},
},
});
// Equivalent to unload the filter
var reloadParams = {
context: {},
};
pivot.reload(reloadParams);
assert.deepEqual(pivot.getContext(), {
pivot_column_groupby: ['customer'],
pivot_measures: ['foo'],
pivot_row_groupby: ['product_id'],
}, "context should be correct");
// Let's get rid of the rows groupBy
pivot.$('tbody .o_pivot_header_cell_opened').click();
assert.deepEqual(pivot.getContext(), {
pivot_column_groupby: ['customer'],
pivot_measures: ['foo'],
pivot_row_groupby: [],
}, "context should be correct");
// And now, get rid of the col groupby
pivot.$('thead .o_pivot_header_cell_opened').click();
assert.deepEqual(pivot.getContext(), {
pivot_column_groupby: [],
pivot_measures: ['foo'],
pivot_row_groupby: [],
}, "context should be correct");
pivot.$('tbody .o_pivot_header_cell_closed').click();
pivot.$('.o_pivot_field_menu li[data-field=product_id] a').click();
assert.deepEqual(pivot.getContext(), {
pivot_column_groupby: [],
pivot_measures: ['foo'],
pivot_row_groupby: ['product_id'],
}, "context should be correct");
pivot.$('thead .o_pivot_header_cell_closed').click();
pivot.$('.o_pivot_field_menu li[data-field=customer] a').click();
assert.deepEqual(pivot.getContext(), {
pivot_column_groupby: ['customer'],
pivot_measures: ['foo'],
pivot_row_groupby: ['product_id'],
}, "context should be correct");
pivot.destroy();
});
QUnit.test('Unload Filter, reset display, load another filter', function (assert) {
assert.expect(18);
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot>' +
'<field name="foo" type="measure"/>' +
'</pivot>',
viewOptions: {
context: {
pivot_measures: ['foo'],
pivot_column_groupby: ['customer'],
pivot_row_groupby: ['product_id'],
},
},
});
// Check Columns
assert.strictEqual(pivot.$('thead .o_pivot_header_cell_opened').length, 1,
'The column should be grouped');
assert.strictEqual(pivot.$('thead tr:contains("First")').length, 1,
'There should be a column "First"');
assert.strictEqual(pivot.$('thead tr:contains("Second")').length, 1,
'There should be a column "Second"');
// Check Rows
assert.strictEqual(pivot.$('tbody .o_pivot_header_cell_opened').length, 1,
'The row should be grouped');
assert.strictEqual(pivot.$('tbody tr:contains("xphone")').length, 1,
'There should be a row "xphone"');
assert.strictEqual(pivot.$('tbody tr:contains("xpad")').length, 1,
'There should be a row "xpad"');
// Equivalent to unload the filter
var reloadParams = {
context: {},
};
pivot.reload(reloadParams);
// collapse all headers
pivot.$('.o_pivot_header_cell_opened').click();
pivot.$('.o_pivot_header_cell_opened').click();
// Check Columns
assert.strictEqual(pivot.$('thead .o_pivot_header_cell_closed').length, 1,
'The column should not be grouped');
assert.strictEqual(pivot.$('thead tr:contains("First")').length, 0,
'There should not be a column "First"');
assert.strictEqual(pivot.$('thead tr:contains("Second")').length, 0,
'There should not be a column "Second"');
// Check Rows
assert.strictEqual(pivot.$('tbody .o_pivot_header_cell_closed').length, 1,
'The row should not be grouped');
assert.strictEqual(pivot.$('tbody tr:contains("xphone")').length, 0,
'There should not be a row "xphone"');
assert.strictEqual(pivot.$('tbody tr:contains("xpad")').length, 0,
'There should not be a row "xpad"');
// Equivalent to load another filter
reloadParams = {
context: {
pivot_measures: ['foo'],
pivot_column_groupby: ['customer'],
pivot_row_groupby: ['product_id'],
},
};
pivot.reload(reloadParams);
// Check Columns
assert.strictEqual(pivot.$('thead .o_pivot_header_cell_opened').length, 1,
'The column should be grouped');
assert.strictEqual(pivot.$('thead tr:contains("First")').length, 1,
'There should be a column "First"');
assert.strictEqual(pivot.$('thead tr:contains("Second")').length, 1,
'There should be a column "Second"');
// Check Rows
assert.strictEqual(pivot.$('tbody .o_pivot_header_cell_opened').length, 1,
'The row should be grouped');
assert.strictEqual(pivot.$('tbody tr:contains("xphone")').length, 1,
'There should be a row "xphone"');
assert.strictEqual(pivot.$('tbody tr:contains("xpad")').length, 1,
'There should be a row "xpad"');
pivot.destroy();
});
QUnit.test('correctly uses pivot_ keys from the context', function (assert) {
assert.expect(7);
this.data.partner.fields.amount = {string: "Amount", type: "float"};
var pivot = createView({
View: PivotView,
model: "partner",
data: this.data,
arch: '<pivot>' +