|
1 | 1 | import React from 'react'; |
2 | | -import { shallow } from 'enzyme'; |
3 | | -import { Annotation, EditableAnnotation } from '../src'; |
4 | | -import { EditableAnnotationProps } from '../lib/components/EditableAnnotation'; |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | +import '@testing-library/jest-dom'; |
| 4 | + |
| 5 | +import EditableAnnotation from '../src/components/EditableAnnotation'; |
5 | 6 |
|
6 | 7 | describe('<EditableAnnotation />', () => { |
7 | | - function setup(props?: Partial<EditableAnnotationProps>) { |
8 | | - return shallow( |
9 | | - <EditableAnnotation width={100} height={100} {...props}> |
10 | | - <div /> |
11 | | - </EditableAnnotation>, |
| 8 | + type EditableAnnotationProps = React.ComponentProps<typeof EditableAnnotation>; |
| 9 | + |
| 10 | + const defaultProps: EditableAnnotationProps = { |
| 11 | + width: 100, |
| 12 | + height: 100, |
| 13 | + x: 0, |
| 14 | + y: 0, |
| 15 | + dx: 0, |
| 16 | + dy: 0, |
| 17 | + children: <div data-testid="child-content">Child content</div>, |
| 18 | + }; |
| 19 | + |
| 20 | + function renderComponent(props?: Partial<EditableAnnotationProps>) { |
| 21 | + return render( |
| 22 | + <svg> |
| 23 | + <EditableAnnotation {...defaultProps} {...props} /> |
| 24 | + </svg>, |
12 | 25 | ); |
13 | 26 | } |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + jest.clearAllMocks(); |
| 30 | + }); |
| 31 | + |
14 | 32 | it('should be defined', () => { |
15 | | - expect(EditableAnnotation).toBeDefined(); |
| 33 | + expect(() => renderComponent()).not.toThrow(); |
16 | 34 | }); |
17 | | - it('should render two resize handles', () => { |
18 | | - expect(setup().find('circle')).toHaveLength(2); |
| 35 | + |
| 36 | + it('should render two resize handles by default', () => { |
| 37 | + const { container } = renderComponent(); |
| 38 | + const circles = container.querySelectorAll('circle'); |
| 39 | + expect(circles).toHaveLength(2); |
19 | 40 | }); |
20 | | - it('should render one resize handle if canEditLabel or canEditSubject are false', () => { |
21 | | - expect(setup({ canEditLabel: false }).find('circle')).toHaveLength(1); |
22 | | - expect(setup({ canEditSubject: false }).find('circle')).toHaveLength(1); |
| 41 | + |
| 42 | + it('should render one resize handle if canEditLabel is false', () => { |
| 43 | + const { container } = renderComponent({ canEditLabel: false }); |
| 44 | + const circles = container.querySelectorAll('circle'); |
| 45 | + expect(circles).toHaveLength(1); |
23 | 46 | }); |
24 | | - it('should render an Annotation', () => { |
25 | | - expect(setup().find(Annotation)).toHaveLength(1); |
| 47 | + |
| 48 | + it('should render one resize handle if canEditSubject is false', () => { |
| 49 | + const { container } = renderComponent({ canEditSubject: false }); |
| 50 | + const circles = container.querySelectorAll('circle'); |
| 51 | + expect(circles).toHaveLength(1); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should render children content', () => { |
| 55 | + const { getByTestId } = renderComponent(); |
| 56 | + expect(getByTestId('child-content')).toBeInTheDocument(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should render with correct initial positions', () => { |
| 60 | + const { container } = renderComponent({ |
| 61 | + x: 10, |
| 62 | + y: 20, |
| 63 | + dx: 30, |
| 64 | + dy: 40, |
| 65 | + }); |
| 66 | + |
| 67 | + const circles = container.querySelectorAll('circle'); |
| 68 | + const [subjectHandle, labelHandle] = circles; |
| 69 | + |
| 70 | + expect(subjectHandle).toHaveAttribute('cx', '10'); |
| 71 | + expect(subjectHandle).toHaveAttribute('cy', '20'); |
| 72 | + expect(labelHandle).toHaveAttribute('cx', '40'); // x + dx |
| 73 | + expect(labelHandle).toHaveAttribute('cy', '60'); // y + dy |
26 | 74 | }); |
27 | 75 | }); |
0 commit comments