@khanacademy/wonder-blocks-tooltip 1.4.0 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,17 +1,12 @@
1
1
  // @flow
2
2
  import * as React from "react";
3
- import * as ReactDOM from "react-dom";
4
- import {mount} from "enzyme";
5
- import "jest-enzyme";
3
+ import {render, screen} from "@testing-library/react";
6
4
 
7
5
  import {View} from "@khanacademy/wonder-blocks-core";
8
6
 
9
7
  import TooltipBubble from "../tooltip-bubble.js";
10
8
  import typeof TooltipContent from "../tooltip-content.js";
11
9
 
12
- const sleep = (duration = 0) =>
13
- new Promise((resolve, reject) => setTimeout(resolve, duration));
14
-
15
10
  describe("TooltipBubble", () => {
16
11
  // A little helper method to make the actual test more readable.
17
12
  const makePopperProps = () => ({
@@ -27,55 +22,43 @@ describe("TooltipBubble", () => {
27
22
 
28
23
  test("updates reference to bubble container", async () => {
29
24
  // Arrange
30
- const bubbleNode = await new Promise((resolve) => {
31
- // Get some props and set the ref to our assert, that way we assert
32
- // when the bubble component is mounted.
33
- const props = makePopperProps();
25
+ // Get some props and set the ref to our assert, that way we assert
26
+ // when the bubble component is mounted.
27
+ const props = makePopperProps();
28
+
29
+ // Do some casting to pretend this is `TooltipContent`. That way we are
30
+ // isolating behaviors a bit more.
31
+ const fakeContent = (((
32
+ <View id="content">Some content</View>
33
+ ): any): React.Element<TooltipContent>);
34
34
 
35
- // Do some casting to pretend this is `TooltipContent`. That way
36
- // we are isolating behaviors a bit more.
37
- const fakeContent = (((
38
- <View id="content">Some content</View>
39
- ): any): React.Element<TooltipContent>);
40
- const nodes = (
41
- <View>
42
- <TooltipBubble
43
- id="bubble"
44
- placement={props.placement}
45
- tailOffset={props.tailOffset}
46
- updateBubbleRef={resolve}
47
- onActiveChanged={() => {}}
48
- >
49
- {fakeContent}
50
- </TooltipBubble>
51
- </View>
52
- );
35
+ // Act
36
+ render(
37
+ <View>
38
+ <TooltipBubble
39
+ id="bubble"
40
+ placement={props.placement}
41
+ tailOffset={props.tailOffset}
42
+ updateBubbleRef={jest.fn()}
43
+ onActiveChanged={() => {}}
44
+ >
45
+ {fakeContent}
46
+ </TooltipBubble>
47
+ </View>,
48
+ );
53
49
 
54
- // Act
55
- mount(nodes);
56
- });
50
+ // Is the node a mounted element?
51
+ const tooltip = await screen.findByRole("tooltip");
57
52
 
58
53
  /**
59
- * All we're doing is making sure we got called and verifying that
60
- * we got called with an element we expect.
54
+ * All we're doing is making sure we got called and verifying that we
55
+ * got called with an element we expect.
61
56
  */
62
57
  // Assert
63
- // Did we get a node?
64
- expect(bubbleNode).toBeDefined();
65
-
66
- // Is the node a mounted element?
67
- const realElement = ReactDOM.findDOMNode(bubbleNode);
68
- expect(realElement instanceof Element).toBeTruthy();
69
-
70
- // Keep flow happy...
71
- if (realElement instanceof Element) {
72
- // Did we apply our data attribute?
73
- expect(realElement.getAttribute("data-placement")).toBe("top");
58
+ // Did we apply our data attribute?
59
+ expect(tooltip.getAttribute("data-placement")).toBe("top");
74
60
 
75
- // Did we render our content?
76
- await sleep();
77
- const contentElement = document.getElementById("content");
78
- expect(contentElement).toBeDefined();
79
- }
61
+ // Did we render our content?
62
+ expect(screen.getByText("Some content")).toBeInTheDocument();
80
63
  });
81
64
  });
@@ -1,8 +1,7 @@
1
1
  // @flow
2
2
  import * as React from "react";
3
3
  import * as ReactDOM from "react-dom";
4
- import {mount} from "enzyme";
5
- import "jest-enzyme";
4
+ import {render} from "@testing-library/react";
6
5
 
7
6
  import {View} from "@khanacademy/wonder-blocks-core";
8
7
 
@@ -58,7 +57,7 @@ describe("TooltipPopper", () => {
58
57
  <TestHarness placement="bottom" resultRef={resolve} />
59
58
  </View>
60
59
  );
61
- mount(nodes);
60
+ render(nodes);
62
61
  });
63
62
 
64
63
  if (!ref) {
@@ -1,6 +1,6 @@
1
1
  // @flow
2
2
  import * as React from "react";
3
- import {shallow} from "enzyme";
3
+ import {render} from "@testing-library/react";
4
4
 
5
5
  import TooltipTail from "../tooltip-tail.js";
6
6
 
@@ -14,10 +14,12 @@ describe("TooltipTail", () => {
14
14
  const nodes = <TooltipTail placement={fakePlacement} />;
15
15
 
16
16
  // Act
17
- const underTest = () => shallow(nodes);
17
+ const underTest = () => render(nodes);
18
18
 
19
19
  // Assert
20
- expect(underTest).toThrowErrorMatchingSnapshot();
20
+ expect(underTest).toThrowErrorMatchingInlineSnapshot(
21
+ `"Unknown placement: notaplacement"`,
22
+ );
21
23
  });
22
24
 
23
25
  test("known placement, does not throw", () => {
@@ -26,7 +28,7 @@ describe("TooltipTail", () => {
26
28
  const makeNode = (p) => <TooltipTail placement={p} />;
27
29
 
28
30
  // Act
29
- const testees = testPoints.map((tp) => () => shallow(makeNode(tp)));
31
+ const testees = testPoints.map((tp) => () => render(makeNode(tp)));
30
32
 
31
33
  // Assert
32
34
  for (const testee of testees) {
@@ -34,84 +36,4 @@ describe("TooltipTail", () => {
34
36
  }
35
37
  });
36
38
  });
37
-
38
- describe("INTERNALS", () => {
39
- // We have some code internally that is there for code maintenance to
40
- // catch if we add a new placement string and forget to update one of
41
- // our methods. These tests are to verify those maintenance checks
42
- // so they poke at the insides of the TooltipTail. We only test the
43
- // throw case in these examples and rely on testing the external
44
- // behavior of the component to verify the expected execution paths.
45
- test("_getFilterPositioning throws on bad placement", () => {
46
- // Arrange
47
- const fakePlacement = (("notaplacement": any): Placement);
48
- // We need an instance so let's make one with a valid placement so
49
- // that we can render it.
50
- const nodes = <TooltipTail placement="top" />;
51
- const wrapper = shallow(nodes);
52
- const tailInstance = wrapper.instance();
53
- // Sneakily change props so as not to re-render.
54
- // This means getting rid of the read-only props and
55
- // recreating so that we can write a fake placement value
56
- // for testing.
57
- const oldProps = tailInstance.props;
58
- // $FlowIgnore
59
- delete tailInstance.props;
60
- tailInstance.props = {
61
- ...oldProps,
62
- placement: fakePlacement,
63
- };
64
-
65
- // Act
66
- const underTest = () => tailInstance._getFilterPositioning();
67
-
68
- // Assert
69
- expect(underTest).toThrowErrorMatchingSnapshot();
70
- });
71
-
72
- test("_calculateDimensionsFromPlacement throws on bad placement", () => {
73
- // Arrange
74
- const fakePlacement = (("notaplacement": any): Placement);
75
- // We need an instance so let's make one with a valid placement so
76
- // that we can render it.
77
- const nodes = <TooltipTail placement="top" />;
78
- const wrapper = shallow(nodes);
79
- const tailInstance = wrapper.instance();
80
- // Sneakily change props so as not to re-render.
81
- // This means getting rid of the read-only props and
82
- // recreating so that we can write a fake placement value
83
- // for testing.
84
- const oldProps = tailInstance.props;
85
- // $FlowIgnore
86
- delete tailInstance.props;
87
- tailInstance.props = {
88
- ...oldProps,
89
- placement: fakePlacement,
90
- };
91
-
92
- // Act
93
- const underTest = () =>
94
- tailInstance._calculateDimensionsFromPlacement();
95
-
96
- // Assert
97
- expect(underTest).toThrowErrorMatchingSnapshot();
98
- });
99
-
100
- test("_minDistanceFromCorners throws on bad placement", () => {
101
- // Arrange
102
- const fakePlacement = (("notaplacement": any): Placement);
103
- // We need an instance so let's make one with a valid placement so
104
- // that we can render it.
105
- const nodes = <TooltipTail placement="top" />;
106
- const wrapper = shallow(nodes);
107
- const tailInstance = wrapper.instance();
108
-
109
- // Act
110
- const underTest = () =>
111
- tailInstance._minDistanceFromCorners(fakePlacement);
112
-
113
- // Assert
114
- expect(underTest).toThrowErrorMatchingSnapshot();
115
- });
116
- });
117
39
  });