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
|
odoo.define('web.dialog_tests', function (require) {
"use strict";
var Dialog = require('web.Dialog');
var testUtils = require('web.test_utils');
var Widget = require('web.Widget');
var ESCAPE_KEY = $.Event("keyup", { which: 27 });
async function createEmptyParent(debug) {
var widget = new Widget();
await testUtils.mock.addMockEnvironment(widget, {
debug: debug || false,
});
return widget;
}
QUnit.module('core', {}, function () {
QUnit.module('Dialog');
QUnit.test("Closing custom dialog using buttons calls standard callback", async function (assert) {
assert.expect(3);
var testPromise = testUtils.makeTestPromiseWithAssert(assert, 'custom callback');
var parent = await createEmptyParent();
new Dialog(parent, {
buttons: [
{
text: "Close",
classes: 'btn-primary',
close: true,
click: testPromise.resolve,
},
],
$content: $('<main/>'),
onForceClose: testPromise.reject,
}).open();
assert.verifySteps([]);
await testUtils.nextTick();
await testUtils.dom.click($('.modal[role="dialog"] .btn-primary'));
testPromise.then(() => {
assert.verifySteps(['ok custom callback']);
});
parent.destroy();
});
QUnit.test("Closing custom dialog without using buttons calls force close callback", async function (assert) {
assert.expect(3);
var testPromise = testUtils.makeTestPromiseWithAssert(assert, 'custom callback');
var parent = await createEmptyParent();
new Dialog(parent, {
buttons: [
{
text: "Close",
classes: 'btn-primary',
close: true,
click: testPromise.reject,
},
],
$content: $('<main/>'),
onForceClose: testPromise.resolve,
}).open();
assert.verifySteps([]);
await testUtils.nextTick();
await testUtils.dom.triggerEvents($('.modal[role="dialog"]'), [ESCAPE_KEY]);
testPromise.then(() => {
assert.verifySteps(['ok custom callback']);
});
parent.destroy();
});
QUnit.test("Closing confirm dialog without using buttons calls cancel callback", async function (assert) {
assert.expect(3);
var testPromise = testUtils.makeTestPromiseWithAssert(assert, 'confirm callback');
var parent = await createEmptyParent();
var options = {
confirm_callback: testPromise.reject,
cancel_callback: testPromise.resolve,
};
Dialog.confirm(parent, "", options);
assert.verifySteps([]);
await testUtils.nextTick();
await testUtils.dom.triggerEvents($('.modal[role="dialog"]'), [ESCAPE_KEY]);
testPromise.then(() => {
assert.verifySteps(['ok confirm callback']);
});
parent.destroy();
});
QUnit.test("Closing alert dialog without using buttons calls confirm callback", async function (assert) {
assert.expect(3);
var testPromise = testUtils.makeTestPromiseWithAssert(assert, 'alert callback');
var parent = await createEmptyParent();
var options = {
confirm_callback: testPromise.resolve,
};
Dialog.alert(parent, "", options);
assert.verifySteps([]);
await testUtils.nextTick();
await testUtils.dom.triggerEvents($('.modal[role="dialog"]'), [ESCAPE_KEY]);
testPromise.then(() => {
assert.verifySteps(['ok alert callback']);
});
parent.destroy();
});
QUnit.test("Ensure on_attach_callback and on_detach_callback are properly called", async function (assert) {
assert.expect(4);
const TestDialog = Dialog.extend({
on_attach_callback() {
assert.step('on_attach_callback');
},
on_detach_callback() {
assert.step('on_detach_callback');
},
});
const parent = await createEmptyParent();
const dialog = new TestDialog(parent, {
buttons: [
{
text: "Close",
classes: 'btn-primary',
close: true,
},
],
$content: $('<main/>'),
}).open();
await dialog.opened();
assert.verifySteps(['on_attach_callback']);
await testUtils.dom.click($('.modal[role="dialog"] .btn-primary'));
assert.verifySteps(['on_detach_callback']);
parent.destroy();
});
QUnit.test("Should not be displayed if parent is destroyed while dialog is being opened", async function (assert) {
assert.expect(1);
const parent = await createEmptyParent();
const dialog = new Dialog(parent);
dialog.open();
parent.destroy();
await testUtils.nextTick();
assert.containsNone(document.body, ".modal[role='dialog']");
});
});
});
|