summaryrefslogtreecommitdiff
path: root/web-ui/test/spec/mail_view/ui/attachment_list.spec.js
blob: 20f82704c08009f84c74346e02b565f634e485e3 (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
describeMixin('mail_view/ui/attachment_list', function () {
    'use strict';

    describe('initialization', function () {
        beforeEach(function () {
            this.setupComponent('<div id="attachment-list">' +
                '<ul id="attachment-list-item"></ul>' +
                '</div>');
        });

        it('should add attachment to the list based on uploadedAttachment event', function () {
            var stubAttachment = {ident: 'faked'};
            $(document).trigger(Pixelated.events.mail.appendAttachment, stubAttachment);
            expect(this.component.attr.attachments).toEqual([stubAttachment]);

            var anotherStubAttachment = {ident: 'faked 2'};
            $(document).trigger(Pixelated.events.mail.appendAttachment, anotherStubAttachment);
            expect(this.component.attr.attachments).toEqual([stubAttachment, anotherStubAttachment]);
        });

        it('should trigger add attachment event', function () {
            var triggerUploadAttachment = spyOnEvent(document, Pixelated.events.mail.appendAttachment);
            var stubAttachment = {ident: 'faked'};

            $(document).trigger(Pixelated.events.mail.uploadedAttachment, stubAttachment);

            expect(triggerUploadAttachment).toHaveBeenTriggeredOnAndWith(document, stubAttachment);
        });

        it('should render attachment list view based on uploadedAttachment event', function () {
            var stubAttachment = {ident: 'faked', name: 'haha.txt', size: 4500, encoding: 'base64'};

            $(document).trigger(Pixelated.events.mail.uploadedAttachment, stubAttachment);

            expect(this.component.select('attachmentListItem').html()).toContain('href="/attachment/faked');
            expect(this.component.select('attachmentListItem').html()).toContain('filename=haha.txt');
            expect(this.component.select('attachmentListItem').html()).toContain('encoding=base64');
            expect(this.component.select('attachmentListItem').html()).toContain('haha.txt');
            expect(this.component.select('attachmentListItem').html()).toContain('(4.39 Kb');
        });

        describe('Upload files -- max file size -- ', function (){
            var ONE_MEGABYTE = 1024*1024;
            var submitFile = 'file not submitted', submitted = 'file submitted';
            var mockSubmit = function (){ submitFile = submitted; };
            var largeAttachment = {originalFiles: [{size: ONE_MEGABYTE+1}], submit: mockSubmit};
            var dummyEvent = 'whatever, not used';

            it('should show error messages on the dom, when uploading files larger than 1MB', function () {
                this.component.checkAttachmentSize(dummyEvent, largeAttachment);

                expect(this.component.select('uploadError').html()).toContain('Upload failed. This file exceeds the 1MB limit.');
            });

            it('should dismiss upload failed message when clicking close icon', function () {
                this.component.checkAttachmentSize(dummyEvent, largeAttachment);

                this.component.select('closeIcon').click();

                expect(this.component.select('uploadError').html()).toBe(undefined);
            });

            it('should dismiss upload failed message when clicking dismiss button', function () {
                this.component.checkAttachmentSize(dummyEvent, largeAttachment);

                this.component.select('dismissButton').click();

                expect(this.component.select('uploadError').html()).toBe(undefined);
            });

            it('should start file upload when clicking Choose another file button', function () {
                this.component.checkAttachmentSize(dummyEvent, largeAttachment);

                var triggerUploadAttachment = spyOnEvent(document, Pixelated.events.mail.startUploadAttachment);

                this.component.select('uploadFileButton').click();

                expect(triggerUploadAttachment).toHaveBeenTriggeredOn(document);
            });

            it('should not upload files larger than 1MB', function () {
                spyOn(largeAttachment, 'submit');

                this.component.checkAttachmentSize(dummyEvent, largeAttachment);

                expect(largeAttachment.submit).not.toHaveBeenCalled();
            });

            it('should upload files less or equal 1MB', function () {
                var smallAttachment = {originalFiles: [{size: ONE_MEGABYTE}], submit: mockSubmit};
                this.component.checkAttachmentSize(dummyEvent, smallAttachment);

                expect(submitFile).toEqual(submitted);
            });
        });
    });
});