summaryrefslogtreecommitdiff
path: root/spec/javascripts/ide/components/new_dropdown/upload_spec.js
blob: 66ddf6c0ee67dc45b0259cf2c7e34b2e626121cc (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
import Vue from 'vue';
import createComponent from 'spec/helpers/vue_mount_component_helper';
import upload from '~/ide/components/new_dropdown/upload.vue';

describe('new dropdown upload', () => {
  let vm;

  beforeEach(() => {
    const Component = Vue.extend(upload);

    vm = createComponent(Component, {
      path: '',
    });

    vm.entryName = 'testing';

    spyOn(vm, '$emit').and.callThrough();
  });

  afterEach(() => {
    vm.$destroy();
  });

  describe('openFile', () => {
    it('calls for each file', () => {
      const files = ['test', 'test2', 'test3'];

      spyOn(vm, 'readFile');
      spyOnProperty(vm.$refs.fileUpload, 'files').and.returnValue(files);

      vm.openFile();

      expect(vm.readFile.calls.count()).toBe(3);

      files.forEach((file, i) => {
        expect(vm.readFile.calls.argsFor(i)).toEqual([file]);
      });
    });
  });

  describe('readFile', () => {
    beforeEach(() => {
      spyOn(FileReader.prototype, 'readAsDataURL');
    });

    it('calls readAsDataURL for all files', () => {
      const file = {
        type: 'images/png',
      };

      vm.readFile(file);

      expect(FileReader.prototype.readAsDataURL).toHaveBeenCalledWith(file);
    });
  });

  describe('createFile', () => {
    const textTarget = {
      result: 'base64,cGxhaW4gdGV4dA==',
    };
    const binaryTarget = {
      result: 'base64,w4I=',
    };
    const textFile = new File(['plain text'], 'textFile');

    const binaryFile = {
      name: 'binaryFile',
      type: 'image/png',
    };

    beforeEach(() => {
      spyOn(FileReader.prototype, 'readAsText').and.callThrough();
    });

    it('calls readAsText and creates file in plain text (without encoding) if the file content is plain text', done => {
      const waitForCreate = new Promise(resolve => vm.$on('create', resolve));

      vm.createFile(textTarget, textFile);

      expect(FileReader.prototype.readAsText).toHaveBeenCalledWith(textFile);

      waitForCreate
        .then(() => {
          expect(vm.$emit).toHaveBeenCalledWith('create', {
            name: textFile.name,
            type: 'blob',
            content: 'plain text',
            base64: false,
            binary: false,
            rawPath: '',
          });
        })
        .then(done)
        .catch(done.fail);
    });

    it('splits content on base64 if binary', () => {
      vm.createFile(binaryTarget, binaryFile);

      expect(FileReader.prototype.readAsText).not.toHaveBeenCalledWith(textFile);

      expect(vm.$emit).toHaveBeenCalledWith('create', {
        name: binaryFile.name,
        type: 'blob',
        content: binaryTarget.result.split('base64,')[1],
        base64: true,
        binary: true,
        rawPath: binaryTarget.result,
      });
    });
  });
});