@instructure/ui-link 10.19.2-snapshot-2 → 10.19.2-snapshot-4

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