2021-04-17 20:07:23 +05:30
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import $ from 'jquery';
|
2022-07-16 23:28:13 +05:30
|
|
|
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
|
2021-04-17 20:07:23 +05:30
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2023-04-23 21:23:45 +05:30
|
|
|
import { HTTP_STATUS_BAD_REQUEST, HTTP_STATUS_OK } from '~/lib/utils/http_status';
|
2021-04-17 20:07:23 +05:30
|
|
|
import SingleFileDiff from '~/single_file_diff';
|
|
|
|
|
|
|
|
describe('SingleFileDiff', () => {
|
|
|
|
let mock = new MockAdapter(axios);
|
|
|
|
const blobDiffPath = '/mock-path';
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
2023-04-23 21:23:45 +05:30
|
|
|
mock
|
|
|
|
.onGet(blobDiffPath)
|
|
|
|
.replyOnce(HTTP_STATUS_OK, { html: `<div class="diff-content">MOCKED</div>` });
|
2021-04-17 20:07:23 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
2022-07-16 23:28:13 +05:30
|
|
|
resetHTMLFixture();
|
2021-04-17 20:07:23 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
mock.onGet(blobDiffPath).replyOnce(HTTP_STATUS_BAD_REQUEST, '');
|
2021-04-17 20:07:23 +05:30
|
|
|
|
|
|
|
// 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);
|
|
|
|
});
|
|
|
|
|
2023-07-07 10:43:13 +05:30
|
|
|
it('ignores user-defined diff path attributes', () => {
|
|
|
|
setHTMLFixture(`
|
|
|
|
<div class="diff-file">
|
|
|
|
<div class="diff-content">
|
|
|
|
<div class="diff-viewer" data-type="simple">
|
|
|
|
<div class="note-text"><a data-diff-for-path="test/note/path">Test note</a></div>
|
|
|
|
<div data-diff-for-path="${blobDiffPath}">MOCK CONTENT</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`);
|
|
|
|
const { diffForPath } = new SingleFileDiff(document.querySelector('.diff-file'));
|
|
|
|
expect(diffForPath).toEqual(blobDiffPath);
|
|
|
|
});
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|