-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
1 parent
b5b5b57
commit fd12b20
Showing
2 changed files
with
64 additions
and
162 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 |
---|---|---|
@@ -1,189 +1,91 @@ | ||
import * as getOffset from "../../util/getOffset"; | ||
import * as getWindowSize from "../../util/getWindowSize"; | ||
import { placeTooltip } from "./placeTooltip"; | ||
import { determineAutoPosition, TooltipPosition } from "./tooltipPosition"; | ||
|
||
const positionPrecedence: TooltipPosition[] = [ | ||
"bottom", | ||
"top", | ||
"right", | ||
"left", | ||
]; | ||
|
||
describe("placeTooltip", () => { | ||
test("should automatically place the tooltip position when there is enough space", () => { | ||
// Arrange | ||
jest.spyOn(getOffset, "default").mockReturnValue({ | ||
height: 100, | ||
width: 100, | ||
top: 0, | ||
left: 0, | ||
}); | ||
|
||
jest.spyOn(getWindowSize, "default").mockReturnValue({ | ||
height: 1000, | ||
width: 1000, | ||
}); | ||
|
||
jest.spyOn(Element.prototype, "getBoundingClientRect").mockReturnValue({ | ||
x: 0, | ||
y: 0, | ||
toJSON: jest.fn, | ||
width: 100, | ||
height: 100, | ||
top: 200, | ||
left: 200, | ||
bottom: 300, | ||
right: 300, | ||
}); | ||
|
||
const stepElement = document.createElement("div"); | ||
const tooltipLayer = document.createElement("div"); | ||
const arrowLayer = document.createElement("div"); | ||
|
||
// Act | ||
placeTooltip( | ||
tooltipLayer, | ||
arrowLayer, | ||
stepElement, | ||
"top", | ||
["top", "bottom", "left", "right"], | ||
false, | ||
true | ||
); | ||
|
||
// Assert | ||
expect(tooltipLayer.className).toBe( | ||
"introjs-tooltip introjs-top-right-aligned" | ||
); | ||
}); | ||
|
||
test("should skip auto positioning when autoPosition is false", () => { | ||
// Arrange | ||
const stepElement = document.createElement("div"); | ||
const tooltipLayer = document.createElement("div"); | ||
const arrowLayer = document.createElement("div"); | ||
|
||
// Act | ||
placeTooltip( | ||
tooltipLayer, | ||
arrowLayer, | ||
stepElement, | ||
const position = determineAutoPosition( | ||
positionPrecedence, | ||
{ | ||
top: 200, | ||
left: 200, | ||
height: 100, | ||
width: 100, | ||
right: 300, | ||
bottom: 300, | ||
absoluteTop: 200, | ||
absoluteLeft: 200, | ||
absoluteRight: 300, | ||
absoluteBottom: 300, | ||
}, | ||
100, | ||
100, | ||
"top", | ||
["top", "bottom"], | ||
false, | ||
false | ||
{ height: 1000, width: 1000 } | ||
); | ||
|
||
// Assert | ||
expect(tooltipLayer.className).toBe("introjs-tooltip introjs-top"); | ||
expect(position).toBe("top-right-aligned"); | ||
}); | ||
|
||
test("should use floating tooltips when height/width is limited", () => { | ||
// Arrange | ||
jest.spyOn(getOffset, "default").mockReturnValue({ | ||
height: 100, | ||
width: 100, | ||
top: 0, | ||
left: 0, | ||
}); | ||
|
||
jest.spyOn(getWindowSize, "default").mockReturnValue({ | ||
height: 100, | ||
width: 100, | ||
}); | ||
|
||
jest.spyOn(Element.prototype, "getBoundingClientRect").mockReturnValue({ | ||
x: 0, | ||
y: 0, | ||
toJSON: jest.fn, | ||
width: 100, | ||
height: 100, | ||
top: 0, | ||
left: 0, | ||
bottom: 0, | ||
right: 0, | ||
}); | ||
|
||
const stepElement = document.createElement("div"); | ||
const tooltipLayer = document.createElement("div"); | ||
const arrowLayer = document.createElement("div"); | ||
|
||
// Act | ||
placeTooltip( | ||
tooltipLayer, | ||
arrowLayer, | ||
stepElement, | ||
"left", | ||
["top", "bottom", "left", "right"], | ||
false, | ||
true | ||
const position = determineAutoPosition( | ||
positionPrecedence, | ||
{ | ||
top: 0, | ||
left: 0, | ||
height: 100, | ||
width: 100, | ||
right: 0, | ||
bottom: 0, | ||
absoluteTop: 0, | ||
absoluteLeft: 0, | ||
absoluteRight: 0, | ||
absoluteBottom: 0, | ||
}, | ||
100, | ||
100, | ||
"top", | ||
{ height: 100, width: 100 } | ||
); | ||
|
||
// Assert | ||
expect(tooltipLayer.className).toBe("introjs-tooltip introjs-floating"); | ||
expect(position).toBe("floating"); | ||
}); | ||
|
||
test("should use bottom middle aligned when there is enough vertical space", () => { | ||
// Arrange | ||
jest.spyOn(getOffset, "default").mockReturnValue({ | ||
height: 100, | ||
width: 100, | ||
top: 0, | ||
left: 0, | ||
}); | ||
|
||
jest.spyOn(getWindowSize, "default").mockReturnValue({ | ||
height: 500, | ||
width: 100, | ||
}); | ||
|
||
jest.spyOn(Element.prototype, "getBoundingClientRect").mockReturnValue({ | ||
x: 0, | ||
y: 0, | ||
toJSON: jest.fn, | ||
width: 100, | ||
height: 100, | ||
top: 0, | ||
left: 0, | ||
bottom: 0, | ||
right: 0, | ||
}); | ||
|
||
const stepElement = document.createElement("div"); | ||
const tooltipLayer = document.createElement("div"); | ||
const arrowLayer = document.createElement("div"); | ||
|
||
// Act | ||
placeTooltip( | ||
tooltipLayer, | ||
arrowLayer, | ||
stepElement, | ||
"left", | ||
["top", "bottom", "left", "right"], | ||
false, | ||
true | ||
); | ||
|
||
// Assert | ||
expect(tooltipLayer.className).toBe( | ||
"introjs-tooltip introjs-bottom-middle-aligned" | ||
); | ||
}); | ||
|
||
test("should attach the global custom tooltip css class", () => { | ||
// Arrange | ||
const stepElement = document.createElement("div"); | ||
const tooltipLayer = document.createElement("div"); | ||
const arrowLayer = document.createElement("div"); | ||
|
||
// Act | ||
placeTooltip( | ||
tooltipLayer, | ||
arrowLayer, | ||
stepElement, | ||
const position = determineAutoPosition( | ||
positionPrecedence, | ||
{ | ||
top: 0, | ||
left: 0, | ||
height: 100, | ||
width: 100, | ||
right: 0, | ||
bottom: 0, | ||
absoluteTop: 0, | ||
absoluteLeft: 0, | ||
absoluteRight: 0, | ||
absoluteBottom: 0, | ||
}, | ||
100, | ||
100, | ||
"left", | ||
["top", "bottom", "left", "right"], | ||
false, | ||
true, | ||
"newClass" | ||
{ height: 500, width: 100 } | ||
); | ||
|
||
// Assert | ||
expect(tooltipLayer.className).toBe( | ||
"introjs-tooltip newClass introjs-bottom-middle-aligned" | ||
); | ||
expect(position).toBe("bottom-middle-aligned"); | ||
}); | ||
}); |
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