summaryrefslogtreecommitdiff
path: root/addons/web/static/tests/components/custom_file_input_tests.js
blob: 279bf67f11b7c887742f3050accb8c13da7a0824 (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
odoo.define('web.custom_file_input_tests', function (require) {
    "use strict";

    const CustomFileInput = require('web.CustomFileInput');
    const testUtils = require('web.test_utils');

    const { createComponent } = testUtils;

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

        // This module cannot be tested as thoroughly as we want it to be:
        // browsers do not let scripts programmatically assign values to inputs
        // of type file
        QUnit.module('CustomFileInput');

        QUnit.test("Upload a file: default props", async function (assert) {
            assert.expect(6);

            const customFileInput = await createComponent(CustomFileInput, {
                env: {
                    services: {
                        async httpRequest(route, params) {
                            assert.deepEqual(params, {
                                csrf_token: odoo.csrf_token,
                                ufile: [],
                            });
                            assert.step(route);
                            return '[]';
                        },
                    },
                },
            });
            const input = customFileInput.el.querySelector('input');

            assert.strictEqual(customFileInput.el.innerText.trim().toUpperCase(), "CHOOSE FILE",
                "File input total text should match its given inner element's text");
            assert.strictEqual(input.accept, '*',
                "Input should accept all files by default");

            await testUtils.dom.triggerEvent(input, 'change');

            assert.notOk(input.multiple, "'multiple' attribute should not be set");
            assert.verifySteps(['/web/binary/upload']);

            customFileInput.destroy();
        });

        QUnit.test("Upload a file: custom attachment", async function (assert) {
            assert.expect(6);

            const customFileInput = await createComponent(CustomFileInput, {
                env: {
                    services: {
                        async httpRequest(route, params) {
                            assert.deepEqual(params, {
                                id: 5,
                                model: 'res.model',
                                csrf_token: odoo.csrf_token,
                                ufile: [],
                            });
                            assert.step(route);
                            return '[]';
                        },
                    },
                },
                props: {
                    accepted_file_extensions: '.png',
                    action: '/web/binary/upload_attachment',
                    id: 5,
                    model: 'res.model',
                    multi_upload: true,
                },
                intercepts: {
                    'uploaded': ev => assert.strictEqual(ev.detail.files.length, 0,
                        "'files' property should be an empty array"),
                },
            });
            const input = customFileInput.el.querySelector('input');

            assert.strictEqual(input.accept, '.png', "Input should now only accept pngs");

            await testUtils.dom.triggerEvent(input, 'change');

            assert.ok(input.multiple, "'multiple' attribute should be set");
            assert.verifySteps(['/web/binary/upload_attachment']);

            customFileInput.destroy();
        });
    });
});