|
1 |
| -import { expect } from '@storybook/jest'; |
| 1 | +import { expect, jest } from '@storybook/jest'; |
2 | 2 | import { ComponentStory, ComponentMeta } from '@storybook/react';
|
3 | 3 | import { userEvent, within } from '@storybook/testing-library';
|
4 | 4 | import * as React from 'react';
|
@@ -96,44 +96,66 @@ CodeGroupInteractions.args = {
|
96 | 96 | </CodeBlock>,
|
97 | 97 | ],
|
98 | 98 | };
|
| 99 | + |
99 | 100 | CodeGroupInteractions.play = async ({ canvasElement }) => {
|
| 101 | + let clipboardData: unknown = ''; |
| 102 | + |
| 103 | + jest.spyOn(window, 'navigator', 'get').mockImplementation(() => { |
| 104 | + return { |
| 105 | + clipboard: { |
| 106 | + writeText: jest.fn((data) => { |
| 107 | + clipboardData = data; |
| 108 | + }), |
| 109 | + readText: jest.fn(() => { |
| 110 | + return clipboardData; |
| 111 | + }), |
| 112 | + }, |
| 113 | + } as never; |
| 114 | + }); |
| 115 | + |
100 | 116 | const canvas = within(canvasElement);
|
101 |
| - // 👇 Assert DOM structure |
| 117 | + await expect(window.navigator.clipboard).toBeDefined(); |
| 118 | + |
102 | 119 | await delay(20);
|
103 |
| - await expect(canvas.getByText(filename)).toBeInTheDOM(); |
104 |
| - await expect(canvas.getByText('Copy')).toBeInTheDOM(); |
| 120 | + await userEvent.hover(await within(canvasElement).getByRole('button')); |
| 121 | + |
| 122 | + // 👇 Assert DOM structure. |
| 123 | + await expect(canvas.getByText(filename)).toBeInTheDocument(); |
| 124 | + await expect(canvas.getByText(testString)).toBeInTheDocument(); |
| 125 | + await expect(canvas.getByText('Copy')).toBeInTheDocument(); |
| 126 | + await expect(canvas.getByText(testString)).toBeInTheDocument; |
105 | 127 | await expect(canvas.getByText('Copy')).not.toBeVisible();
|
106 |
| - await expect(canvas.getByText(testString)).toBeInTheDOM; |
| 128 | + |
107 | 129 | // 👇 Simulate copy to clipboard.
|
108 |
| - await userEvent.click(canvas.getByText('Copy')); |
| 130 | + await userEvent.click(canvas.getByRole('button')); |
109 | 131 |
|
110 | 132 | // 👇 Assert DOM structure.
|
111 |
| - await delay(20); |
112 |
| - await expect(canvas.getByText('Copied')).toBeInTheDOM(); |
| 133 | + await delay(100); |
| 134 | + await expect(canvas.getByText('Copied')).toBeInTheDocument(); |
113 | 135 | await expect(canvas.getByText('Copied')).toBeVisible();
|
114 | 136 | // 👇 Assert if copied to clipboard.
|
115 |
| - await expect(await navigator.clipboard.readText()).toEqual(testString); |
| 137 | + await expect(await window.navigator.clipboard.readText()).toEqual(testString); |
116 | 138 |
|
117 | 139 | // 👇 Simulate tab switch.
|
118 | 140 | await userEvent.click(canvas.getByText(filename2));
|
119 |
| - await delay(20); |
120 |
| - await expect(canvas.getByText(filename2)).toBeInTheDOM(); |
121 |
| - await expect(canvas.getByText(testString2)).toBeInTheDOM(); |
| 141 | + await delay(100); |
| 142 | + await expect(canvas.getByText(filename2)).toBeInTheDocument(); |
| 143 | + await expect(canvas.getByText(testString2)).toBeInTheDocument(); |
122 | 144 |
|
123 | 145 | // 👇 Simulate copy to clipboard click.
|
124 |
| - await userEvent.click(canvas.getByText('Copied')); |
| 146 | + await userEvent.click(canvas.getByRole('button')); |
| 147 | + await delay(500); |
125 | 148 |
|
126 | 149 | // 👇 Assert DOM structure.
|
127 |
| - await delay(20); |
128 |
| - await expect(canvas.getByText('Copied')).toBeInTheDOM(); |
129 |
| - await expect(canvas.getByText('Copied')).toBeVisible(); |
| 150 | + await expect(canvas.queryByText('Copied')).toBeInTheDocument(); |
| 151 | + await expect(canvas.queryByText('Copied')).toBeVisible(); |
130 | 152 |
|
131 | 153 | // 👇 Assert if copied to clipboard.
|
132 |
| - await expect(await navigator.clipboard.readText()).toEqual(testString2); |
| 154 | + await expect(await window.navigator.clipboard.readText()).toEqual(testString2); |
133 | 155 |
|
134 | 156 | // 👇 Wait for Tooltip to close.
|
135 | 157 | await delay(3000);
|
136 | 158 |
|
137 | 159 | // 👇 Expect Tooltip to be hidden.
|
138 |
| - await expect(canvas.getByText('Copy')).not.toBeVisible(); |
| 160 | + await expect(canvas.queryByText('Copy')).not.toBeVisible(); |
139 | 161 | };
|
0 commit comments