Skip to content
This repository was archived by the owner on Nov 25, 2021. It is now read-only.

Commit a1924c1

Browse files
committed
refactor: allow dispatching other event types in tests
1 parent 05ac759 commit a1924c1

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

src/hoverifier.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { HoverOverlayProps } from './HoverOverlay'
1717
import { findPositionsFromEvents } from './positions'
1818
import { CodeViewProps, DOM } from './testutils/dom'
1919
import { createHoverMerged, createStubHoverFetcher, createStubJumpURLFetcher } from './testutils/lsp'
20-
import { clickPositionImpure } from './testutils/mouse'
20+
import { dispatchMouseEventAtPositionImpure } from './testutils/mouse'
2121
import { LOADING } from './types'
2222

2323
describe('Hoverifier', () => {
@@ -104,7 +104,7 @@ describe('Hoverifier', () => {
104104

105105
// Click https://sourcegraph.sgdev.org/github.com/gorilla/mux@cb4698366aa625048f3b815af6a0dea8aef9280a/-/blob/mux.go#L24:6
106106
cold(inputDiagram).subscribe(() =>
107-
clickPositionImpure(codeView, {
107+
dispatchMouseEventAtPositionImpure('click', codeView, {
108108
line: 24,
109109
character: 6,
110110
})
@@ -186,7 +186,7 @@ describe('Hoverifier', () => {
186186
}
187187

188188
cold(inputDiagram).subscribe(() =>
189-
clickPositionImpure(codeView, {
189+
dispatchMouseEventAtPositionImpure('click', codeView, {
190190
line: 1,
191191
character: 1,
192192
})

src/positions.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { TestScheduler } from 'rxjs/testing'
44
import { Position } from 'vscode-languageserver-types'
55

66
import { CodeViewProps, DOM } from './testutils/dom'
7-
import { clickPositionImpure } from './testutils/mouse'
7+
import { dispatchMouseEventAtPositionImpure } from './testutils/mouse'
88

99
import { propertyIsDefined } from './helpers'
1010
import { findPositionsFromEvents } from './positions'
@@ -48,7 +48,9 @@ describe('positions', () => {
4848
map(({ position: { line, character } }) => ({ line, character }))
4949
)
5050

51-
cold<Position>(diagram, positions).subscribe(position => clickPositionImpure(codeView, position))
51+
cold<Position>(diagram, positions).subscribe(position =>
52+
dispatchMouseEventAtPositionImpure('click', codeView, position)
53+
)
5254

5355
expectObservable(clickedTokens).toBe(diagram, tokens)
5456
})

src/testutils/mouse.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface Coordinates {
88
y: number
99
}
1010

11-
export const createMouseEvent = (type: string) => (coords: Coordinates) => {
11+
export const createMouseEvent = (type: string, coords: Coordinates) => {
1212
const event = new MouseEvent(type, {
1313
clientX: coords.x,
1414
clientY: coords.y,
@@ -18,20 +18,22 @@ export const createMouseEvent = (type: string) => (coords: Coordinates) => {
1818
return event
1919
}
2020

21-
export const createMouseMoveEvent = createMouseEvent('mousemove')
22-
export const createClickEvent = createMouseEvent('click')
23-
2421
const invalidPosition = ({ line, character }: Position, message: string) =>
2522
`Invalid postion L${line}:${character}. ${message}. Remember, LSP Positions are 0-indexed.`
2623

2724
/**
28-
* Click the given position in a code element. This is impure because the current hoverifier implementation
29-
* requires the click event to come from the already tokenized DOM elements. Ideally we would not rely on this at all.
25+
* Dispatch a mouse event at the given position in a code element. This is impure because the current hoverifier
26+
* implementation requires the click event to come from the already tokenized DOM elements. Ideally we would not
27+
* rely on this at all.
3028
*
3129
* @param codeViewProps the codeViewProps props from the test cases.
32-
* @param position the position to click.
30+
* @param position the position of the event.
3331
*/
34-
export const clickPositionImpure = ({ codeView, getCodeElementFromLineNumber }: CodeViewProps, position: Position) => {
32+
export const dispatchMouseEventAtPositionImpure = (
33+
eventType: 'click' | 'mouseover' | 'mousemove',
34+
{ codeView, getCodeElementFromLineNumber }: CodeViewProps,
35+
position: Position
36+
) => {
3537
const line = getCodeElementFromLineNumber(codeView, position.line)
3638
if (!line) {
3739
throw new Error(invalidPosition(position, 'Line not found'))
@@ -52,7 +54,7 @@ export const clickPositionImpure = ({ codeView, getCodeElementFromLineNumber }:
5254
const rect = line.getBoundingClientRect()
5355
const { top, height, left, width } = rect
5456

55-
const event = createClickEvent({
57+
const event = createMouseEvent(eventType, {
5658
x: left + width / 2,
5759
y: top + height / 2,
5860
})
@@ -88,7 +90,7 @@ export const clickPosition = ({ codeView, getCodeElementFromLineNumber }: CodeVi
8890
const top = rect.top
8991
const height = rect.height
9092

91-
const event = createClickEvent({
93+
const event = createMouseEvent('click', {
9294
x: left + width / 2,
9395
y: top + height / 2,
9496
})

0 commit comments

Comments
 (0)