-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtooltip-popper.test.tsx
More file actions
72 lines (64 loc) · 2.19 KB
/
tooltip-popper.test.tsx
File metadata and controls
72 lines (64 loc) · 2.19 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
import * as React from "react";
import * as ReactDOM from "react-dom";
import {render} from "@testing-library/react";
import {View} from "@khanacademy/wonder-blocks-core";
import TooltipBubble from "../tooltip-bubble";
import TooltipPopper from "../tooltip-popper";
type State = {
ref?: HTMLElement;
};
/**
* A little wrapper for the TooltipPopper so that we can provide an anchor
* element reference and test that the children get rendered.
*/
class TestHarness extends React.Component<any, State> {
state: State = {};
updateRef(ref: any) {
const actualRef = ref && ReactDOM.findDOMNode(ref);
if (actualRef && this.state.ref !== actualRef) {
this.setState({ref: actualRef as HTMLElement});
}
}
render(): React.ReactElement {
const fakeBubble = (
<View ref={(ref: any) => this.props.resultRef(ref)}>
Fake bubble
</View>
) as React.ReactElement<React.ComponentProps<typeof TooltipBubble>>;
return (
<View>
<View ref={(ref: any) => this.updateRef(ref)}>Anchor</View>
<TooltipPopper
placement={this.props.placement}
anchorElement={this.state.ref}
>
{(props: any) => fakeBubble}
</TooltipPopper>
</View>
);
}
}
describe("TooltipPopper", () => {
// The TooltipPopper component is just a wrapper around @floating-ui/react-dom.
// Floating UI requires full visual rendering and we don't do that here as
// we're not in a browser.
// So, let's do a test that we at least render the content how we expect
// and use other things to test the overall placement things.
test("ensure component renders", async () => {
// Arrange
const ref = await new Promise((resolve: any, reject: any) => {
const nodes = (
<View>
<TestHarness placement="bottom" resultRef={resolve} />
</View>
);
render(nodes);
});
if (!ref) {
return;
}
// Act
// Assert
expect(ref).toBeDefined();
});
});