@instructure/ui-link 9.2.1-snapshot-9 → 9.2.1-snapshot-11

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.
package/CHANGELOG.md CHANGED
@@ -3,7 +3,7 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
- ## [9.2.1-snapshot-9](https://github.com/instructure/instructure-ui/compare/v9.2.0...v9.2.1-snapshot-9) (2024-07-16)
6
+ ## [9.2.1-snapshot-11](https://github.com/instructure/instructure-ui/compare/v9.2.0...v9.2.1-snapshot-11) (2024-07-16)
7
7
 
8
8
  **Note:** Version bump only for package @instructure/ui-link
9
9
 
@@ -0,0 +1,363 @@
1
+ var _Link, _Link2, _Link3, _svg, _TruncateText2, _svg2, _Link4, _Link5, _Link6, _Link7, _Link8, _Link9, _Link10, _Link11, _Link12, _Link13, _Link14, _Link15, _Link16, _Link17;
2
+ /*
3
+ * The MIT License (MIT)
4
+ *
5
+ * Copyright (c) 2015 - present Instructure, Inc.
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ import React from 'react';
27
+ import { userEvent } from '@testing-library/user-event';
28
+ import { render, screen, waitFor } from '@testing-library/react';
29
+ import '@testing-library/jest-dom';
30
+ import { vi } from 'vitest';
31
+ import { runAxeCheck } from '@instructure/ui-axe-check';
32
+ import { Link } from '../index';
33
+
34
+ //<unknown> is needed for React 17 compatibility
35
+ class TruncateText extends React.Component {
36
+ render() {
37
+ return /*#__PURE__*/React.createElement("span", null, this.props.children);
38
+ }
39
+ }
40
+ TruncateText.displayName = "TruncateText";
41
+ describe('<Link />', () => {
42
+ let consoleErrorMock;
43
+ beforeEach(() => {
44
+ // Mocking console to prevent test output pollution
45
+ consoleErrorMock = vi.spyOn(console, 'error').mockImplementation(() => {});
46
+ });
47
+ afterEach(() => {
48
+ consoleErrorMock.mockRestore();
49
+ });
50
+ it('should render the children as text content', async () => {
51
+ render(_Link || (_Link = /*#__PURE__*/React.createElement(Link, {
52
+ href: "https://instructure.design"
53
+ }, "Hello World")));
54
+ const link = screen.getByRole('link');
55
+ expect(link).toHaveTextContent('Hello World');
56
+ });
57
+ it('should render a button', async () => {
58
+ const onClick = vi.fn();
59
+ render( /*#__PURE__*/React.createElement(Link, {
60
+ onClick: onClick
61
+ }, "Hello World"));
62
+ const button = screen.getByRole('button');
63
+ expect(button).toHaveAttribute('type', 'button');
64
+ });
65
+ it('should meet a11y standards', async () => {
66
+ const _render = render(_Link2 || (_Link2 = /*#__PURE__*/React.createElement(Link, {
67
+ href: "https://instructure.design"
68
+ }, "Hello World"))),
69
+ container = _render.container;
70
+ const axeCheck = await runAxeCheck(container);
71
+ expect(axeCheck).toBe(true);
72
+ });
73
+ it('should focus with the focus helper', async () => {
74
+ let linkRef;
75
+ render( /*#__PURE__*/React.createElement(Link, {
76
+ href: "https://instructure.design",
77
+ elementRef: el => {
78
+ linkRef = el;
79
+ }
80
+ }, "Hello World"));
81
+ const linkElement = screen.getByRole('link');
82
+ linkRef.focus();
83
+ expect(linkElement).toHaveFocus();
84
+ });
85
+ it('should display block when TruncateText is a child', async () => {
86
+ render(_Link3 || (_Link3 = /*#__PURE__*/React.createElement(Link, {
87
+ href: "example.html"
88
+ }, /*#__PURE__*/React.createElement(TruncateText, null, "Hello World"))));
89
+ const link = screen.getByRole('link');
90
+ expect(link).toHaveStyle('display: block');
91
+ });
92
+ it('should display inline-flex when TruncateText is a child and there is an icon', async () => {
93
+ const customIcon = _svg || (_svg = /*#__PURE__*/React.createElement("svg", {
94
+ height: "24",
95
+ width: "24"
96
+ }, /*#__PURE__*/React.createElement("title", null, "Custom icon"), /*#__PURE__*/React.createElement("circle", {
97
+ cx: "50",
98
+ cy: "50",
99
+ r: "40"
100
+ })));
101
+ render( /*#__PURE__*/React.createElement(Link, {
102
+ href: "example.html",
103
+ renderIcon: customIcon
104
+ }, _TruncateText2 || (_TruncateText2 = /*#__PURE__*/React.createElement(TruncateText, null, "Hello World"))));
105
+ const link = screen.getByRole('link');
106
+ expect(link).toHaveStyle('display: inline-flex');
107
+ });
108
+ it('should call the onClick prop when clicked', async () => {
109
+ const onClick = vi.fn();
110
+ render( /*#__PURE__*/React.createElement(Link, {
111
+ onClick: onClick
112
+ }, "Hello World"));
113
+ const link = screen.getByRole('button');
114
+ userEvent.click(link);
115
+ await waitFor(() => {
116
+ expect(onClick).toHaveBeenCalledTimes(1);
117
+ });
118
+ });
119
+ it('should call the onMouseEnter prop when mouseEntered', async () => {
120
+ const onMouseEnter = vi.fn();
121
+ const _render2 = render( /*#__PURE__*/React.createElement(Link, {
122
+ onMouseEnter: onMouseEnter
123
+ }, "Hello World")),
124
+ container = _render2.container;
125
+ const link = container.querySelector('span[class$="-link"]');
126
+ userEvent.hover(link);
127
+ await waitFor(() => {
128
+ expect(onMouseEnter).toHaveBeenCalledTimes(1);
129
+ });
130
+ });
131
+ it('should pass down an icon via the icon property', async () => {
132
+ const customIcon = _svg2 || (_svg2 = /*#__PURE__*/React.createElement("svg", {
133
+ "data-testid": "svg",
134
+ height: "100",
135
+ width: "100"
136
+ }, /*#__PURE__*/React.createElement("title", null, "Custom icon"), /*#__PURE__*/React.createElement("circle", {
137
+ cx: "50",
138
+ cy: "50",
139
+ r: "40"
140
+ })));
141
+ render( /*#__PURE__*/React.createElement(Link, {
142
+ href: "https://instructure.design",
143
+ renderIcon: customIcon
144
+ }, "Hello World"));
145
+ const title = screen.getByText('Custom icon');
146
+ const icon = screen.getByTestId('svg');
147
+ expect(title).toBeInTheDocument();
148
+ expect(icon).toBeInTheDocument();
149
+ });
150
+ describe('when interaction is disabled', () => {
151
+ it('should apply aria-disabled when interaction is disabled', async () => {
152
+ render(_Link4 || (_Link4 = /*#__PURE__*/React.createElement(Link, {
153
+ href: "example.html",
154
+ interaction: "disabled"
155
+ }, "Hello World")));
156
+ const link = screen.getByRole('link');
157
+ expect(link).toHaveAttribute('aria-disabled');
158
+ });
159
+ });
160
+ it('should apply aria-disabled when `disabled` is set', async () => {
161
+ render(_Link5 || (_Link5 = /*#__PURE__*/React.createElement(Link, {
162
+ href: "example.html",
163
+ disabled: true
164
+ }, "Hello World")));
165
+ const link = screen.getByRole('link');
166
+ expect(link).toHaveAttribute('aria-disabled');
167
+ });
168
+ it('should not be clickable when interaction is disabled', async () => {
169
+ const onClick = vi.fn();
170
+ render( /*#__PURE__*/React.createElement(Link, {
171
+ onClick: onClick,
172
+ interaction: "disabled"
173
+ }, "Hello World"));
174
+ const button = screen.getByRole('button');
175
+ expect(button).toHaveAttribute('aria-disabled', 'true');
176
+ });
177
+ it('should not be clickable when `disabled` is set', async () => {
178
+ const onClick = vi.fn();
179
+ render( /*#__PURE__*/React.createElement(Link, {
180
+ onClick: onClick,
181
+ disabled: true
182
+ }, "Hello World"));
183
+ const button = screen.getByRole('button');
184
+ expect(button).toHaveAttribute('aria-disabled', 'true');
185
+ });
186
+ });
187
+ describe('with `as` prop', () => {
188
+ describe('with `onClick`', () => {
189
+ it('should render designated tag', async () => {
190
+ const onClick = vi.fn();
191
+ render( /*#__PURE__*/React.createElement(Link, {
192
+ as: "a",
193
+ onClick: onClick
194
+ }, "Hello World"));
195
+ const link = screen.getByText('Hello World');
196
+ expect(link.tagName).toBe('A');
197
+ });
198
+ it('should set role="button"', async () => {
199
+ const onClick = vi.fn();
200
+ render( /*#__PURE__*/React.createElement(Link, {
201
+ as: "span",
202
+ onClick: onClick
203
+ }, "Hello World"));
204
+ const spanLink = screen.getByRole('button');
205
+ expect(spanLink).toHaveAttribute('role', 'button');
206
+ });
207
+ });
208
+ describe('should not set type="button", unless it is actually a button', () => {
209
+ it('should not set type="button" on other things like <span>s', async () => {
210
+ const onClick = vi.fn();
211
+ render( /*#__PURE__*/React.createElement(Link, {
212
+ as: "span",
213
+ onClick: onClick
214
+ }, "Hello World"));
215
+ const link = screen.getByText('Hello World');
216
+ expect(link).not.toHaveAttribute('type', 'button');
217
+ });
218
+ it('should set type="button" on <button>s', async () => {
219
+ const onClick = vi.fn();
220
+ render( /*#__PURE__*/React.createElement(Link, {
221
+ as: "button",
222
+ onClick: onClick
223
+ }, "Hello World"));
224
+ const button = screen.getByText('Hello World');
225
+ expect(button).toHaveAttribute('type', 'button');
226
+ });
227
+ it('should set tabIndex="0"', async () => {
228
+ const onClick = vi.fn();
229
+ render( /*#__PURE__*/React.createElement(Link, {
230
+ as: "span",
231
+ onClick: onClick
232
+ }, "Hello World"));
233
+ const spanLink = screen.getByText('Hello World');
234
+ expect(spanLink).toHaveAttribute('tabIndex', '0');
235
+ });
236
+ });
237
+ describe('without `onClick`', () => {
238
+ it('should render designated tag', async () => {
239
+ render(_Link6 || (_Link6 = /*#__PURE__*/React.createElement(Link, {
240
+ as: "a"
241
+ }, "Hello World")));
242
+ const link = screen.getByText('Hello World');
243
+ expect(link.tagName).toBe('A');
244
+ });
245
+ it('should not set role="button"', async () => {
246
+ render(_Link7 || (_Link7 = /*#__PURE__*/React.createElement(Link, {
247
+ as: "span"
248
+ }, "Hello World")));
249
+ const link = screen.getByText('Hello World');
250
+ expect(link).not.toHaveAttribute('role', 'button');
251
+ });
252
+ it('should not set type="button"', async () => {
253
+ render(_Link8 || (_Link8 = /*#__PURE__*/React.createElement(Link, {
254
+ as: "span"
255
+ }, "Hello World")));
256
+ const link = screen.getByText('Hello World');
257
+ expect(link).not.toHaveAttribute('type', 'button');
258
+ });
259
+ it('should not set tabIndex="0"', async () => {
260
+ render(_Link9 || (_Link9 = /*#__PURE__*/React.createElement(Link, {
261
+ as: "span"
262
+ }, "Hello World")));
263
+ const link = screen.getByText('Hello World');
264
+ expect(link).not.toHaveAttribute('tabIndex', '0');
265
+ });
266
+ });
267
+ describe('when an href is provided', () => {
268
+ it('should render an anchor element', async () => {
269
+ render(_Link10 || (_Link10 = /*#__PURE__*/React.createElement(Link, {
270
+ href: "example.html"
271
+ }, "Hello World")));
272
+ const link = screen.getByRole('link');
273
+ expect(link.tagName).toBe('A');
274
+ });
275
+ it('should set the href attribute', async () => {
276
+ render(_Link11 || (_Link11 = /*#__PURE__*/React.createElement(Link, {
277
+ href: "example.html"
278
+ }, "Hello World")));
279
+ const link = screen.getByRole('link');
280
+ expect(link).toHaveAttribute('href', 'example.html');
281
+ });
282
+ it('should not set role="button"', async () => {
283
+ render(_Link12 || (_Link12 = /*#__PURE__*/React.createElement(Link, {
284
+ href: "example.html"
285
+ }, "Hello World")));
286
+ const link = screen.getByRole('link');
287
+ expect(link).not.toHaveAttribute('role', 'button');
288
+ });
289
+ it('should not set type="button"', async () => {
290
+ render(_Link13 || (_Link13 = /*#__PURE__*/React.createElement(Link, {
291
+ href: "example.html"
292
+ }, "Hello World")));
293
+ const link = screen.getByRole('link');
294
+ expect(link).not.toHaveAttribute('type', 'button');
295
+ });
296
+ });
297
+ describe('when a `role` is provided', () => {
298
+ it('should set role', async () => {
299
+ render(_Link14 || (_Link14 = /*#__PURE__*/React.createElement(Link, {
300
+ href: "example.html",
301
+ role: "button"
302
+ }, "Hello World")));
303
+ const link = screen.getByText('Hello World');
304
+ expect(link).toHaveAttribute('role', 'button');
305
+ });
306
+ it('should not override button role when it is forced by default', async () => {
307
+ const onClick = vi.fn();
308
+ render( /*#__PURE__*/React.createElement(Link, {
309
+ role: "link",
310
+ onClick: onClick,
311
+ as: "a"
312
+ }, "Hello World"));
313
+ const link = screen.getByText('Hello World');
314
+ expect(link).toHaveAttribute('role', 'button');
315
+ });
316
+ });
317
+ describe('when a `forceButtonRole` is set to false', () => {
318
+ it('should not force button role', async () => {
319
+ const onClick = vi.fn();
320
+ render( /*#__PURE__*/React.createElement(Link, {
321
+ onClick: onClick,
322
+ as: "a",
323
+ forceButtonRole: false
324
+ }, "Hello World"));
325
+ const link = screen.getByText('Hello World');
326
+ expect(link).not.toHaveAttribute('role');
327
+ });
328
+ it('should override button role with `role` prop', async () => {
329
+ const onClick = vi.fn();
330
+ render( /*#__PURE__*/React.createElement(Link, {
331
+ role: "link",
332
+ onClick: onClick,
333
+ as: "a",
334
+ forceButtonRole: false
335
+ }, "Hello World"));
336
+ const link = screen.getByText('Hello World');
337
+ expect(link).toHaveAttribute('role', 'link');
338
+ });
339
+ });
340
+ describe('when a `to` is provided', () => {
341
+ it('should render an anchor element', async () => {
342
+ render(_Link15 || (_Link15 = /*#__PURE__*/React.createElement(Link, {
343
+ to: "/example"
344
+ }, "Hello World")));
345
+ const link = screen.getByText('Hello World');
346
+ expect(link.tagName).toBe('A');
347
+ });
348
+ it('should set the to attribute', async () => {
349
+ render(_Link16 || (_Link16 = /*#__PURE__*/React.createElement(Link, {
350
+ to: "/example"
351
+ }, "Hello World")));
352
+ const link = screen.getByText('Hello World');
353
+ expect(link).toHaveAttribute('to', '/example');
354
+ });
355
+ it('should not set role="button"', async () => {
356
+ render(_Link17 || (_Link17 = /*#__PURE__*/React.createElement(Link, {
357
+ to: "/example"
358
+ }, "Hello World")));
359
+ const link = screen.getByText('Hello World');
360
+ expect(link).not.toHaveAttribute('role', 'button');
361
+ });
362
+ });
363
+ });