@sakura-ui/sakura-ui 0.4.2 → 0.5.0

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.
@@ -2,46 +2,150 @@ import React from 'react'
2
2
  import { describe, expect, it } from 'vitest'
3
3
  import { render, screen } from '@testing-library/react'
4
4
 
5
- import { Card, CardBody, CardHeader } from '../src'
5
+ import { Card, CardBody, CardFooter, CardHeader } from '../src'
6
6
 
7
7
  describe('Card', () => {
8
- it('should labeled text from Card', async () => {
8
+ it('should render its header and body', async () => {
9
9
  render(
10
10
  <Card>
11
- <CardHeader>Card-Header</CardHeader>
11
+ <CardHeader as="h3">Card-Header</CardHeader>
12
12
  <CardBody>Card-Body</CardBody>
13
13
  </Card>
14
14
  )
15
15
 
16
- const text1 = screen.getByText(/Card-Header/)
17
- expect(text1).toBeInTheDocument()
16
+ expect(screen.getByText('Card-Header')).toBeInTheDocument()
17
+ expect(screen.getByText('Card-Body')).toBeInTheDocument()
18
+ })
19
+
20
+ it('should render the header as the element given by the as property', async () => {
21
+ render(
22
+ <Card>
23
+ <CardHeader as="h4">Card-Header</CardHeader>
24
+ </Card>
25
+ )
18
26
 
19
- const text2 = screen.getByText(/Card-Body/)
20
- expect(text2).toBeInTheDocument()
27
+ // Querying by role rather than by tag name: the point of the change is that
28
+ // the title is reachable by heading navigation, not that it is an <h4>.
29
+ expect(
30
+ screen.getByRole('heading', { level: 4, name: 'Card-Header' })
31
+ ).toBeInTheDocument()
21
32
  })
22
- it('should set the role and id correctly', async () => {
33
+
34
+ it('should render the header as a paragraph when asked to', async () => {
23
35
  render(
24
36
  <Card>
25
- <CardHeader data-testid="header">Card-Header</CardHeader>
37
+ <CardHeader as="p">Card-Header</CardHeader>
38
+ </Card>
39
+ )
40
+
41
+ // Lists of many cards use 'p' so that the headings do not pollute the outline.
42
+ expect(screen.queryByRole('heading')).toBeNull()
43
+ expect(screen.getByText('Card-Header')).toBeInTheDocument()
44
+ })
45
+
46
+ it('should render the root as the element given by the as property', async () => {
47
+ render(
48
+ <Card as="article">
49
+ <CardHeader as="h3">Card-Header</CardHeader>
50
+ </Card>
51
+ )
52
+
53
+ expect(screen.getByRole('article')).toBeInTheDocument()
54
+ })
55
+
56
+ it('should not generate any id or aria reference of its own', async () => {
57
+ const { container } = render(
58
+ <Card as="article">
59
+ <CardHeader as="h3" data-testid="header">
60
+ Card-Header
61
+ </CardHeader>
26
62
  <CardBody data-testid="body">Card-Body</CardBody>
27
63
  </Card>
28
64
  )
29
65
 
30
66
  const card = screen.getByRole('article')
31
- expect(card).toBeInTheDocument()
67
+ // A generated id can only ever dangle or collide, so the library emits none.
68
+ // Callers that want the card named do it themselves, see the test below.
69
+ expect(card).not.toHaveAttribute('aria-labelledby')
70
+ expect(card).not.toHaveAttribute('aria-describedby')
71
+ expect(screen.getByTestId('header')).not.toHaveAttribute('id')
72
+ expect(screen.getByTestId('body')).not.toHaveAttribute('id')
73
+ expect(container.querySelector('[id=""]')).toBeNull()
74
+ })
75
+
76
+ it('should not repeat an id when more than one body is rendered', async () => {
77
+ render(
78
+ <Card>
79
+ <CardBody data-testid="body1">Body-1</CardBody>
80
+ <CardBody data-testid="body2">Body-2</CardBody>
81
+ </Card>
82
+ )
83
+
84
+ expect(screen.getByTestId('body1')).not.toHaveAttribute('id')
85
+ expect(screen.getByTestId('body2')).not.toHaveAttribute('id')
86
+ })
87
+
88
+ it('should let the caller name the card explicitly', async () => {
89
+ render(
90
+ <Card as="article" aria-labelledby="card-title" aria-describedby="card-desc">
91
+ <CardHeader as="h3" id="card-title">
92
+ Card-Header
93
+ </CardHeader>
94
+ <CardBody id="card-desc">Card-Body</CardBody>
95
+ </Card>
96
+ )
97
+
98
+ const card = screen.getByRole('article')
99
+ expect(card).toHaveAccessibleName('Card-Header')
100
+ expect(card).toHaveAccessibleDescription('Card-Body')
101
+ })
32
102
 
33
- const header = screen.getByTestId('header')
34
- expect(header).toBeInTheDocument()
35
- expect(header).toHaveAttribute('id')
103
+ it('should render a card without a header', async () => {
104
+ render(
105
+ <Card as="article">
106
+ <CardBody>Card-Body</CardBody>
107
+ </Card>
108
+ )
109
+
110
+ // Used to leave aria-labelledby pointing at an element that was never rendered.
111
+ const card = screen.getByRole('article')
112
+ expect(card).not.toHaveAttribute('aria-labelledby')
113
+ expect(card).toHaveAccessibleName('')
114
+ })
36
115
 
37
- const headerText = screen.getByLabelText('Card-Header')
38
- expect(headerText).toBeInTheDocument()
116
+ it('should say which property is missing when as is left out', async () => {
117
+ // React would otherwise report an invalid element type and suggest a missing
118
+ // export, which points nowhere near the actual mistake.
119
+ const Header = CardHeader as unknown as React.ComponentType<{
120
+ children: React.ReactNode
121
+ }>
39
122
 
40
- const body = screen.getByTestId('body')
41
- expect(body).toBeInTheDocument()
42
- expect(body).toHaveAttribute('id')
123
+ expect(() =>
124
+ render(
125
+ <Card>
126
+ <Header>Card-Header</Header>
127
+ </Card>
128
+ )
129
+ ).toThrow(/CardHeader: the "as" property is required/)
130
+ })
131
+
132
+ it('should pass unknown properties through to the elements', async () => {
133
+ render(
134
+ <Card>
135
+ <CardHeader as="h3" data-testid="header" data-kind="title">
136
+ Card-Header
137
+ </CardHeader>
138
+ <CardBody data-testid="body" data-kind="desc">
139
+ Card-Body
140
+ </CardBody>
141
+ <CardFooter data-testid="footer" data-kind="meta">
142
+ Card-Footer
143
+ </CardFooter>
144
+ </Card>
145
+ )
43
146
 
44
- const bodyText = screen.getByRole('article', { description: 'Card-Body' })
45
- expect(bodyText).toBeInTheDocument()
147
+ expect(screen.getByTestId('header')).toHaveAttribute('data-kind', 'title')
148
+ expect(screen.getByTestId('body')).toHaveAttribute('data-kind', 'desc')
149
+ expect(screen.getByTestId('footer')).toHaveAttribute('data-kind', 'meta')
46
150
  })
47
151
  })
@@ -0,0 +1,75 @@
1
+ import React from 'react'
2
+ import { describe, expect, it } from 'vitest'
3
+ import { render, screen } from '@testing-library/react'
4
+
5
+ import { Answer, Faq, Question } from '../src'
6
+
7
+ describe('Faq', () => {
8
+ it('should render the questions and the answers', async () => {
9
+ render(
10
+ <Faq>
11
+ <Question>Question-1</Question>
12
+ <Answer>Answer-1</Answer>
13
+ <Question>Question-2</Question>
14
+ <Answer>Answer-2</Answer>
15
+ </Faq>
16
+ )
17
+
18
+ expect(screen.getByText('Question-1')).toBeInTheDocument()
19
+ expect(screen.getByText('Answer-1')).toBeInTheDocument()
20
+ expect(screen.getByText('Question-2')).toBeInTheDocument()
21
+ expect(screen.getByText('Answer-2')).toBeInTheDocument()
22
+ })
23
+
24
+ it('should keep the Q and A markers out of the accessibility tree', async () => {
25
+ render(
26
+ <Faq>
27
+ <Question data-testid="question">Question-1</Question>
28
+ <Answer>Answer-1</Answer>
29
+ </Faq>
30
+ )
31
+
32
+ // The letters are there for the eye only; a reader gets the question text.
33
+ const marker = screen.getByTestId('question').firstElementChild
34
+ expect(marker).toHaveTextContent('Q')
35
+ expect(marker).toHaveAttribute('aria-hidden', 'true')
36
+ })
37
+
38
+ it('should render a plain definition list', async () => {
39
+ const { container } = render(
40
+ <Faq>
41
+ <Question>Question-1</Question>
42
+ <Answer>Answer-1</Answer>
43
+ <Question>Question-2</Question>
44
+ <Answer>Answer-2</Answer>
45
+ </Faq>
46
+ )
47
+
48
+ // The schema.org markup described the whole list as one Question carrying a
49
+ // name and an answer per pair, and it wrapped everything in an article that
50
+ // nothing could name. Both are gone.
51
+ expect(container.querySelector('[itemscope]')).toBeNull()
52
+ expect(container.querySelector('[itemprop]')).toBeNull()
53
+ expect(container.querySelector('article')).toBeNull()
54
+ expect(container.querySelector('dl')).toBeInTheDocument()
55
+ expect(container.querySelectorAll('dt')).toHaveLength(2)
56
+ expect(container.querySelectorAll('dd')).toHaveLength(2)
57
+ })
58
+
59
+ it('should pass unknown properties through to the elements', async () => {
60
+ render(
61
+ <Faq data-testid="faq" data-kind="list">
62
+ <Question data-testid="question" data-kind="q">
63
+ Question-1
64
+ </Question>
65
+ <Answer data-testid="answer" data-kind="a">
66
+ Answer-1
67
+ </Answer>
68
+ </Faq>
69
+ )
70
+
71
+ expect(screen.getByTestId('faq')).toHaveAttribute('data-kind', 'list')
72
+ expect(screen.getByTestId('question')).toHaveAttribute('data-kind', 'q')
73
+ expect(screen.getByTestId('answer')).toHaveAttribute('data-kind', 'a')
74
+ })
75
+ })
@@ -0,0 +1,151 @@
1
+ import React from 'react'
2
+ import { describe, expect, it, vi } from 'vitest'
3
+ import { render, screen } from '@testing-library/react'
4
+
5
+ import { CardBody, LinkCard, LinkCardFooter, LinkCardHeader } from '../src'
6
+
7
+ describe('LinkCard', () => {
8
+ it('should render the title as a link', async () => {
9
+ render(
10
+ <LinkCard>
11
+ <LinkCardHeader as="h3" href="/readme">
12
+ Link-Card-Header
13
+ </LinkCardHeader>
14
+ <CardBody>Link-Card-Body</CardBody>
15
+ </LinkCard>
16
+ )
17
+
18
+ const link = screen.getByRole('link')
19
+ expect(link).toHaveAttribute('href', '/readme')
20
+ })
21
+
22
+ it('should take the accessible name of the link from the title alone', async () => {
23
+ render(
24
+ <LinkCard>
25
+ <LinkCardHeader as="h3" href="/readme">
26
+ Link-Card-Header
27
+ </LinkCardHeader>
28
+ <CardBody>Link-Card-Body</CardBody>
29
+ <LinkCardFooter>June 27th, 2026</LinkCardFooter>
30
+ </LinkCard>
31
+ )
32
+
33
+ // Asserting the accessible name rather than the link text: when the anchor
34
+ // wrapped the whole card, the body and the footer were read out as part of
35
+ // the link name before it was announced as a link.
36
+ const link = screen.getByRole('link')
37
+ expect(link).toHaveAccessibleName('Link-Card-Header')
38
+ expect(link).not.toHaveAccessibleName(/Link-Card-Body/)
39
+ })
40
+
41
+ it('should keep the title reachable by heading navigation', async () => {
42
+ render(
43
+ <LinkCard>
44
+ <LinkCardHeader as="h3" href="/readme">
45
+ Link-Card-Header
46
+ </LinkCardHeader>
47
+ </LinkCard>
48
+ )
49
+
50
+ expect(
51
+ screen.getByRole('heading', { level: 3, name: 'Link-Card-Header' })
52
+ ).toBeInTheDocument()
53
+ })
54
+
55
+ it('should tell that the link opens in a new tab', async () => {
56
+ render(
57
+ <LinkCard>
58
+ <LinkCardHeader as="h3" href="https://example.com" target="_blank">
59
+ Link-Card-Header
60
+ </LinkCardHeader>
61
+ </LinkCard>
62
+ )
63
+
64
+ // The icon itself is aria-hidden, so without the alternative text the fact
65
+ // that the link opens elsewhere reached sighted users only.
66
+ expect(screen.getByRole('link')).toHaveAccessibleName(
67
+ /新しいタブで開きます/
68
+ )
69
+ })
70
+
71
+ it('should not tell about a new tab for a link that stays in the tab', async () => {
72
+ render(
73
+ <LinkCard>
74
+ <LinkCardHeader as="h3" href="/readme">
75
+ Link-Card-Header
76
+ </LinkCardHeader>
77
+ </LinkCard>
78
+ )
79
+
80
+ expect(screen.getByRole('link')).toHaveAccessibleName('Link-Card-Header')
81
+ })
82
+
83
+ it('should render the link with the component given by linkAs', async () => {
84
+ const NextLinkLike = ({
85
+ to,
86
+ children,
87
+ ...rest
88
+ }: { to: string; children: React.ReactNode }) => (
89
+ <a href={to} {...rest}>
90
+ {children}
91
+ </a>
92
+ )
93
+
94
+ render(
95
+ <LinkCard>
96
+ <LinkCardHeader as="h3" linkAs={NextLinkLike} to="/readme">
97
+ Link-Card-Header
98
+ </LinkCardHeader>
99
+ </LinkCard>
100
+ )
101
+
102
+ expect(screen.getByRole('link')).toHaveAttribute('href', '/readme')
103
+ })
104
+
105
+ it('should pass unknown properties through to the link and the footer', async () => {
106
+ render(
107
+ <LinkCard data-testid="card">
108
+ <LinkCardHeader as="h3" href="/readme" data-kind="title">
109
+ Link-Card-Header
110
+ </LinkCardHeader>
111
+ <LinkCardFooter data-testid="footer" data-kind="meta">
112
+ June 27th, 2026
113
+ </LinkCardFooter>
114
+ </LinkCard>
115
+ )
116
+
117
+ // These used to be dropped: both components declared that they accept the
118
+ // properties of a div but never spread them onto an element.
119
+ expect(screen.getByRole('link')).toHaveAttribute('data-kind', 'title')
120
+ expect(screen.getByTestId('footer')).toHaveAttribute('data-kind', 'meta')
121
+ expect(screen.getByTestId('card')).toBeInTheDocument()
122
+ })
123
+
124
+ it('should say which property is missing when as is left out', async () => {
125
+ const Header = LinkCardHeader as unknown as React.ComponentType<{
126
+ href: string
127
+ children: React.ReactNode
128
+ }>
129
+
130
+ expect(() =>
131
+ render(
132
+ <LinkCard>
133
+ <Header href="/readme">Link-Card-Header</Header>
134
+ </LinkCard>
135
+ )
136
+ ).toThrow(/LinkCardHeader: the "as" property is required/)
137
+ })
138
+
139
+ it('should pass an object to the ref property', async () => {
140
+ const ref = vi.fn()
141
+ render(
142
+ <LinkCard ref={ref}>
143
+ <LinkCardHeader as="h3" href="/readme">
144
+ Link-Card-Header
145
+ </LinkCardHeader>
146
+ </LinkCard>
147
+ )
148
+
149
+ expect(ref).toHaveBeenCalledTimes(1)
150
+ })
151
+ })
@@ -9,4 +9,4 @@ rendering chunks...
9
9
  computing gzip size...
10
10
  dist/index.es.js 24.36 kB │ gzip: 6.13 kB
11
11
  dist/index.cjs.js 16.94 kB │ gzip: 5.27 kB
12
- ✓ built in 582ms
12
+ ✓ built in 375ms
@@ -9,4 +9,4 @@ rendering chunks...
9
9
  computing gzip size...
10
10
  dist/index.es.js 2.90 kB │ gzip: 1.00 kB
11
11
  dist/index.cjs.js 2.72 kB │ gzip: 0.99 kB
12
- ✓ built in 247ms
12
+ ✓ built in 168ms
@@ -1,12 +1,12 @@
1
1
  $ rimraf dist
2
2
  $ run-p build:*
3
3
  $ vite build
4
- vite v7.3.6 building client environment for production...
5
4
  $ tsc && tsc-alias
5
+ vite v7.3.6 building client environment for production...
6
6
  transforming...
7
7
  ✓ 365 modules transformed.
8
8
  rendering chunks...
9
9
  computing gzip size...
10
- dist/index.es.js 554.27 kB │ gzip: 141.23 kB
11
- dist/index.cjs.js 377.84 kB │ gzip: 117.18 kB
12
- ✓ built in 4.31s
10
+ dist/index.es.js 554.82 kB │ gzip: 141.39 kB
11
+ dist/index.cjs.js 378.23 kB │ gzip: 117.29 kB
12
+ ✓ built in 2.18s