forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff-component-spec.js
More file actions
112 lines (93 loc) · 4.76 KB
/
Copy pathdiff-component-spec.js
File metadata and controls
112 lines (93 loc) · 4.76 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/** @babel */
import fs from 'fs'
import path from 'path'
import DiffViewModel from '../lib/diff-view-model'
import DiffComponent from '../lib/diff-component'
import FileDiff from '../lib/file-diff'
import FileList from '../lib/file-list'
import {createFileDiffsFromPath, buildMouseEvent} from './helpers'
function createDiffs(filePath) {
let fileDiffs = createFileDiffsFromPath(filePath)
return new DiffViewModel({fileList: new FileList(fileDiffs)})
}
describe("DiffComponent", function() {
let viewModel, component, element
function getLineNumberAtPosition(position) {
let [fileIndex, hunkIndex, lineIndex] = position
let fileElement = element.querySelectorAll('.git-file-diff')[fileIndex]
let hunkElement = fileElement.querySelectorAll('.git-diff-hunk')[hunkIndex]
let lineElement = hunkElement.querySelectorAll('.git-hunk-line')[lineIndex + 1] // account for header
return lineElement.querySelector('.old-line-number')
}
beforeEach(function() {
viewModel = createDiffs('fixtures/two-file-diff.txt')
component = new DiffComponent({diffViewModel: viewModel})
element = component.element
jasmine.attachToDOM(component.element)
})
it("renders correctly", function() {
expect(element.querySelectorAll('.git-file-diff')).toHaveLength(2)
expect(element.querySelector('.git-diff-hunk')).toBeDefined()
expect(element.querySelector('.git-hunk-line')).toBeDefined()
})
describe("mouse selection of diff lines", function() {
it("selects a line when clicked", function() {
let selection = viewModel.getLastSelection()
expect(selection.getMode()).toBe('hunk')
let line = getLineNumberAtPosition([0, 0, 4])
line.dispatchEvent(buildMouseEvent('mousedown', { target: line }))
line.dispatchEvent(buildMouseEvent('mouseup', { target: line }))
selection = viewModel.getLastSelection()
expect(selection.getMode()).toBe('line')
expect(selection.getHeadPosition()).toEqual([0, 0, 4])
expect(selection.getTailPosition()).toEqual([0, 0, 4])
})
it("selects multiple lines when dragged", function() {
let selection
let line1 = getLineNumberAtPosition([0, 0, 4])
let line2 = getLineNumberAtPosition([0, 0, 5])
line1.dispatchEvent(buildMouseEvent('mousedown', { target: line1 }))
line1.dispatchEvent(buildMouseEvent('mousemove', { target: line1 }))
line2.dispatchEvent(buildMouseEvent('mousemove', { target: line2 }))
line2.dispatchEvent(buildMouseEvent('mouseup', { target: line2 }))
selection = viewModel.getLastSelection()
expect(selection.getMode()).toBe('line')
expect(selection.getHeadPosition()).toEqual([0, 0, 4])
expect(selection.getTailPosition()).toEqual([0, 0, 5])
})
it("creates a new selection when dragging a new part of the gutter", function() {
let selection
let lineOld = getLineNumberAtPosition([0, 0, 4])
lineOld.dispatchEvent(buildMouseEvent('mousedown', { target: lineOld }))
lineOld.dispatchEvent(buildMouseEvent('mouseup', { target: lineOld }))
let line1 = getLineNumberAtPosition([0, 1, 3])
let line2 = getLineNumberAtPosition([0, 1, 4])
line1.dispatchEvent(buildMouseEvent('mousedown', { target: line1 }))
line1.dispatchEvent(buildMouseEvent('mousemove', { target: line1 }))
line2.dispatchEvent(buildMouseEvent('mousemove', { target: line2 }))
line2.dispatchEvent(buildMouseEvent('mouseup', { target: line2 }))
expect(viewModel.getSelections()).toHaveLength(1)
selection = viewModel.getLastSelection()
expect(selection.getMode()).toBe('line')
expect(selection.getHeadPosition()).toEqual([0, 1, 3])
expect(selection.getTailPosition()).toEqual([0, 1, 4])
})
it("adds to the selection when dragging a new part of the gutter with the shift key down", function() {
let selection
let lineOld = getLineNumberAtPosition([0, 0, 4])
lineOld.dispatchEvent(buildMouseEvent('mousedown', { target: lineOld }))
lineOld.dispatchEvent(buildMouseEvent('mouseup', { target: lineOld }))
let line1 = getLineNumberAtPosition([0, 1, 3])
let line2 = getLineNumberAtPosition([0, 1, 4])
line1.dispatchEvent(buildMouseEvent('mousedown', { target: line1, shiftKey: true }))
line1.dispatchEvent(buildMouseEvent('mousemove', { target: line1, shiftKey: true }))
line2.dispatchEvent(buildMouseEvent('mousemove', { target: line2, shiftKey: true }))
line2.dispatchEvent(buildMouseEvent('mouseup', { target: line2, shiftKey: true }))
expect(viewModel.getSelections()).toHaveLength(2)
selection = viewModel.getLastSelection()
expect(selection.getMode()).toBe('line')
expect(selection.getHeadPosition()).toEqual([0, 1, 3])
expect(selection.getTailPosition()).toEqual([0, 1, 4])
})
})
})