|
1 | 1 | import React from 'react'; |
2 | | -import { describe, expect, it, render, waitFor } from '@test/utils'; |
| 2 | +import { describe, expect, it, render } from '@test/utils'; |
3 | 3 | import Divider from '../Divider'; |
4 | | -import { TdDividerProps } from '../type'; |
5 | 4 |
|
6 | 5 | describe('Divider', () => { |
7 | 6 | describe('props', () => { |
8 | | - it(':align', async () => { |
9 | | - const testId = 'divider test align'; |
10 | | - const aligns: TdDividerProps['align'][] = ['left', 'right', 'center']; |
11 | | - const { getByTestId } = render( |
12 | | - <div data-testid={testId}>{aligns?.map((align, index) => <Divider key={index} align={align} />)}</div>, |
13 | | - ); |
14 | | - |
15 | | - const instance = await waitFor(() => getByTestId(testId)); |
| 7 | + it(':align', () => { |
| 8 | + const aligns = ['left', 'right', 'center'] as const; |
16 | 9 |
|
17 | | - for (let index = 0; index < aligns.length; index++) { |
18 | | - const align = aligns[index]; |
19 | | - expect(() => instance.querySelector(`.t-divider--${align}`)).toBeTruthy(); |
| 10 | + function checkAlign(align: (typeof aligns)[number]) { |
| 11 | + const { container } = render(<Divider align={align}>Text</Divider>); |
| 12 | + expect(container.firstChild).toHaveClass(`t-divider--${align}`); |
20 | 13 | } |
21 | | - }); |
22 | 14 |
|
23 | | - it(':layout', async () => { |
24 | | - const testId = 'divider test layout'; |
25 | | - const layouts: TdDividerProps['layout'][] = ['horizontal', 'vertical']; |
26 | | - const { getByTestId } = render( |
27 | | - <div data-testid={testId}>{layouts?.map((layout, index) => <Divider key={index} layout={layout} />)}</div>, |
28 | | - ); |
| 15 | + aligns.forEach(checkAlign); |
| 16 | + }); |
29 | 17 |
|
30 | | - const instance = await waitFor(() => getByTestId(testId)); |
| 18 | + it(':layout', () => { |
| 19 | + const layouts = ['horizontal', 'vertical'] as const; |
31 | 20 |
|
32 | | - for (let index = 0; index < layouts.length; index++) { |
33 | | - const layout = layouts[index]; |
34 | | - expect(() => instance.querySelector(`.t-divider--${layout}`)).toBeTruthy(); |
| 21 | + function checkLayout(layout: (typeof layouts)[number]) { |
| 22 | + const { container } = render(<Divider layout={layout} />); |
| 23 | + expect(container.firstChild).toHaveClass(`t-divider--${layout}`); |
35 | 24 | } |
| 25 | + |
| 26 | + layouts.forEach(checkLayout); |
36 | 27 | }); |
37 | 28 |
|
38 | | - it(':dashed', async () => { |
39 | | - const { container } = render(<Divider dashed />); |
40 | | - const dividerElement = container.firstChild; |
41 | | - expect(dividerElement).toHaveClass('t-divider--dashed'); |
| 29 | + it(':content', () => { |
| 30 | + // Test content as a function |
| 31 | + const { container: container1 } = render(<Divider content={() => <span className="custom-node">TNode</span>} />); |
| 32 | + expect(container1.querySelector('.custom-node')).toBeTruthy(); |
| 33 | + expect(container1).toMatchSnapshot(); |
| 34 | + |
| 35 | + // Test content as a string |
| 36 | + const { container: container2 } = render(<Divider content="TDesign" />); |
| 37 | + expect(container2.textContent).toBe('TDesign'); |
| 38 | + expect(container2).toMatchSnapshot(); |
42 | 39 | }); |
43 | 40 |
|
44 | | - it(':content', async () => { |
45 | | - const content = 'DividerContent'; |
46 | | - const { getByText } = render(<Divider content={content}></Divider>); |
47 | | - expect(getByText(content).textContent).toBeTruthy(); |
| 41 | + it(':dashed', () => { |
| 42 | + // Test default value (false) |
| 43 | + const { container: container1 } = render(<Divider />); |
| 44 | + expect(container1.firstChild).not.toHaveClass('t-divider--dashed'); |
| 45 | + |
| 46 | + // Test dashed = true |
| 47 | + const { container: container2 } = render(<Divider dashed={true} />); |
| 48 | + expect(container2.firstChild).toHaveClass('t-divider--dashed'); |
| 49 | + |
| 50 | + // Test dashed = false |
| 51 | + const { container: container3 } = render(<Divider dashed={false} />); |
| 52 | + expect(container3.firstChild).not.toHaveClass('t-divider--dashed'); |
48 | 53 | }); |
49 | 54 | }); |
50 | 55 |
|
51 | | - describe('function', () => { |
52 | | - it(':content', async () => { |
53 | | - const content = () => 'DividerContent'; |
54 | | - const { getByText } = render(<Divider>{content}</Divider>); |
55 | | - expect(getByText(content()).textContent).toBeTruthy(); |
| 56 | + describe('slot', () => { |
| 57 | + it(':default', () => { |
| 58 | + const { container } = render(<Divider>TDesign</Divider>); |
| 59 | + expect(container).toMatchSnapshot(); |
| 60 | + }); |
| 61 | + |
| 62 | + it(':content', () => { |
| 63 | + const testSlotContentId = 'divider-content-test-slot-id'; |
| 64 | + const { container } = render( |
| 65 | + <Divider> |
| 66 | + <span className="custom-node" data-testid={testSlotContentId}> |
| 67 | + TNode |
| 68 | + </span> |
| 69 | + </Divider>, |
| 70 | + ); |
| 71 | + expect(container.querySelector('.custom-node')).toBeTruthy(); |
| 72 | + expect(container.querySelector(`[data-testid="${testSlotContentId}"]`)).toBeTruthy(); |
| 73 | + expect(container).toMatchSnapshot(); |
56 | 74 | }); |
57 | 75 | }); |
58 | 76 |
|
59 | | - describe('slot', () => { |
60 | | - it(':children', async () => { |
61 | | - const content = 'DividerContent'; |
62 | | - const { getByText } = render(<Divider>{content}</Divider>); |
63 | | - expect(getByText(content).textContent).toBeTruthy(); |
| 77 | + it('content prop and default slot exist meanwhile', () => { |
| 78 | + const { container } = render(<Divider content="prop content">TDesign</Divider>); |
| 79 | + expect(container.textContent).toBe('prop content'); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('vertical divider', () => { |
| 83 | + it('applies dashed class on vertical divider', () => { |
| 84 | + const { container } = render(<Divider layout="vertical" dashed={true} />); |
| 85 | + expect(container.firstChild).toHaveClass('t-divider--dashed'); |
| 86 | + expect(container).toMatchSnapshot(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('applies align class on vertical divider', () => { |
| 90 | + const { container } = render( |
| 91 | + <Divider layout="vertical" align="left"> |
| 92 | + text |
| 93 | + </Divider>, |
| 94 | + ); |
| 95 | + expect(container.firstChild).toHaveClass('t-divider--left'); |
| 96 | + expect(container).toMatchSnapshot(); |
64 | 97 | }); |
65 | 98 | }); |
66 | 99 | }); |
0 commit comments