summaryrefslogtreecommitdiff
path: root/addons/web/static/tests/fields/signature_tests.js
blob: 088c6d705cbe33866b7ee49f1753fb4702953585 (plain)
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
odoo.define('web.signature_field_tests', function (require) {
"use strict";

var ajax = require('web.ajax');
var core = require('web.core');
var FormView = require('web.FormView');
var testUtils = require('web.test_utils');

var createView = testUtils.createView;

QUnit.module('fields', {}, function () {

QUnit.module('signature', {
    beforeEach: function () {
        this.data = {
            partner: {
                fields: {
                    display_name: {string: "Name", type: "char" },
                    product_id: {string: "Product Name", type: "many2one", relation: 'product'},
                    sign: {string: "Signature", type: "binary"},
                },
                records: [{
                    id: 1,
                    display_name: "Pop's Chock'lit",
                    product_id: 7,
                }],
                onchanges: {},
            },
            product: {
                fields: {
                    name: {string: "Product Name", type: "char"}
                },
                records: [{
                    id: 7,
                    display_name: "Veggie Burger",
                }]
            },
        };
    }
}, function () {

    QUnit.module('Signature Field', {
        before: function () {
            return ajax.loadXML('/web/static/src/xml/name_and_signature.xml', core.qweb);
        },
    });

    QUnit.test('Set simple field in "full_name" node option', async function (assert) {
        assert.expect(3);

        var form = await createView({
            View: FormView,
            model: 'partner',
            res_id: 1,
            data: this.data,
            arch: '<form>' +
                    '<field name="display_name"/>' +
                    '<field name="sign" widget="signature" options="{\'full_name\': \'display_name\'}" />' +
                '</form>',
            mockRPC: function (route, args) {
                if (route === '/web/sign/get_fonts/') {
                    return Promise.resolve();
                }
                return this._super(route, args);
            },
        });

        await testUtils.form.clickEdit(form);

        assert.containsOnce(form, 'div[name=sign] div.o_signature svg',
            "should have a valid signature widget");
        // Click on the widget to open signature modal
        await testUtils.dom.click(form.$('div[name=sign] div.o_signature'));
        assert.strictEqual($('.modal .modal-body a.o_web_sign_auto_button').length, 1,
            'should open a modal with "Auto" button');
        assert.strictEqual($('.modal .modal-body .o_web_sign_name_input').val(), "Pop's Chock'lit",
            'Correct Value should be set in the input for auto drawing the signature');

        form.destroy();
    });

    QUnit.test('Set m2o field in "full_name" node option', async function (assert) {
        assert.expect(3);

        var form = await createView({
            View: FormView,
            model: 'partner',
            res_id: 1,
            data: this.data,
            arch: '<form>' +
                    '<field name="product_id"/>' +
                    '<field name="sign" widget="signature" options="{\'full_name\': \'product_id\'}" />' +
                '</form>',
            mockRPC: function (route, args) {
                if (route === '/web/sign/get_fonts/') {
                    return Promise.resolve();
                }
                return this._super(route, args);
            },
        });

        await testUtils.form.clickEdit(form);

        assert.containsOnce(form, 'div[name=sign] div.o_signature svg',
            "should have a valid signature widget");
        // Click on the widget to open signature modal
        await testUtils.dom.click(form.$('div[name=sign] div.o_signature'));
        assert.strictEqual($('.modal .modal-body a.o_web_sign_auto_button').length, 1,
            'should open a modal with "Auto" button');
        assert.strictEqual($('.modal .modal-body .o_web_sign_name_input').val(), "Veggie Burger",
            'Correct Value should be set in the input for auto drawing the signature');

        form.destroy();
    });

    QUnit.module('Signature Widget');

    QUnit.test('Signature widget renders a Sign button', async function (assert) {
        assert.expect(3);

        const form = await createView({
            View: FormView,
            model: 'partner',
            res_id: 1,
            data: this.data,
            arch: '<form>' +
                    '<header>' +
                        '<widget name="signature" string="Sign"/>' +
                    '</header>' +
                '</form>',
            mockRPC: function (route, args) {
                if (route === '/web/sign/get_fonts/') {
                    return Promise.resolve();
                }
                return this._super(route, args);
            },
        });

        assert.containsOnce(form, 'button.o_sign_button.o_widget',
            "Should have a signature widget button");
        assert.strictEqual($('.modal-dialog').length, 0,
            "Should not have any modal");
        // Clicks on the sign button to open the sign modal.
        await testUtils.dom.click(form.$('span.o_sign_label'));
        assert.strictEqual($('.modal-dialog').length, 1,
            "Should have one modal opened");

        form.destroy();
    });

    QUnit.test('Signature widget: full_name option', async function (assert) {
        assert.expect(2);

        const form = await createView({
            View: FormView,
            model: 'partner',
            res_id: 1,
            data: this.data,
            arch: '<form>' +
                    '<header>' +
                        '<widget name="signature" string="Sign" full_name="display_name"/>' +
                    '</header>' +
                    '<field name="display_name"/>' +
                '</form>',
            mockRPC: function (route, args) {
                if (route === '/web/sign/get_fonts/') {
                    return Promise.resolve();
                }
                return this._super(route, args);
            },
        });

        // Clicks on the sign button to open the sign modal.
        await testUtils.dom.click(form.$('span.o_sign_label'));
        assert.strictEqual($('.modal .modal-body a.o_web_sign_auto_button').length, 1,
            "Should open a modal with \"Auto\" button");
        assert.strictEqual($('.modal .modal-body .o_web_sign_name_input').val(), "Pop's Chock'lit",
            "Correct Value should be set in the input for auto drawing the signature");

        form.destroy();
    });

    QUnit.test('Signature widget: highlight option', async function (assert) {
        assert.expect(3);

        const form = await createView({
            View: FormView,
            model: 'partner',
            res_id: 1,
            data: this.data,
            arch: '<form>' +
                    '<header>' +
                        '<widget name="signature" string="Sign" highlight="1"/>' +
                    '</header>' +
                '</form>',
            mockRPC: function (route, args) {
                if (route === '/web/sign/get_fonts/') {
                    return Promise.resolve();
                }
                return this._super(route, args);
            },
        });

        assert.hasClass(form.$('button.o_sign_button.o_widget'), 'btn-primary',
            "The button must have the 'btn-primary' class as \"highlight=1\"");
        // Clicks on the sign button to open the sign modal.
        await testUtils.dom.click(form.$('span.o_sign_label'));
        assert.isNotVisible($('.modal .modal-body a.o_web_sign_auto_button'),
            "\"Auto\" button must be invisible");
        assert.strictEqual($('.modal .modal-body .o_web_sign_name_input').val(), '',
            "No value should be set in the input for auto drawing the signature");

        form.destroy();
    });
});
});
});