summaryrefslogtreecommitdiff
path: root/spec/javascripts/diffs/components/changed_files_spec.js
blob: f737e8fa38ec9ed373bb35b1cdaf86566c18bcad (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
import Vue from 'vue';
import Vuex from 'vuex';
import { mountComponentWithStore } from 'spec/helpers';
import diffsModule from '~/diffs/store/modules';
import changedFiles from '~/diffs/components/changed_files.vue';

describe('ChangedFiles', () => {
  const Component = Vue.extend(changedFiles);
  const store = new Vuex.Store({
    modules: {
      diffs: diffsModule,
    },
  });

  let vm;

  beforeEach(() => {
    setFixtures(`
      <div id="dummy-element"></div>
      <div class="js-tabs-affix"></div>
    `);

    const props = {
      diffFiles: [
        {
          addedLines: 10,
          removedLines: 20,
          blob: {
            path: 'some/code.txt',
          },
          filePath: 'some/code.txt',
        },
      ],
    };

    vm = mountComponentWithStore(Component, { props, store });
  });

  describe('with single file added', () => {
    it('shows files changes', () => {
      expect(vm.$el).toContainText('1 changed file');
    });

    it('shows file additions and deletions', () => {
      expect(vm.$el).toContainText('10 additions');
      expect(vm.$el).toContainText('20 deletions');
    });
  });

  describe('diff view mode buttons', () => {
    let inlineButton;
    let parallelButton;

    beforeEach(() => {
      inlineButton = vm.$el.querySelector('.js-inline-diff-button');
      parallelButton = vm.$el.querySelector('.js-parallel-diff-button');
    });

    it('should have Inline and Side-by-side buttons', () => {
      expect(inlineButton).toBeDefined();
      expect(parallelButton).toBeDefined();
    });

    it('should add active class to Inline button', done => {
      vm.$store.state.diffs.diffViewType = 'inline';

      vm.$nextTick(() => {
        expect(inlineButton.classList.contains('active')).toEqual(true);
        expect(parallelButton.classList.contains('active')).toEqual(false);

        done();
      });
    });

    it('should toggle active state of buttons when diff view type changed', done => {
      vm.$store.state.diffs.diffViewType = 'parallel';

      vm.$nextTick(() => {
        expect(inlineButton.classList.contains('active')).toEqual(false);
        expect(parallelButton.classList.contains('active')).toEqual(true);

        done();
      });
    });

    describe('clicking them', () => {
      it('should toggle the diff view type', done => {
        parallelButton.click();

        vm.$nextTick(() => {
          expect(inlineButton.classList.contains('active')).toEqual(false);
          expect(parallelButton.classList.contains('active')).toEqual(true);

          inlineButton.click();

          vm.$nextTick(() => {
            expect(inlineButton.classList.contains('active')).toEqual(true);
            expect(parallelButton.classList.contains('active')).toEqual(false);
            done();
          });
        });
      });
    });
  });
});