summaryrefslogtreecommitdiff
path: root/spec/frontend/single_file_diff_spec.js
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2021-03-16 18:18:33 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2021-03-16 18:18:33 +0000
commitf64a639bcfa1fc2bc89ca7db268f594306edfd7c (patch)
treea2c3c2ebcc3b45e596949db485d6ed18ffaacfa1 /spec/frontend/single_file_diff_spec.js
parentbfbc3e0d6583ea1a91f627528bedc3d65ba4b10f (diff)
downloadgitlab-ce-f64a639bcfa1fc2bc89ca7db268f594306edfd7c.tar.gz
Add latest changes from gitlab-org/gitlab@13-10-stable-eev13.10.0-rc40
Diffstat (limited to 'spec/frontend/single_file_diff_spec.js')
-rw-r--r--spec/frontend/single_file_diff_spec.js96
1 files changed, 96 insertions, 0 deletions
diff --git a/spec/frontend/single_file_diff_spec.js b/spec/frontend/single_file_diff_spec.js
new file mode 100644
index 00000000000..8718152655f
--- /dev/null
+++ b/spec/frontend/single_file_diff_spec.js
@@ -0,0 +1,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);
+ });
+});