summaryrefslogtreecommitdiff
path: root/spec/javascripts/ide/components/new_dropdown/modal_spec.js
blob: d1a0964ccdd581f63bff45619d5fe603627def0a (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
import Vue from 'vue';
import { createStore } from '~/ide/stores';
import modal from '~/ide/components/new_dropdown/modal.vue';
import { createComponentWithStore } from 'spec/helpers/vue_mount_component_helper';

describe('new file modal component', () => {
  const Component = Vue.extend(modal);
  let vm;

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

  ['tree', 'blob'].forEach(type => {
    describe(type, () => {
      beforeEach(() => {
        const store = createStore();
        store.state.entryModal = {
          type,
          path: '',
          entry: {
            path: '',
          },
        };

        vm = createComponentWithStore(Component, store).$mount();

        vm.name = 'testing';
      });

      it(`sets modal title as ${type}`, () => {
        const title = type === 'tree' ? 'directory' : 'file';

        expect(vm.$el.querySelector('.modal-title').textContent.trim()).toBe(`Create new ${title}`);
      });

      it(`sets button label as ${type}`, () => {
        const title = type === 'tree' ? 'directory' : 'file';

        expect(vm.$el.querySelector('.btn-success').textContent.trim()).toBe(`Create ${title}`);
      });

      it(`sets form label as ${type}`, () => {
        expect(vm.$el.querySelector('.label-bold').textContent.trim()).toBe('Name');
      });

      it(`${type === 'tree' ? 'does not show' : 'shows'} file templates`, () => {
        const templateFilesEl = vm.$el.querySelector('.file-templates');
        if (type === 'tree') {
          expect(templateFilesEl).toBeNull();
        } else {
          expect(templateFilesEl instanceof Element).toBeTruthy();
        }
      });

      describe('createEntryInStore', () => {
        it('$emits create', () => {
          spyOn(vm, 'createTempEntry');

          vm.submitForm();

          expect(vm.createTempEntry).toHaveBeenCalledWith({
            name: 'testing',
            type,
          });
        });
      });
    });
  });

  describe('rename entry', () => {
    beforeEach(() => {
      const store = createStore();
      store.state.entryModal = {
        type: 'rename',
        path: '',
        entry: {
          name: 'test',
          type: 'blob',
          path: 'test-path',
        },
      };

      vm = createComponentWithStore(Component, store).$mount();
    });

    ['tree', 'blob'].forEach(type => {
      it(`renders title and button for renaming ${type}`, done => {
        const text = type === 'tree' ? 'folder' : 'file';

        vm.$store.state.entryModal.entry.type = type;

        vm.$nextTick(() => {
          expect(vm.$el.querySelector('.modal-title').textContent.trim()).toBe(`Rename ${text}`);
          expect(vm.$el.querySelector('.btn-success').textContent.trim()).toBe(`Rename ${text}`);

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

    describe('entryName', () => {
      it('returns entries name', () => {
        expect(vm.entryName).toBe('test-path');
      });

      it('updated name', () => {
        vm.name = 'index.js';

        expect(vm.entryName).toBe('index.js');
      });
    });
  });

  describe('submitForm', () => {
    it('throws an error when target entry exists', () => {
      const store = createStore();
      store.state.entryModal = {
        type: 'rename',
        path: 'test-path/test',
        entry: {
          name: 'test',
          type: 'blob',
          path: 'test-path/test',
        },
      };
      store.state.entries = {
        'test-path/test': {
          name: 'test',
          deleted: false,
        },
      };

      vm = createComponentWithStore(Component, store).$mount();
      const flashSpy = spyOnDependency(modal, 'flash');
      vm.submitForm();

      expect(flashSpy).toHaveBeenCalled();
    });

    it('calls createTempEntry when target path does not exist', () => {
      const store = createStore();
      store.state.entryModal = {
        type: 'rename',
        path: 'test-path/test',
        entry: {
          name: 'test',
          type: 'blob',
          path: 'test-path1/test',
        },
      };

      vm = createComponentWithStore(Component, store).$mount();
      spyOn(vm, 'createTempEntry').and.callFake(() => Promise.resolve());
      vm.submitForm();

      expect(vm.createTempEntry).toHaveBeenCalledWith({
        name: 'test-path1',
        type: 'tree',
      });
    });
  });
});