Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

test-utils.ts 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { Data, Shape, ShapeType } from 'types'
  2. import { getSelectedIds, getSelectedShapes, getShape } from 'utils'
  3. export const rectangleId = '1f6c251c-e12e-40b4-8dd2-c1847d80b72f'
  4. export const arrowId = '5ca167d7-54de-47c9-aa8f-86affa25e44d'
  5. interface PointerOptions {
  6. id?: string
  7. x?: number
  8. y?: number
  9. shiftKey?: boolean
  10. altKey?: boolean
  11. ctrlKey?: boolean
  12. }
  13. export function point(
  14. options: PointerOptions = {} as PointerOptions
  15. ): PointerEvent {
  16. const {
  17. id = '1',
  18. x = 0,
  19. y = 0,
  20. shiftKey = false,
  21. altKey = false,
  22. ctrlKey = false,
  23. } = options
  24. return {
  25. shiftKey,
  26. altKey,
  27. ctrlKey,
  28. pointerId: id,
  29. clientX: x,
  30. clientY: y,
  31. } as any
  32. }
  33. export function idsAreSelected(
  34. data: Data,
  35. ids: string[],
  36. strict = true
  37. ): boolean {
  38. const selectedIds = getSelectedIds(data)
  39. return (
  40. (strict ? selectedIds.size === ids.length : true) &&
  41. ids.every((id) => selectedIds.has(id))
  42. )
  43. }
  44. export function hasParent(
  45. data: Data,
  46. childId: string,
  47. parentId: string
  48. ): boolean {
  49. return getShape(data, childId).parentId === parentId
  50. }
  51. export function getOnlySelectedShape(data: Data): Shape {
  52. const selectedShapes = getSelectedShapes(data)
  53. return selectedShapes.length === 1 ? selectedShapes[0] : undefined
  54. }
  55. export function assertShapeType(
  56. data: Data,
  57. shapeId: string,
  58. type: ShapeType
  59. ): boolean {
  60. const shape = getShape(data, shapeId)
  61. if (shape.type !== type) {
  62. throw new TypeError(
  63. `expected shape ${shapeId} to be of type ${type}, found ${shape?.type} instead`
  64. )
  65. }
  66. return true
  67. }
  68. export function assertShapeProps<T extends Shape>(
  69. shape: T,
  70. props: { [K in keyof Partial<T>]: T[K] }
  71. ): boolean {
  72. for (const key in props) {
  73. if (shape[key] !== props[key]) {
  74. throw new TypeError(
  75. `expected shape ${shape.id} to have property ${key}: ${props[key]}, found ${key}: ${shape[key]} instead`
  76. )
  77. }
  78. }
  79. return true
  80. }