import { render } from '@testing-library/react' import * as React from 'react' import { SVGContainer } from '~components' import type { TLBounds, TLComponentProps, TLShape } from '~types' import Utils from '~utils' import { TLShapeUtil } from './TLShapeUtil' export interface BoxShape extends TLShape { type: 'box' size: number[] } const meta = { legs: 93 } type Meta = typeof meta export const boxShape: BoxShape = { id: 'example1', type: 'box', parentId: 'page', childIndex: 0, name: 'Example Shape', point: [0, 0], size: [100, 100], rotation: 0, } export class BoxUtil extends TLShapeUtil { age = 100 Component = TLShapeUtil.Component(({ shape, events, meta }, ref) => { type T = typeof meta.legs type C = T['toFixed'] return ( ) }) Indicator = TLShapeUtil.Indicator(({ shape }) => { return ( ) }) getBounds = (shape: BoxShape) => { const bounds = Utils.getFromCache(this.boundsCache, shape, () => { const [width, height] = shape.size return { minX: 0, maxX: width, minY: 0, maxY: height, width, height, } as TLBounds }) return Utils.translateBounds(bounds, shape.point) } } describe('When creating a minimal ShapeUtil', () => { const Box = new BoxUtil() it('creates a shape utils', () => { expect(Box).toBeTruthy() }) test('accesses this in an override method', () => { expect(Box.shouldRender(boxShape, { ...boxShape, point: [1, 1] })).toBe(true) }) test('mounts component without crashing', () => { const ref = React.createRef() const ref2 = React.createRef() const H = React.forwardRef((props, ref) => { return
{props.message}
}) render() render( ) }) }) abstract class TLRealisticShapeUtil< T extends TLShape, E extends Element = any, M = any > extends TLShapeUtil { abstract type: T['type'] abstract getShape: (props: Partial) => T create = (props: { id: string } & Partial) => { return this.getShape(props) } } describe('When creating a realistic API around TLShapeUtil', () => { class ExtendedBoxUtil extends TLRealisticShapeUtil { type = 'box' as const age = 100 Component = TLShapeUtil.Component(({ shape, events, meta }, ref) => { type T = typeof meta.legs type C = T['toFixed'] return ( ) }) Indicator = TLShapeUtil.Indicator(({ shape }) => { return ( ) }) getShape = (props: Partial): BoxShape => ({ id: 'example1', type: 'box', parentId: 'page', childIndex: 0, name: 'Example Shape', point: [0, 0], size: [100, 100], rotation: 0, ...props, }) getBounds = (shape: BoxShape) => { const bounds = Utils.getFromCache(this.boundsCache, shape, () => { const [width, height] = shape.size return { minX: 0, maxX: width, minY: 0, maxY: height, width, height, } as TLBounds }) return Utils.translateBounds(bounds, shape.point) } } const Box = new ExtendedBoxUtil() it('creates a shape utils', () => { expect(Box).toBeTruthy() }) it('creates a shape utils with extended properties', () => { expect(Box.age).toBe(100) }) it('creates a shape', () => { expect(Box.create({ id: 'box1' })).toStrictEqual({ ...boxShape, id: 'box1', }) }) test('accesses this in an override method', () => { expect(Box.shouldRender(boxShape, { ...boxShape, point: [1, 1] })).toBe(true) }) test('mounts component without crashing', () => { const box = Box.create({ id: 'box1' }) const ref = React.createRef() const ref2 = React.createRef() const H = React.forwardRef((props, ref) => { return
{props.message}
}) render() render( ) }) })