summaryrefslogtreecommitdiff
path: root/spec/javascripts/repo/components/new_dropdown/index_spec.js
blob: ddbfdab582d71c564e98ebeeb873f99afcf28cf3 (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
import Vue from 'vue';
import newDropdown from '~/repo/components/new_dropdown/index.vue';
import RepoStore from '~/repo/stores/repo_store';
import RepoHelper from '~/repo/helpers/repo_helper';
import eventHub from '~/repo/event_hub';
import createComponent from '../../../helpers/vue_mount_component_helper';

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

  beforeEach(() => {
    const component = Vue.extend(newDropdown);

    vm = createComponent(component);
  });

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

    RepoStore.files = [];
    RepoStore.openedFiles = [];
    RepoStore.setViewToPreview();
  });

  it('renders new file and new directory links', () => {
    expect(vm.$el.querySelectorAll('a')[0].textContent.trim()).toBe('New file');
    expect(vm.$el.querySelectorAll('a')[1].textContent.trim()).toBe('New directory');
  });

  describe('createNewItem', () => {
    it('sets modalType to blob when new file is clicked', () => {
      vm.$el.querySelectorAll('a')[0].click();

      expect(vm.modalType).toBe('blob');
    });

    it('sets modalType to tree when new directory is clicked', () => {
      vm.$el.querySelectorAll('a')[1].click();

      expect(vm.modalType).toBe('tree');
    });

    it('opens modal when link is clicked', (done) => {
      vm.$el.querySelectorAll('a')[0].click();

      Vue.nextTick(() => {
        expect(vm.$el.querySelector('.modal')).not.toBeNull();

        done();
      });
    });
  });

  describe('toggleModalOpen', () => {
    it('closes modal after toggling', (done) => {
      vm.toggleModalOpen();

      Vue.nextTick()
        .then(() => {
          expect(vm.$el.querySelector('.modal')).not.toBeNull();
        })
        .then(vm.toggleModalOpen)
        .then(() => {
          expect(vm.$el.querySelector('.modal')).toBeNull();
        })
        .then(done)
        .catch(done.fail);
    });
  });

  describe('createEntryInStore', () => {
    ['tree', 'blob'].forEach((type) => {
      describe(type, () => {
        it('closes modal after creating file', () => {
          vm.openModal = true;

          eventHub.$emit('createNewEntry', 'testing', type);

          expect(vm.openModal).toBeFalsy();
        });

        it('sets editMode to true', () => {
          eventHub.$emit('createNewEntry', 'testing', type);

          expect(RepoStore.editMode).toBeTruthy();
        });

        it('toggles blob view', () => {
          eventHub.$emit('createNewEntry', 'testing', type);

          expect(RepoStore.isPreviewView()).toBeFalsy();
        });

        it('adds file into activeFiles', () => {
          eventHub.$emit('createNewEntry', 'testing', type);

          expect(RepoStore.openedFiles.length).toBe(1);
        });

        it(`creates ${type} in the current stores path`, () => {
          RepoStore.path = 'testing';

          eventHub.$emit('createNewEntry', 'testing/app', type);

          expect(RepoStore.files[0].path).toBe('testing/app');
          expect(RepoStore.files[0].name).toBe('app');

          if (type === 'tree') {
            expect(RepoStore.files[0].files.length).toBe(1);
          }

          RepoStore.path = '';
        });
      });
    });

    describe('file', () => {
      it('creates new file', () => {
        eventHub.$emit('createNewEntry', 'testing', 'blob');

        expect(RepoStore.files.length).toBe(1);
        expect(RepoStore.files[0].name).toBe('testing');
        expect(RepoStore.files[0].type).toBe('blob');
        expect(RepoStore.files[0].tempFile).toBeTruthy();
      });

      it('does not create temp file when file already exists', () => {
        RepoStore.files.push(RepoHelper.serializeRepoEntity('blob', {
          name: 'testing',
        }));

        eventHub.$emit('createNewEntry', 'testing', 'blob');

        expect(RepoStore.files.length).toBe(1);
        expect(RepoStore.files[0].name).toBe('testing');
        expect(RepoStore.files[0].type).toBe('blob');
        expect(RepoStore.files[0].tempFile).toBeUndefined();
      });
    });

    describe('tree', () => {
      it('creates new tree', () => {
        eventHub.$emit('createNewEntry', 'testing', 'tree');

        expect(RepoStore.files.length).toBe(1);
        expect(RepoStore.files[0].name).toBe('testing');
        expect(RepoStore.files[0].type).toBe('tree');
        expect(RepoStore.files[0].tempFile).toBeTruthy();
        expect(RepoStore.files[0].files.length).toBe(1);
        expect(RepoStore.files[0].files[0].name).toBe('.gitkeep');
      });

      it('creates multiple trees when entryName has slashes', () => {
        eventHub.$emit('createNewEntry', 'app/test', 'tree');

        expect(RepoStore.files.length).toBe(1);
        expect(RepoStore.files[0].name).toBe('app');
        expect(RepoStore.files[0].files[0].name).toBe('test');
        expect(RepoStore.files[0].files[0].files[0].name).toBe('.gitkeep');
      });

      it('creates tree in existing tree', () => {
        RepoStore.files.push(RepoHelper.serializeRepoEntity('tree', {
          name: 'app',
        }));

        eventHub.$emit('createNewEntry', 'app/test', 'tree');

        expect(RepoStore.files.length).toBe(1);
        expect(RepoStore.files[0].name).toBe('app');
        expect(RepoStore.files[0].tempFile).toBeUndefined();
        expect(RepoStore.files[0].files[0].tempFile).toBeTruthy();
        expect(RepoStore.files[0].files[0].name).toBe('test');
        expect(RepoStore.files[0].files[0].files[0].name).toBe('.gitkeep');
      });

      it('does not create new tree when already exists', () => {
        RepoStore.files.push(RepoHelper.serializeRepoEntity('tree', {
          name: 'app',
        }));

        eventHub.$emit('createNewEntry', 'app', 'tree');

        expect(RepoStore.files.length).toBe(1);
        expect(RepoStore.files[0].name).toBe('app');
        expect(RepoStore.files[0].tempFile).toBeUndefined();
        expect(RepoStore.files[0].files.length).toBe(0);
      });
    });
  });
});