-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
546 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
/exampleDist | ||
.DS_Store | ||
npm-debug.log* | ||
.cache | ||
.cache | ||
/coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module.exports = { | ||
presets: [ | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
targets: { | ||
node: "current" | ||
} | ||
} | ||
] | ||
] | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`useContextMenu result is correct 1`] = ` | ||
Array [ | ||
Object { | ||
"ref": Object { | ||
"current": undefined, | ||
}, | ||
"role": "menu", | ||
"style": Object { | ||
"opacity": 0, | ||
"pointerEvents": "none", | ||
"position": "fixed", | ||
}, | ||
"tabIndex": -1, | ||
}, | ||
Object { | ||
"ref": [Function], | ||
"role": "menuitem", | ||
"tabIndex": -1, | ||
}, | ||
[Function], | ||
Object { | ||
"coords": Array [ | ||
0, | ||
0, | ||
], | ||
"data": undefined, | ||
"isVisible": false, | ||
"setCoords": [Function], | ||
"setVisible": [Function], | ||
}, | ||
] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
import { renderHook, act } from "@testing-library/react-hooks"; | ||
import useContextMenu, { keyCodes } from "../useContextMenu"; | ||
|
||
test("useContextMenu result is correct", () => { | ||
const { result } = renderHook(() => useContextMenu()); | ||
|
||
expect(result.current.length).toBe(4); | ||
|
||
expect(result.current).toMatchSnapshot(); | ||
}); | ||
|
||
test("useContextMenu register event listeners only when visible", () => { | ||
const addEventListener = jest.spyOn(document, "addEventListener"); | ||
const { result } = renderHook(() => useContextMenu()); | ||
|
||
result.current[0].ref.current = { | ||
getBoundingClientRect: jest.fn(() => ({ height: 100, width: 100 })) | ||
}; | ||
|
||
expect(addEventListener).not.toHaveBeenCalled(); | ||
|
||
act(() => { | ||
result.current[3].setVisible(true); | ||
result.current[3].setCoords([100, 100]); | ||
}); | ||
|
||
expect(addEventListener).toHaveBeenCalledTimes(5); | ||
expect(addEventListener.mock.calls.map(call => call[0])).toEqual([ | ||
"mousedown", | ||
"touchstart", | ||
"scroll", | ||
"contextmenu", | ||
"keydown" | ||
]); | ||
}); | ||
|
||
test("useContextMenu sets correctly the style when changing visibility", () => { | ||
const { result } = renderHook(() => useContextMenu()); | ||
|
||
result.current[0].ref.current = { | ||
getBoundingClientRect: jest.fn(() => ({ height: 100, width: 100 })) | ||
}; | ||
|
||
expect(result.current[0].style).toEqual({ | ||
opacity: 0, | ||
pointerEvents: "none", | ||
position: "fixed" | ||
}); | ||
|
||
act(() => { | ||
result.current[3].setVisible(true); | ||
result.current[3].setCoords([100, 100]); | ||
}); | ||
|
||
expect(result.current[0].style).toEqual({ | ||
left: "100px", | ||
opacity: 1, | ||
pointerEvents: "auto", | ||
position: "fixed", | ||
top: "100px" | ||
}); | ||
|
||
act(() => { | ||
result.current[3].setVisible(false); | ||
}); | ||
|
||
expect(result.current[0].style).toEqual({ | ||
opacity: 0, | ||
pointerEvents: "none", | ||
position: "fixed" | ||
}); | ||
}); | ||
|
||
test("useContextMenu keeps the menu inside the viewport", () => { | ||
const { result } = renderHook(() => useContextMenu()); | ||
|
||
result.current[0].ref.current = { | ||
getBoundingClientRect: jest.fn(() => ({ height: 100, width: 100 })) | ||
}; | ||
|
||
expect(result.current[0].style).toEqual({ | ||
opacity: 0, | ||
pointerEvents: "none", | ||
position: "fixed" | ||
}); | ||
|
||
act(() => { | ||
result.current[3].setVisible(true); | ||
result.current[3].setCoords([1000, 700]); | ||
}); | ||
|
||
expect(result.current[0].style).toEqual({ | ||
left: "900px", | ||
opacity: 1, | ||
pointerEvents: "auto", | ||
position: "fixed", | ||
top: "600px" | ||
}); | ||
}); | ||
|
||
test("useContextMenu hides the menu when a new contextmenu event is triggered", () => { | ||
const { result } = renderHook(() => useContextMenu()); | ||
|
||
result.current[0].ref.current = { | ||
getBoundingClientRect: jest.fn(() => ({ height: 100, width: 100 })) | ||
}; | ||
|
||
act(() => { | ||
result.current[3].setVisible(true); | ||
}); | ||
|
||
act(() => { | ||
const event = new MouseEvent("contextmenu"); | ||
|
||
document.dispatchEvent(event); | ||
}); | ||
|
||
expect(result.current[3].isVisible).toEqual(false); | ||
}); | ||
|
||
test("useContextMenu hides the menu when clicking outside", () => { | ||
const { result } = renderHook(() => useContextMenu()); | ||
|
||
result.current[0].ref.current = { | ||
getBoundingClientRect: jest.fn(() => ({ height: 100, width: 100 })), | ||
contains: () => false | ||
}; | ||
|
||
act(() => { | ||
result.current[3].setVisible(true); | ||
}); | ||
|
||
act(() => { | ||
const event = new MouseEvent("mousedown"); | ||
|
||
document.dispatchEvent(event); | ||
}); | ||
|
||
expect(result.current[3].isVisible).toEqual(false); | ||
}); | ||
|
||
test("useContextMenu hides the menu when clicking outside", () => { | ||
const { result } = renderHook(() => useContextMenu()); | ||
|
||
result.current[0].ref.current = { | ||
getBoundingClientRect: jest.fn(() => ({ height: 100, width: 100 })), | ||
contains: () => false | ||
}; | ||
|
||
act(() => { | ||
result.current[3].setVisible(true); | ||
}); | ||
|
||
act(() => { | ||
const event = new MouseEvent("touchstart"); | ||
|
||
document.dispatchEvent(event); | ||
}); | ||
|
||
expect(result.current[3].isVisible).toEqual(false); | ||
}); | ||
|
||
test("useContextMenu hides the menu on scroll", () => { | ||
const { result } = renderHook(() => useContextMenu()); | ||
|
||
result.current[0].ref.current = { | ||
getBoundingClientRect: jest.fn(() => ({ height: 100, width: 100 })), | ||
contains: () => false | ||
}; | ||
|
||
act(() => { | ||
result.current[3].setVisible(true); | ||
}); | ||
|
||
act(() => { | ||
document.dispatchEvent(new Event("scroll")); | ||
}); | ||
|
||
expect(result.current[3].isVisible).toEqual(false); | ||
}); | ||
|
||
test("useContextMenu hides the menu on ESCAPE key press", () => { | ||
const { result } = renderHook(() => useContextMenu()); | ||
|
||
result.current[0].ref.current = { | ||
getBoundingClientRect: jest.fn(() => ({ height: 100, width: 100 })), | ||
contains: () => false | ||
}; | ||
|
||
act(() => { | ||
result.current[3].setVisible(true); | ||
}); | ||
|
||
act(() => { | ||
const event = new KeyboardEvent("keydown", { keyCode: keyCodes.ESCAPE }); | ||
document.dispatchEvent(event); | ||
}); | ||
|
||
expect(result.current[3].isVisible).toEqual(false); | ||
}); | ||
|
||
test("useContextMenu hides the menu on ENTER key press", () => { | ||
const { result } = renderHook(() => useContextMenu()); | ||
|
||
result.current[0].ref.current = { | ||
getBoundingClientRect: jest.fn(() => ({ height: 100, width: 100 })), | ||
contains: () => false | ||
}; | ||
|
||
act(() => { | ||
result.current[3].setVisible(true); | ||
}); | ||
|
||
act(() => { | ||
const event = new KeyboardEvent("keydown", { keyCode: keyCodes.ENTER }); | ||
document.dispatchEvent(event); | ||
}); | ||
|
||
expect(result.current[3].isVisible).toEqual(false); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters