summaryrefslogtreecommitdiff
path: root/spec/frontend/single_file_diff_spec.js
blob: 8718152655fb09af42fd65addb454b7d8d96fb39 (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
import MockAdapter from 'axios-mock-adapter';
import $ from 'jquery';
import { setHTMLFixture } from 'helpers/fixtures';
import axios from '~/lib/utils/axios_utils';
import SingleFileDiff from '~/single_file_diff';

describe('SingleFileDiff', () => {
  let mock = new MockAdapter(axios);
  const blobDiffPath = '/mock-path';

  beforeEach(() => {
    mock = new MockAdapter(axios);
    mock.onGet(blobDiffPath).replyOnce(200, { html: `<div class="diff-content">MOCKED</div>` });
  });

  afterEach(() => {
    mock.restore();
  });

  it('loads diff via axios exactly once for collapsed diffs', async () => {
    setHTMLFixture(`
    <div class="diff-file">
      <div class="js-file-title">
        MOCK TITLE
      </div>

      <div class="diff-content">
        <div class="diff-viewer" data-type="simple">
          <div
            class="nothing-here-block diff-collapsed"
            data-diff-for-path="${blobDiffPath}"
          >
            MOCK CONTENT
          </div>
        </div>
      </div>
    </div>
`);

    // Collapsed is the default state
    const diff = new SingleFileDiff(document.querySelector('.diff-file'));
    expect(diff.isOpen).toBe(false);
    expect(diff.content).toBeNull();
    expect(diff.diffForPath).toEqual(blobDiffPath);

    // Opening for the first time
    await diff.toggleDiff($(document.querySelector('.js-file-title')));
    expect(diff.isOpen).toBe(true);
    expect(diff.content).not.toBeNull();

    // Collapsing again
    await diff.toggleDiff($(document.querySelector('.js-file-title')));
    expect(diff.isOpen).toBe(false);
    expect(diff.content).not.toBeNull();

    mock.onGet(blobDiffPath).replyOnce(400, '');

    // Opening again
    await diff.toggleDiff($(document.querySelector('.js-file-title')));
    expect(diff.isOpen).toBe(true);
    expect(diff.content).not.toBeNull();

    expect(mock.history.get.length).toBe(1);
  });

  it('does not load diffs via axios for already expanded diffs', async () => {
    setHTMLFixture(`
    <div class="diff-file">
      <div class="js-file-title">
        MOCK TITLE
      </div>

      <div class="diff-content">
        EXPANDED MOCK CONTENT
      </div>
    </div>
`);

    // Opened is the default state
    const diff = new SingleFileDiff(document.querySelector('.diff-file'));
    expect(diff.isOpen).toBe(true);
    expect(diff.content).not.toBeNull();
    expect(diff.diffForPath).toEqual(undefined);

    // Collapsing for the first time
    await diff.toggleDiff($(document.querySelector('.js-file-title')));
    expect(diff.isOpen).toBe(false);
    expect(diff.content).not.toBeNull();

    // Opening again
    await diff.toggleDiff($(document.querySelector('.js-file-title')));
    expect(diff.isOpen).toBe(true);

    expect(mock.history.get.length).toBe(0);
  });
});