Commit 12d7d348 authored by Thomas Randolph's avatar Thomas Randolph Committed by Phil Hughes

Add a metadata mock

Right now, we don't test the metadata endpoint
really at all. To test it, this mock adds a sample
response that could be paired with the diff_file
mock.
parent aa82f724
...@@ -13,6 +13,7 @@ export default { ...@@ -13,6 +13,7 @@ export default {
blob_name: 'CHANGELOG', blob_name: 'CHANGELOG',
blob_icon: '<i aria-hidden="true" data-hidden="true" class="fa fa-file-text-o fa-fw"></i>', blob_icon: '<i aria-hidden="true" data-hidden="true" class="fa fa-file-text-o fa-fw"></i>',
file_hash: '1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a', file_hash: '1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a',
file_identifier_hash: '928f8286952bda02d674b692addcbe077084663a',
file_path: 'CHANGELOG', file_path: 'CHANGELOG',
new_file: false, new_file: false,
deleted_file: false, deleted_file: false,
......
/* eslint-disable import/prefer-default-export */
/* https://gitlab.com/gitlab-org/frontend/rfcs/-/issues/20 */
export const diffMetadata = {
real_size: '1',
size: 1,
branch_name: 'update-changelog',
source_branch_exists: true,
target_branch_name: 'master',
commit: null,
context_commits: null,
merge_request_diff: {
version_index: null,
created_at: '2019-11-07T06:48:35.202Z',
commits_count: 1,
latest: true,
short_commit_sha: 'eb227b3e',
base_version_path: '/gitlab-org/gitlab-test/-/merge_requests/4/diffs?diff_id=4',
head_version_path: '/gitlab-org/gitlab-test/-/merge_requests/4/diffs?diff_head=true',
version_path: '/gitlab-org/gitlab-test/-/merge_requests/4/diffs?diff_id=4',
compare_path:
'/gitlab-org/gitlab-test/-/merge_requests/4/diffs?diff_id=4\u0026start_sha=eb227b3e214624708c474bdab7bde7afc17cefcc',
},
start_version: null,
latest_diff: true,
latest_version_path: '/gitlab-org/gitlab-test/-/merge_requests/4/diffs',
added_lines: 2,
removed_lines: 0,
render_overflow_warning: false,
email_patch_path: '/gitlab-org/gitlab-test/-/merge_requests/4.patch',
plain_diff_path: '/gitlab-org/gitlab-test/-/merge_requests/4.diff',
merge_request_diffs: [
{
version_index: null,
created_at: '2019-11-07T06:48:35.202Z',
commits_count: 1,
latest: true,
short_commit_sha: 'eb227b3e',
base_version_path: '/gitlab-org/gitlab-test/-/merge_requests/4/diffs?diff_id=4',
head_version_path: '/gitlab-org/gitlab-test/-/merge_requests/4/diffs?diff_head=true',
version_path: '/gitlab-org/gitlab-test/-/merge_requests/4/diffs?diff_id=4',
compare_path:
'/gitlab-org/gitlab-test/-/merge_requests/4/diffs?diff_id=4\u0026start_sha=eb227b3e214624708c474bdab7bde7afc17cefcc',
},
],
diff_files: [
{
added_lines: 2,
removed_lines: 0,
new_path: 'CHANGELOG',
old_path: 'CHANGELOG',
new_file: false,
deleted_file: false,
file_identifier_hash: '928f8286952bda02d674b692addcbe077084663a',
file_hash: '1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a',
},
],
};
...@@ -53,6 +53,7 @@ import * as utils from '~/diffs/store/utils'; ...@@ -53,6 +53,7 @@ import * as utils from '~/diffs/store/utils';
import * as commonUtils from '~/lib/utils/common_utils'; import * as commonUtils from '~/lib/utils/common_utils';
import { mergeUrlParams } from '~/lib/utils/url_utility'; import { mergeUrlParams } from '~/lib/utils/url_utility';
import { useLocalStorageSpy } from 'helpers/local_storage_helper'; import { useLocalStorageSpy } from 'helpers/local_storage_helper';
import { diffMetadata } from '../mock_data/diff_metadata';
import createFlash from '~/flash'; import createFlash from '~/flash';
jest.mock('~/flash', () => jest.fn()); jest.mock('~/flash', () => jest.fn());
...@@ -259,13 +260,19 @@ describe('DiffsStoreActions', () => { ...@@ -259,13 +260,19 @@ describe('DiffsStoreActions', () => {
}); });
describe('fetchDiffFilesMeta', () => { describe('fetchDiffFilesMeta', () => {
it('should fetch diff meta information', done => { const endpointMetadata = '/fetch/diffs_metadata.json?view=inline';
const endpointMetadata = '/fetch/diffs_meta?view=inline'; const noFilesData = { ...diffMetadata };
const mock = new MockAdapter(axios); let mock;
const data = { diff_files: [] };
const res = { data }; beforeEach(() => {
mock.onGet(endpointMetadata).reply(200, res); mock = new MockAdapter(axios);
delete noFilesData.diff_files;
mock.onGet(endpointMetadata).reply(200, diffMetadata);
});
it('should fetch diff meta information', done => {
testAction( testAction(
fetchDiffFilesMeta, fetchDiffFilesMeta,
{}, {},
...@@ -273,8 +280,8 @@ describe('DiffsStoreActions', () => { ...@@ -273,8 +280,8 @@ describe('DiffsStoreActions', () => {
[ [
{ type: types.SET_LOADING, payload: true }, { type: types.SET_LOADING, payload: true },
{ type: types.SET_LOADING, payload: false }, { type: types.SET_LOADING, payload: false },
{ type: types.SET_MERGE_REQUEST_DIFFS, payload: [] }, { type: types.SET_MERGE_REQUEST_DIFFS, payload: diffMetadata.merge_request_diffs },
{ type: types.SET_DIFF_DATA, payload: { data } }, { type: types.SET_DIFF_DATA, payload: noFilesData },
], ],
[], [],
() => { () => {
...@@ -390,13 +397,18 @@ describe('DiffsStoreActions', () => { ...@@ -390,13 +397,18 @@ describe('DiffsStoreActions', () => {
}); });
describe('fetchDiffFilesMeta', () => { describe('fetchDiffFilesMeta', () => {
it('should fetch diff meta information', done => { const endpointMetadata = '/fetch/diffs_metadata.json';
const endpointMetadata = '/fetch/diffs_meta'; const noFilesData = { ...diffMetadata };
const mock = new MockAdapter(axios); let mock;
const data = { diff_files: [] };
const res = { data };
mock.onGet(endpointMetadata).reply(200, res);
beforeEach(() => {
mock = new MockAdapter(axios);
delete noFilesData.diff_files;
mock.onGet(endpointMetadata).reply(200, diffMetadata);
});
it('should fetch diff meta information', done => {
testAction( testAction(
fetchDiffFilesMeta, fetchDiffFilesMeta,
{}, {},
...@@ -404,8 +416,8 @@ describe('DiffsStoreActions', () => { ...@@ -404,8 +416,8 @@ describe('DiffsStoreActions', () => {
[ [
{ type: types.SET_LOADING, payload: true }, { type: types.SET_LOADING, payload: true },
{ type: types.SET_LOADING, payload: false }, { type: types.SET_LOADING, payload: false },
{ type: types.SET_MERGE_REQUEST_DIFFS, payload: [] }, { type: types.SET_MERGE_REQUEST_DIFFS, payload: diffMetadata.merge_request_diffs },
{ type: types.SET_DIFF_DATA, payload: { data } }, { type: types.SET_DIFF_DATA, payload: noFilesData },
], ],
[], [],
() => { () => {
......
...@@ -14,9 +14,11 @@ import { ...@@ -14,9 +14,11 @@ import {
} from '~/diffs/constants'; } from '~/diffs/constants';
import { MERGE_REQUEST_NOTEABLE_TYPE } from '~/notes/constants'; import { MERGE_REQUEST_NOTEABLE_TYPE } from '~/notes/constants';
import diffFileMockData from '../mock_data/diff_file'; import diffFileMockData from '../mock_data/diff_file';
import { diffMetadata } from '../mock_data/diff_metadata';
import { noteableDataMock } from '../../notes/mock_data'; import { noteableDataMock } from '../../notes/mock_data';
const getDiffFileMock = () => JSON.parse(JSON.stringify(diffFileMockData)); const getDiffFileMock = () => JSON.parse(JSON.stringify(diffFileMockData));
const getDiffMetadataMock = () => JSON.parse(JSON.stringify(diffMetadata));
describe('DiffsStoreUtils', () => { describe('DiffsStoreUtils', () => {
describe('findDiffFile', () => { describe('findDiffFile', () => {
...@@ -430,112 +432,177 @@ describe('DiffsStoreUtils', () => { ...@@ -430,112 +432,177 @@ describe('DiffsStoreUtils', () => {
}); });
describe('prepareDiffData', () => { describe('prepareDiffData', () => {
let mock; describe('for regular diff files', () => {
let preparedDiff; let mock;
let splitInlineDiff; let preparedDiff;
let splitParallelDiff; let splitInlineDiff;
let completedDiff; let splitParallelDiff;
let completedDiff;
beforeEach(() => {
mock = getDiffFileMock();
preparedDiff = { diff_files: [mock] };
splitInlineDiff = {
diff_files: [{ ...mock, parallel_diff_lines: undefined }],
};
splitParallelDiff = {
diff_files: [{ ...mock, highlighted_diff_lines: undefined }],
};
completedDiff = {
diff_files: [{ ...mock, highlighted_diff_lines: undefined }],
};
beforeEach(() => { preparedDiff.diff_files = utils.prepareDiffData(preparedDiff);
mock = getDiffFileMock(); splitInlineDiff.diff_files = utils.prepareDiffData(splitInlineDiff);
preparedDiff = { diff_files: [mock] }; splitParallelDiff.diff_files = utils.prepareDiffData(splitParallelDiff);
splitInlineDiff = { completedDiff.diff_files = utils.prepareDiffData(completedDiff, [mock]);
diff_files: [{ ...mock, parallel_diff_lines: undefined }], });
};
splitParallelDiff = {
diff_files: [{ ...mock, highlighted_diff_lines: undefined }],
};
completedDiff = {
diff_files: [{ ...mock, highlighted_diff_lines: undefined }],
};
preparedDiff.diff_files = utils.prepareDiffData(preparedDiff); it('sets the renderIt and collapsed attribute on files', () => {
splitInlineDiff.diff_files = utils.prepareDiffData(splitInlineDiff); const firstParallelDiffLine = preparedDiff.diff_files[0].parallel_diff_lines[2];
splitParallelDiff.diff_files = utils.prepareDiffData(splitParallelDiff);
completedDiff.diff_files = utils.prepareDiffData(completedDiff, [mock]);
});
it('sets the renderIt and collapsed attribute on files', () => { expect(firstParallelDiffLine.left.discussions.length).toBe(0);
const firstParallelDiffLine = preparedDiff.diff_files[0].parallel_diff_lines[2]; expect(firstParallelDiffLine.left).not.toHaveAttr('text');
expect(firstParallelDiffLine.right.discussions.length).toBe(0);
expect(firstParallelDiffLine.right).not.toHaveAttr('text');
const firstParallelChar = firstParallelDiffLine.right.rich_text.charAt(0);
expect(firstParallelDiffLine.left.discussions.length).toBe(0); expect(firstParallelChar).not.toBe(' ');
expect(firstParallelDiffLine.left).not.toHaveAttr('text'); expect(firstParallelChar).not.toBe('+');
expect(firstParallelDiffLine.right.discussions.length).toBe(0); expect(firstParallelChar).not.toBe('-');
expect(firstParallelDiffLine.right).not.toHaveAttr('text');
const firstParallelChar = firstParallelDiffLine.right.rich_text.charAt(0);
expect(firstParallelChar).not.toBe(' '); const checkLine = preparedDiff.diff_files[0].highlighted_diff_lines[0];
expect(firstParallelChar).not.toBe('+');
expect(firstParallelChar).not.toBe('-');
const checkLine = preparedDiff.diff_files[0].highlighted_diff_lines[0]; expect(checkLine.discussions.length).toBe(0);
expect(checkLine).not.toHaveAttr('text');
const firstChar = checkLine.rich_text.charAt(0);
expect(checkLine.discussions.length).toBe(0); expect(firstChar).not.toBe(' ');
expect(checkLine).not.toHaveAttr('text'); expect(firstChar).not.toBe('+');
const firstChar = checkLine.rich_text.charAt(0); expect(firstChar).not.toBe('-');
expect(firstChar).not.toBe(' '); expect(preparedDiff.diff_files[0].renderIt).toBeTruthy();
expect(firstChar).not.toBe('+'); expect(preparedDiff.diff_files[0].collapsed).toBeFalsy();
expect(firstChar).not.toBe('-'); });
expect(preparedDiff.diff_files[0].renderIt).toBeTruthy(); it('adds line_code to all lines', () => {
expect(preparedDiff.diff_files[0].collapsed).toBeFalsy(); expect(
}); preparedDiff.diff_files[0].parallel_diff_lines.filter(line => !line.line_code),
).toHaveLength(0);
});
it('adds line_code to all lines', () => { it('uses right line code if left has none', () => {
expect( const firstLine = preparedDiff.diff_files[0].parallel_diff_lines[0];
preparedDiff.diff_files[0].parallel_diff_lines.filter(line => !line.line_code),
).toHaveLength(0);
});
it('uses right line code if left has none', () => { expect(firstLine.line_code).toEqual(firstLine.right.line_code);
const firstLine = preparedDiff.diff_files[0].parallel_diff_lines[0]; });
expect(firstLine.line_code).toEqual(firstLine.right.line_code); it('guarantees an empty array for both diff styles', () => {
}); expect(splitInlineDiff.diff_files[0].parallel_diff_lines.length).toEqual(0);
expect(splitInlineDiff.diff_files[0].highlighted_diff_lines.length).toBeGreaterThan(0);
expect(splitParallelDiff.diff_files[0].parallel_diff_lines.length).toBeGreaterThan(0);
expect(splitParallelDiff.diff_files[0].highlighted_diff_lines.length).toEqual(0);
});
it('guarantees an empty array for both diff styles', () => { it('merges existing diff files with newly loaded diff files to ensure split diffs are eventually completed', () => {
expect(splitInlineDiff.diff_files[0].parallel_diff_lines.length).toEqual(0); expect(completedDiff.diff_files.length).toEqual(1);
expect(splitInlineDiff.diff_files[0].highlighted_diff_lines.length).toBeGreaterThan(0); expect(completedDiff.diff_files[0].parallel_diff_lines.length).toBeGreaterThan(0);
expect(splitParallelDiff.diff_files[0].parallel_diff_lines.length).toBeGreaterThan(0); expect(completedDiff.diff_files[0].highlighted_diff_lines.length).toBeGreaterThan(0);
expect(splitParallelDiff.diff_files[0].highlighted_diff_lines.length).toEqual(0); });
});
it('merges existing diff files with newly loaded diff files to ensure split diffs are eventually completed', () => { it('leaves files in the existing state', () => {
expect(completedDiff.diff_files.length).toEqual(1); const priorFiles = [mock];
expect(completedDiff.diff_files[0].parallel_diff_lines.length).toBeGreaterThan(0); const fakeNewFile = {
expect(completedDiff.diff_files[0].highlighted_diff_lines.length).toBeGreaterThan(0); ...mock,
}); content_sha: 'ABC',
file_hash: 'DEF',
};
const updatedFilesList = utils.prepareDiffData({ diff_files: [fakeNewFile] }, priorFiles);
it('leaves files in the existing state', () => { expect(updatedFilesList).toEqual([mock, fakeNewFile]);
const priorFiles = [mock]; });
const fakeNewFile = {
...mock,
content_sha: 'ABC',
file_hash: 'DEF',
};
const updatedFilesList = utils.prepareDiffData({ diff_files: [fakeNewFile] }, priorFiles);
expect(updatedFilesList).toEqual([mock, fakeNewFile]); it('completes an existing split diff without overwriting existing diffs', () => {
// The current state has a file that has only loaded inline lines
const priorFiles = [{ ...mock, parallel_diff_lines: [] }];
// The next (batch) load loads two files: the other half of that file, and a new file
const fakeBatch = [
{ ...mock, highlighted_diff_lines: undefined },
{ ...mock, highlighted_diff_lines: undefined, content_sha: 'ABC', file_hash: 'DEF' },
];
const updatedFilesList = utils.prepareDiffData({ diff_files: fakeBatch }, priorFiles);
expect(updatedFilesList).toEqual([
mock,
expect.objectContaining({
content_sha: 'ABC',
file_hash: 'DEF',
}),
]);
});
}); });
it('completes an existing split diff without overwriting existing diffs', () => { describe('for diff metadata', () => {
// The current state has a file that has only loaded inline lines let mock;
const priorFiles = [{ ...mock, parallel_diff_lines: [] }]; let preparedDiffFiles;
// The next (batch) load loads two files: the other half of that file, and a new file
const fakeBatch = [
{ ...mock, highlighted_diff_lines: undefined },
{ ...mock, highlighted_diff_lines: undefined, content_sha: 'ABC', file_hash: 'DEF' },
];
const updatedFilesList = utils.prepareDiffData({ diff_files: fakeBatch }, priorFiles);
expect(updatedFilesList).toEqual([ beforeEach(() => {
mock, mock = getDiffMetadataMock();
expect.objectContaining({
content_sha: 'ABC', preparedDiffFiles = utils.prepareDiffData(mock);
file_hash: 'DEF', });
}),
]); it('sets the renderIt and collapsed attribute on files', () => {
expect(preparedDiffFiles[0].renderIt).toBeTruthy();
expect(preparedDiffFiles[0].collapsed).toBeFalsy();
});
it('guarantees an empty array of lines for both diff styles', () => {
expect(preparedDiffFiles[0].parallel_diff_lines.length).toEqual(0);
expect(preparedDiffFiles[0].highlighted_diff_lines.length).toEqual(0);
});
it('leaves files in the existing state', () => {
const fileMock = getDiffFileMock();
const metaData = getDiffMetadataMock();
const priorFiles = [fileMock];
const updatedFilesList = utils.prepareDiffData(metaData, priorFiles);
expect(updatedFilesList.length).toEqual(2);
expect(updatedFilesList[0]).toEqual(fileMock);
});
it('adds a new file to the file that already exists in state', () => {
// This is actually buggy behavior:
// Because the metadata doesn't include a content_sha,
// the de-duplicator in prepareDiffData doesn't realize it
// should combine these two.
// This buggy behavior hasn't caused a defect YET, because
// `diffs_metadata.json` is only called the first time the
// diffs app starts up, which is:
// - after a fresh page load
// - after you switch to the changes tab *the first time*
// This test should begin FAILING and can be reversed to check
// for just a single file when this is implemented:
// https://gitlab.com/groups/gitlab-org/-/epics/2852#note_304803233
const fileMock = getDiffFileMock();
const metaMock = getDiffMetadataMock();
const priorFiles = [{ ...fileMock }];
const updatedFilesList = utils.prepareDiffData(metaMock, priorFiles);
expect(updatedFilesList).toEqual([
fileMock,
{
...metaMock.diff_files[0],
highlighted_diff_lines: [],
parallel_diff_lines: [],
},
]);
});
}); });
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment