summaryrefslogtreecommitdiff
path: root/spec/javascripts/diffs/components/changed_files_spec.js
blob: 2d57af6137c666859e47d2de1e6267d240b78523 (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
import Vue from 'vue';
import $ from 'jquery';
import { mountComponentWithStore } from 'spec/helpers';
import store from '~/diffs/store';
import ChangedFiles from '~/diffs/components/changed_files.vue';

describe('ChangedFiles', () => {
  const Component = Vue.extend(ChangedFiles);
  const createComponent = props => mountComponentWithStore(Component, { props, store });
  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 = createComponent(props);
  });

  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('template', () => {
    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();
            });
          });
        });
      });
    });
  });
});