diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-20 21:08:51 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-20 21:08:51 +0300 |
commit | df2eda3f14dccb703bd7054d4ddde7803cb1fe7e (patch) | |
tree | 0200bb0def01cde22da3bc4c9ed0a7b0f91d6b50 /spec/javascripts/diffs | |
parent | b9bac6dbf78a5a7976fba14aaeef96bdeb0da612 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/javascripts/diffs')
-rw-r--r-- | spec/javascripts/diffs/components/diff_expansion_cell_spec.js | 203 | ||||
-rw-r--r-- | spec/javascripts/diffs/store/mutations_spec.js | 3 | ||||
-rw-r--r-- | spec/javascripts/diffs/store/utils_spec.js | 124 |
3 files changed, 272 insertions, 58 deletions
diff --git a/spec/javascripts/diffs/components/diff_expansion_cell_spec.js b/spec/javascripts/diffs/components/diff_expansion_cell_spec.js index e8ff6778512..9a5048d9332 100644 --- a/spec/javascripts/diffs/components/diff_expansion_cell_spec.js +++ b/spec/javascripts/diffs/components/diff_expansion_cell_spec.js @@ -1,64 +1,229 @@ import Vue from 'vue'; +import { cloneDeep } from 'lodash'; import { createComponentWithStore } from 'spec/helpers/vue_mount_component_helper'; import { createStore } from '~/mr_notes/stores'; import DiffExpansionCell from '~/diffs/components/diff_expansion_cell.vue'; +import { getPreviousLineIndex } from '~/diffs/store/utils'; +import { INLINE_DIFF_VIEW_TYPE, PARALLEL_DIFF_VIEW_TYPE } from '~/diffs/constants'; import diffFileMockData from '../mock_data/diff_file'; const EXPAND_UP_CLASS = '.js-unfold'; const EXPAND_DOWN_CLASS = '.js-unfold-down'; const EXPAND_ALL_CLASS = '.js-unfold-all'; +const LINE_TO_USE = 5; +const lineSources = { + [INLINE_DIFF_VIEW_TYPE]: 'highlighted_diff_lines', + [PARALLEL_DIFF_VIEW_TYPE]: 'parallel_diff_lines', +}; +const lineHandlers = { + [INLINE_DIFF_VIEW_TYPE]: line => line, + [PARALLEL_DIFF_VIEW_TYPE]: line => line.right || line.left, +}; + +function makeLoadMoreLinesPayload({ + sinceLine, + toLine, + oldLineNumber, + diffViewType, + fileHash, + nextLineNumbers = {}, + unfold = false, + bottom = false, + isExpandDown = false, +}) { + return { + endpoint: 'contextLinesPath', + params: { + since: sinceLine, + to: toLine, + offset: toLine + 1 - oldLineNumber, + view: diffViewType, + unfold, + bottom, + }, + lineNumbers: { + oldLineNumber, + newLineNumber: toLine + 1, + }, + nextLineNumbers, + fileHash, + isExpandDown, + }; +} + +function getLine(file, type, index) { + const source = lineSources[type]; + const handler = lineHandlers[type]; + + return handler(file[source][index]); +} describe('DiffExpansionCell', () => { - const matchLine = diffFileMockData.highlighted_diff_lines[5]; + let mockFile; + let mockLine; + let store; + let vm; + + beforeEach(() => { + mockFile = cloneDeep(diffFileMockData); + mockLine = getLine(mockFile, INLINE_DIFF_VIEW_TYPE, LINE_TO_USE); + store = createStore(); + store.state.diffs.diffFiles = [mockFile]; + spyOn(store, 'dispatch').and.returnValue(Promise.resolve()); + }); const createComponent = (options = {}) => { const cmp = Vue.extend(DiffExpansionCell); const defaults = { - fileHash: diffFileMockData.file_hash, + fileHash: mockFile.file_hash, contextLinesPath: 'contextLinesPath', - line: matchLine, + line: mockLine, isTop: false, isBottom: false, }; const props = Object.assign({}, defaults, options); - return createComponentWithStore(cmp, createStore(), props).$mount(); + vm = createComponentWithStore(cmp, store, props).$mount(); }; + const findExpandUp = () => vm.$el.querySelector(EXPAND_UP_CLASS); + const findExpandDown = () => vm.$el.querySelector(EXPAND_DOWN_CLASS); + const findExpandAll = () => vm.$el.querySelector(EXPAND_ALL_CLASS); + describe('top row', () => { it('should have "expand up" and "show all" option', () => { - const vm = createComponent({ + createComponent({ isTop: true, }); - const el = vm.$el; - expect(el.querySelector(EXPAND_UP_CLASS)).not.toBe(null); - expect(el.querySelector(EXPAND_DOWN_CLASS)).toBe(null); - expect(el.querySelector(EXPAND_ALL_CLASS)).not.toBe(null); + expect(findExpandUp()).not.toBe(null); + expect(findExpandDown()).toBe(null); + expect(findExpandAll()).not.toBe(null); }); }); describe('middle row', () => { it('should have "expand down", "show all", "expand up" option', () => { - const vm = createComponent(); - const el = vm.$el; + createComponent(); - expect(el.querySelector(EXPAND_UP_CLASS)).not.toBe(null); - expect(el.querySelector(EXPAND_DOWN_CLASS)).not.toBe(null); - expect(el.querySelector(EXPAND_ALL_CLASS)).not.toBe(null); + expect(findExpandUp()).not.toBe(null); + expect(findExpandDown()).not.toBe(null); + expect(findExpandAll()).not.toBe(null); }); }); describe('bottom row', () => { it('should have "expand down" and "show all" option', () => { - const vm = createComponent({ + createComponent({ isBottom: true, }); - const el = vm.$el; - expect(el.querySelector(EXPAND_UP_CLASS)).toBe(null); - expect(el.querySelector(EXPAND_DOWN_CLASS)).not.toBe(null); - expect(el.querySelector(EXPAND_ALL_CLASS)).not.toBe(null); + expect(findExpandUp()).toBe(null); + expect(findExpandDown()).not.toBe(null); + expect(findExpandAll()).not.toBe(null); + }); + }); + + describe('any row', () => { + [ + { diffViewType: INLINE_DIFF_VIEW_TYPE, file: { parallel_diff_lines: [] } }, + { diffViewType: PARALLEL_DIFF_VIEW_TYPE, file: { highlighted_diff_lines: [] } }, + ].forEach(({ diffViewType, file }) => { + describe(`with diffViewType (${diffViewType})`, () => { + beforeEach(() => { + mockLine = getLine(mockFile, diffViewType, LINE_TO_USE); + store.state.diffs.diffFiles = [{ ...mockFile, ...file }]; + store.state.diffs.diffViewType = diffViewType; + }); + + it('does not initially dispatch anything', () => { + expect(store.dispatch).not.toHaveBeenCalled(); + }); + + it('on expand all clicked, dispatch loadMoreLines', () => { + const oldLineNumber = mockLine.meta_data.old_pos; + const newLineNumber = mockLine.meta_data.new_pos; + const previousIndex = getPreviousLineIndex(diffViewType, mockFile, { + oldLineNumber, + newLineNumber, + }); + + createComponent(); + + findExpandAll().click(); + + expect(store.dispatch).toHaveBeenCalledWith( + 'diffs/loadMoreLines', + makeLoadMoreLinesPayload({ + fileHash: mockFile.file_hash, + toLine: newLineNumber - 1, + sinceLine: previousIndex, + oldLineNumber, + diffViewType, + }), + ); + }); + + it('on expand up clicked, dispatch loadMoreLines', () => { + mockLine.meta_data.old_pos = 200; + mockLine.meta_data.new_pos = 200; + + const oldLineNumber = mockLine.meta_data.old_pos; + const newLineNumber = mockLine.meta_data.new_pos; + + createComponent(); + + findExpandUp().click(); + + expect(store.dispatch).toHaveBeenCalledWith( + 'diffs/loadMoreLines', + makeLoadMoreLinesPayload({ + fileHash: mockFile.file_hash, + toLine: newLineNumber - 1, + sinceLine: 179, + oldLineNumber, + diffViewType, + unfold: true, + }), + ); + }); + + it('on expand down clicked, dispatch loadMoreLines', () => { + mockFile[lineSources[diffViewType]][LINE_TO_USE + 1] = cloneDeep( + mockFile[lineSources[diffViewType]][LINE_TO_USE], + ); + const nextLine = getLine(mockFile, diffViewType, LINE_TO_USE + 1); + + nextLine.meta_data.old_pos = 300; + nextLine.meta_data.new_pos = 300; + mockLine.meta_data.old_pos = 200; + mockLine.meta_data.new_pos = 200; + + createComponent(); + + findExpandDown().click(); + + expect(store.dispatch).toHaveBeenCalledWith('diffs/loadMoreLines', { + endpoint: 'contextLinesPath', + params: { + since: 1, + to: 21, // the load amount, plus 1 line + offset: 0, + view: diffViewType, + unfold: true, + bottom: true, + }, + lineNumbers: { + // when expanding down, these are based on the previous line, 0, in this case + oldLineNumber: 0, + newLineNumber: 0, + }, + nextLineNumbers: { old_line: 200, new_line: 200 }, + fileHash: mockFile.file_hash, + isExpandDown: true, + }); + }); + }); }); }); }); diff --git a/spec/javascripts/diffs/store/mutations_spec.js b/spec/javascripts/diffs/store/mutations_spec.js index cb89a89e216..ffe5d89e615 100644 --- a/spec/javascripts/diffs/store/mutations_spec.js +++ b/spec/javascripts/diffs/store/mutations_spec.js @@ -167,7 +167,7 @@ describe('DiffsStoreMutations', () => { highlighted_diff_lines: [], parallel_diff_lines: [], }; - const state = { diffFiles: [diffFile] }; + const state = { diffFiles: [diffFile], diffViewType: 'viewType' }; const lines = [{ old_line: 1, new_line: 1 }]; const findDiffFileSpy = spyOnDependency(mutations, 'findDiffFile').and.returnValue(diffFile); @@ -195,6 +195,7 @@ describe('DiffsStoreMutations', () => { expect(addContextLinesSpy).toHaveBeenCalledWith({ inlineLines: diffFile.highlighted_diff_lines, parallelLines: diffFile.parallel_diff_lines, + diffViewType: 'viewType', contextLines: options.contextLines, bottom: options.params.bottom, lineNumbers: options.lineNumbers, diff --git a/spec/javascripts/diffs/store/utils_spec.js b/spec/javascripts/diffs/store/utils_spec.js index 051820cedfa..223c4d7e40b 100644 --- a/spec/javascripts/diffs/store/utils_spec.js +++ b/spec/javascripts/diffs/store/utils_spec.js @@ -1,3 +1,4 @@ +import { clone } from 'lodash'; import * as utils from '~/diffs/store/utils'; import { LINE_POSITION_LEFT, @@ -8,6 +9,7 @@ import { NEW_LINE_TYPE, OLD_LINE_TYPE, MATCH_LINE_TYPE, + INLINE_DIFF_VIEW_TYPE, PARALLEL_DIFF_VIEW_TYPE, } from '~/diffs/constants'; import { MERGE_REQUEST_NOTEABLE_TYPE } from '~/notes/constants'; @@ -47,7 +49,38 @@ describe('DiffsStoreUtils', () => { describe('findIndexInParallelLines', () => { it('should return correct index for given line numbers', () => { - expectSet(utils.findIndexInParallelLines, getDiffFileMock().parallel_diff_lines, {}); + expectSet(utils.findIndexInParallelLines, getDiffFileMock().parallel_diff_lines, []); + }); + }); + }); + + describe('getPreviousLineIndex', () => { + [ + { diffViewType: INLINE_DIFF_VIEW_TYPE, file: { parallel_diff_lines: [] } }, + { diffViewType: PARALLEL_DIFF_VIEW_TYPE, file: { highlighted_diff_lines: [] } }, + ].forEach(({ diffViewType, file }) => { + describe(`with diffViewType (${diffViewType}) in split diffs`, () => { + let diffFile; + + beforeEach(() => { + diffFile = { ...clone(diffFileMockData), ...file }; + }); + + it('should return the correct previous line number', () => { + const emptyLines = + diffViewType === INLINE_DIFF_VIEW_TYPE + ? diffFile.parallel_diff_lines + : diffFile.highlighted_diff_lines; + + // This expectation asserts that we cannot possibly be using the opposite view type lines in the next expectation + expect(emptyLines.length).toBe(0); + expect( + utils.getPreviousLineIndex(diffViewType, diffFile, { + oldLineNumber: 3, + newLineNumber: 5, + }), + ).toBe(4); + }); }); }); }); @@ -80,44 +113,59 @@ describe('DiffsStoreUtils', () => { }); describe('addContextLines', () => { - it('should add context lines', () => { - const diffFile = getDiffFileMock(); - const inlineLines = diffFile.highlighted_diff_lines; - const parallelLines = diffFile.parallel_diff_lines; - const lineNumbers = { oldLineNumber: 3, newLineNumber: 5 }; - const contextLines = [{ lineNumber: 42, line_code: '123' }]; - const options = { inlineLines, parallelLines, contextLines, lineNumbers }; - const inlineIndex = utils.findIndexInInlineLines(inlineLines, lineNumbers); - const parallelIndex = utils.findIndexInParallelLines(parallelLines, lineNumbers); - const normalizedParallelLine = { - left: options.contextLines[0], - right: options.contextLines[0], - line_code: '123', - }; - - utils.addContextLines(options); - - expect(inlineLines[inlineIndex]).toEqual(contextLines[0]); - expect(parallelLines[parallelIndex]).toEqual(normalizedParallelLine); - }); - - it('should add context lines properly with bottom parameter', () => { - const diffFile = getDiffFileMock(); - const inlineLines = diffFile.highlighted_diff_lines; - const parallelLines = diffFile.parallel_diff_lines; - const lineNumbers = { oldLineNumber: 3, newLineNumber: 5 }; - const contextLines = [{ lineNumber: 42, line_code: '123' }]; - const options = { inlineLines, parallelLines, contextLines, lineNumbers, bottom: true }; - const normalizedParallelLine = { - left: options.contextLines[0], - right: options.contextLines[0], - line_code: '123', - }; - - utils.addContextLines(options); + [INLINE_DIFF_VIEW_TYPE, PARALLEL_DIFF_VIEW_TYPE].forEach(diffViewType => { + it(`should add context lines for ${diffViewType}`, () => { + const diffFile = getDiffFileMock(); + const inlineLines = diffFile.highlighted_diff_lines; + const parallelLines = diffFile.parallel_diff_lines; + const lineNumbers = { oldLineNumber: 3, newLineNumber: 5 }; + const contextLines = [{ lineNumber: 42, line_code: '123' }]; + const options = { inlineLines, parallelLines, contextLines, lineNumbers, diffViewType }; + const inlineIndex = utils.findIndexInInlineLines(inlineLines, lineNumbers); + const parallelIndex = utils.findIndexInParallelLines(parallelLines, lineNumbers); + const normalizedParallelLine = { + left: options.contextLines[0], + right: options.contextLines[0], + line_code: '123', + }; + + utils.addContextLines(options); + + if (diffViewType === INLINE_DIFF_VIEW_TYPE) { + expect(inlineLines[inlineIndex]).toEqual(contextLines[0]); + } else { + expect(parallelLines[parallelIndex]).toEqual(normalizedParallelLine); + } + }); - expect(inlineLines[inlineLines.length - 1]).toEqual(contextLines[0]); - expect(parallelLines[parallelLines.length - 1]).toEqual(normalizedParallelLine); + it(`should add context lines properly with bottom parameter for ${diffViewType}`, () => { + const diffFile = getDiffFileMock(); + const inlineLines = diffFile.highlighted_diff_lines; + const parallelLines = diffFile.parallel_diff_lines; + const lineNumbers = { oldLineNumber: 3, newLineNumber: 5 }; + const contextLines = [{ lineNumber: 42, line_code: '123' }]; + const options = { + inlineLines, + parallelLines, + contextLines, + lineNumbers, + bottom: true, + diffViewType, + }; + const normalizedParallelLine = { + left: options.contextLines[0], + right: options.contextLines[0], + line_code: '123', + }; + + utils.addContextLines(options); + + if (diffViewType === INLINE_DIFF_VIEW_TYPE) { + expect(inlineLines[inlineLines.length - 1]).toEqual(contextLines[0]); + } else { + expect(parallelLines[parallelLines.length - 1]).toEqual(normalizedParallelLine); + } + }); }); }); |