|
| 1 | +import expect from 'expect' |
| 2 | +import { |
| 3 | + getAllBlameDecorations, |
| 4 | + getBlameDecorations, |
| 5 | + getBlameDecorationsForSelections, |
| 6 | + getDecorationFromHunk, |
| 7 | + Hunk |
| 8 | +} from './blame' |
| 9 | +import { createMockSourcegraphAPI } from './util/stubs' |
| 10 | + |
| 11 | +const FIXTURE_HUNK_1: Hunk = { |
| 12 | + startLine: 1, |
| 13 | + endLine: 2, |
| 14 | + author: { |
| 15 | + person: { |
| 16 | + displayName: 'a' |
| 17 | + }, |
| 18 | + date: '2018-09-10T21:52:45Z' |
| 19 | + }, |
| 20 | + rev: 'b', |
| 21 | + message: 'c', |
| 22 | + commit: { |
| 23 | + url: 'd' |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +const FIXTURE_HUNK_2: Hunk = { |
| 28 | + startLine: 2, |
| 29 | + endLine: 3, |
| 30 | + author: { |
| 31 | + person: { |
| 32 | + displayName: 'e' |
| 33 | + }, |
| 34 | + date: '2018-11-10T21:52:45Z' |
| 35 | + }, |
| 36 | + rev: 'f', |
| 37 | + message: 'g', |
| 38 | + commit: { |
| 39 | + url: 'h' |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +const FIXTURE_HUNK_3: Hunk = { |
| 44 | + startLine: 3, |
| 45 | + endLine: 4, |
| 46 | + author: { |
| 47 | + person: { |
| 48 | + displayName: 'i' |
| 49 | + }, |
| 50 | + date: '2018-10-10T21:52:45Z' |
| 51 | + }, |
| 52 | + rev: 'j', |
| 53 | + message: 'k', |
| 54 | + commit: { |
| 55 | + url: 'l' |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +const NOW = +new Date('2018-12-01T21:52:45Z') |
| 60 | + |
| 61 | +const SOURCEGRAPH = createMockSourcegraphAPI() |
| 62 | + |
| 63 | +describe('getDecorationsFromHunk()', () => { |
| 64 | + |
| 65 | + it('creates a TextDocumentDecoration from a Hunk', () => { |
| 66 | + expect(getDecorationFromHunk(FIXTURE_HUNK_1, NOW, 0, SOURCEGRAPH as any)).toEqual({ |
| 67 | + after: { |
| 68 | + contentText: 'a, 3 months ago: • c', |
| 69 | + dark: { |
| 70 | + backgroundColor: 'rgba(15, 43, 89, 0.65)', |
| 71 | + color: 'rgba(235, 235, 255, 0.55)', |
| 72 | + }, |
| 73 | + hoverMessage: 'c', |
| 74 | + light: { |
| 75 | + backgroundColor: 'rgba(193, 217, 255, 0.65)', |
| 76 | + color: 'rgba(0, 0, 25, 0.55)', |
| 77 | + }, |
| 78 | + linkURL: 'https://sourcegraph.test/d', |
| 79 | + }, |
| 80 | + isWholeLine: true, |
| 81 | + range: { |
| 82 | + end: 0, |
| 83 | + start: 0, |
| 84 | + } |
| 85 | + } |
| 86 | + ) |
| 87 | + }) |
| 88 | + |
| 89 | + it('truncates long commit messsages', () => { |
| 90 | + const decoration = getDecorationFromHunk({ |
| 91 | + ...FIXTURE_HUNK_1, |
| 92 | + message: 'asdgjdsag asdklgbasdghladg asdgjlhbasdgjlhabsdg asdgilbadsgiobasgd' |
| 93 | + }, NOW, 0, SOURCEGRAPH as any) |
| 94 | + expect(decoration.after && decoration.after.contentText).toEqual('a, 3 months ago: • asdgjdsag asdklgbasdghladg asdgjlhbasdgjlhabs…') |
| 95 | + }) |
| 96 | + |
| 97 | + it('truncates long display names', () => { |
| 98 | + const decoration = getDecorationFromHunk({ |
| 99 | + ...FIXTURE_HUNK_1, |
| 100 | + author: { |
| 101 | + person: { |
| 102 | + displayName: 'asdgjdsag asdklgbasdghladg asdgjlhbasdgjlhabsdg asdgilbadsgiobasgd' |
| 103 | + }, |
| 104 | + date:'2018-09-10T21:52:45Z' |
| 105 | + } |
| 106 | + }, NOW, 0, SOURCEGRAPH as any) |
| 107 | + expect(decoration.after && decoration.after.contentText).toEqual('asdgjdsag asdklgbasdghlad…, 3 months ago: • c') |
| 108 | + }) |
| 109 | +}) |
| 110 | + |
| 111 | +describe('getBlameDecorationsForSelections()', () => { |
| 112 | + |
| 113 | + it('adds decorations only for hunks that are within the selections', () => { |
| 114 | + const decorations = getBlameDecorationsForSelections([FIXTURE_HUNK_1, FIXTURE_HUNK_2, FIXTURE_HUNK_3], [ |
| 115 | + new SOURCEGRAPH.Selection(new SOURCEGRAPH.Position(1, 0), new SOURCEGRAPH.Position(1, 0)) as any |
| 116 | + ], NOW, SOURCEGRAPH as any) |
| 117 | + expect(decorations).toEqual([ |
| 118 | + getDecorationFromHunk(FIXTURE_HUNK_2, NOW, 1, SOURCEGRAPH as any) |
| 119 | + ]) |
| 120 | + }) |
| 121 | + |
| 122 | + it('handles multiple selections', () => { |
| 123 | + const decorations = getBlameDecorationsForSelections([FIXTURE_HUNK_1, FIXTURE_HUNK_2, FIXTURE_HUNK_3], [ |
| 124 | + new SOURCEGRAPH.Selection(new SOURCEGRAPH.Position(1, 0), new SOURCEGRAPH.Position(1, 0)) as any, |
| 125 | + new SOURCEGRAPH.Selection(new SOURCEGRAPH.Position(2, 0), new SOURCEGRAPH.Position(5, 0)) as any, |
| 126 | + new SOURCEGRAPH.Selection(new SOURCEGRAPH.Position(6, 0), new SOURCEGRAPH.Position(10, 0)) as any, |
| 127 | + ], NOW, SOURCEGRAPH as any) |
| 128 | + expect(decorations).toEqual([ |
| 129 | + getDecorationFromHunk(FIXTURE_HUNK_2, NOW, 1, SOURCEGRAPH as any), |
| 130 | + getDecorationFromHunk(FIXTURE_HUNK_3, NOW, 2, SOURCEGRAPH as any), |
| 131 | + ]) |
| 132 | + }) |
| 133 | + |
| 134 | + it('handles multiple hunks per selection', () => { |
| 135 | + const decorations = getBlameDecorationsForSelections([FIXTURE_HUNK_1, FIXTURE_HUNK_2, FIXTURE_HUNK_3], [ |
| 136 | + new SOURCEGRAPH.Selection(new SOURCEGRAPH.Position(0, 0), new SOURCEGRAPH.Position(5, 0)) as any |
| 137 | + ], NOW, SOURCEGRAPH as any) |
| 138 | + expect(decorations).toEqual([ |
| 139 | + getDecorationFromHunk(FIXTURE_HUNK_1, NOW, 0, SOURCEGRAPH as any), |
| 140 | + getDecorationFromHunk(FIXTURE_HUNK_2, NOW, 1, SOURCEGRAPH as any), |
| 141 | + getDecorationFromHunk(FIXTURE_HUNK_3, NOW, 2, SOURCEGRAPH as any), |
| 142 | + ]) |
| 143 | + }) |
| 144 | + |
| 145 | + it('decorates the start line of the selection if the start line of the hunk is outside of the selection boundaries', () => { |
| 146 | + const decorations = getBlameDecorationsForSelections([{ |
| 147 | + ...FIXTURE_HUNK_1, |
| 148 | + startLine: 1, |
| 149 | + endLine: 10 |
| 150 | + }], [ |
| 151 | + new SOURCEGRAPH.Selection(new SOURCEGRAPH.Position(2, 0), new SOURCEGRAPH.Position(2, 0)) as any |
| 152 | + ], NOW, SOURCEGRAPH as any) |
| 153 | + expect(decorations).toEqual([ |
| 154 | + getDecorationFromHunk(FIXTURE_HUNK_1, NOW, 2, SOURCEGRAPH as any), |
| 155 | + ]) |
| 156 | + }) |
| 157 | + |
| 158 | +}) |
| 159 | + |
| 160 | +describe('getAllBlameDecorations()', () => { |
| 161 | + |
| 162 | + it('adds decorations for all hunks', () => { |
| 163 | + expect(getAllBlameDecorations([ |
| 164 | + FIXTURE_HUNK_1, |
| 165 | + FIXTURE_HUNK_2, |
| 166 | + FIXTURE_HUNK_3 |
| 167 | + ], NOW, SOURCEGRAPH as any)).toEqual([ |
| 168 | + getDecorationFromHunk(FIXTURE_HUNK_1, NOW, 0, SOURCEGRAPH as any), |
| 169 | + getDecorationFromHunk(FIXTURE_HUNK_2, NOW, 1, SOURCEGRAPH as any), |
| 170 | + getDecorationFromHunk(FIXTURE_HUNK_3, NOW, 2, SOURCEGRAPH as any), |
| 171 | + |
| 172 | + ]) |
| 173 | + }) |
| 174 | + |
| 175 | +}) |
| 176 | + |
| 177 | +describe('getBlameDecorations()', () => { |
| 178 | + |
| 179 | + it('gets no decorations if git.blame.lineDecorations is false', async () => { |
| 180 | + expect(await getBlameDecorations({ |
| 181 | + uri: 'a', |
| 182 | + settings: { |
| 183 | + 'git.blame.lineDecorations': false |
| 184 | + }, |
| 185 | + now: NOW, |
| 186 | + selections: null, |
| 187 | + queryHunks: () => Promise.resolve([FIXTURE_HUNK_1, FIXTURE_HUNK_2, FIXTURE_HUNK_3]), |
| 188 | + sourcegraph: SOURCEGRAPH as any |
| 189 | + })).toEqual([]) |
| 190 | + }) |
| 191 | + |
| 192 | + it('gets decorations for all hunks if no selections are passed', async () => { |
| 193 | + expect(await getBlameDecorations({ |
| 194 | + uri: 'a', |
| 195 | + settings: { |
| 196 | + 'git.blame.lineDecorations': true |
| 197 | + }, |
| 198 | + now: NOW, |
| 199 | + selections: null, |
| 200 | + queryHunks: () => Promise.resolve([FIXTURE_HUNK_1, FIXTURE_HUNK_2, FIXTURE_HUNK_3]), |
| 201 | + sourcegraph: SOURCEGRAPH as any |
| 202 | + })).toEqual([ |
| 203 | + getDecorationFromHunk(FIXTURE_HUNK_1, NOW, 0, SOURCEGRAPH as any), |
| 204 | + getDecorationFromHunk(FIXTURE_HUNK_2, NOW, 1, SOURCEGRAPH as any), |
| 205 | + getDecorationFromHunk(FIXTURE_HUNK_3, NOW, 2, SOURCEGRAPH as any), |
| 206 | + ]) |
| 207 | + }) |
| 208 | + |
| 209 | + it('gets decorations for the selections if selections are passed', async () => { |
| 210 | + expect(await getBlameDecorations({ |
| 211 | + uri: 'a', |
| 212 | + settings: { |
| 213 | + 'git.blame.lineDecorations': true |
| 214 | + }, |
| 215 | + now: NOW, |
| 216 | + selections: [ |
| 217 | + new SOURCEGRAPH.Selection( |
| 218 | + new SOURCEGRAPH.Position(2, 0), |
| 219 | + new SOURCEGRAPH.Position(2, 0) |
| 220 | + ) as any |
| 221 | + ], |
| 222 | + queryHunks: () => Promise.resolve([FIXTURE_HUNK_1, FIXTURE_HUNK_2, FIXTURE_HUNK_3]), |
| 223 | + sourcegraph: SOURCEGRAPH as any |
| 224 | + })).toEqual([ |
| 225 | + getDecorationFromHunk(FIXTURE_HUNK_3, NOW, 2, SOURCEGRAPH as any), |
| 226 | + ]) |
| 227 | + }) |
| 228 | + |
| 229 | +}) |
0 commit comments