Tldraw/packages/editor/src/lib/test/commands/stackShapes.test.ts

228 wiersze
4.4 KiB
TypeScript
Czysty Zwykły widok Historia

2023-04-25 11:01:25 +00:00
import { createCustomShapeId } from '@tldraw/tlschema'
import { TestApp } from '../TestApp'
jest.useFakeTimers()
let app: TestApp
const ids = {
boxA: createCustomShapeId('boxA'),
boxB: createCustomShapeId('boxB'),
boxC: createCustomShapeId('boxC'),
boxD: createCustomShapeId('boxD'),
}
beforeEach(() => {
app = new TestApp()
})
describe('distributeShapes command', () => {
beforeEach(() => {
app.selectAll()
app.deleteShapes()
app.createShapes([
{
id: ids.boxA,
type: 'geo',
x: 0,
y: 0,
},
{
id: ids.boxB,
type: 'geo',
x: 100,
y: 100,
},
{
id: ids.boxC,
type: 'geo',
x: 400,
y: 400,
},
{
id: ids.boxD,
type: 'geo',
x: 700,
y: 700,
},
])
})
describe('when less than three shapes are selected', () => {
it('does nothing', () => {
app.setSelectedIds([ids.boxA, ids.boxB])
[feature] ui events (#1326) This PR updates the editor events: - adds types to the events emitted by the app (by `app.emit`) - removes a few events emitted by the app (e.g. `move-to-page`, `change-camera`) - adds `onEvent` prop to the <TldrawUi> / <Tldraw> components - call the `onEvent` when actions occur or tools are selected - does some superficial cleanup on editor app APIs ### Release Note - Fix layout bug in error dialog - (ui) Add `TLEventMap` for types emitted from editor app - (editor) Update `crash` event emitted from editor app to include error - (editor) Update `change-history` event emitted from editor app - (editor) Remove `change-camera` event from editor app - (editor) Remove `move-to-page` event from editor app - (ui) Add `onEvent` prop and events to <Tldraw> / <TldrawUi> - (editor) Replace `app.openMenus` plain Set with computed value - (editor) Add `addOpenMenu` method - (editor) Add `removeOpenMenu` method - (editor) Add `setFocusMode` method - (editor) Add `setToolLocked` method - (editor) Add `setSnapMode` method - (editor) Add `isSnapMode` method - (editor) Update `setGridMode` method return type to editor app - (editor) Update `setReadOnly` method return type to editor app - (editor) Update `setPenMode` method return type to editor app - (editor) Update `selectNone` method return type to editor app - (editor) Rename `backToContent` to `zoomToContent` - (editor) Remove `TLReorderOperation` type --------- Co-authored-by: Orange Mug <orangemug@users.noreply.github.com>
2023-05-11 22:14:58 +00:00
const fn = jest.fn()
app.on('change-history', fn)
2023-04-25 11:01:25 +00:00
app.stackShapes('horizontal')
jest.advanceTimersByTime(1000)
expect(fn).not.toHaveBeenCalled()
})
})
describe('when stacking horizontally', () => {
it('stacks the shapes based on a given value', () => {
app.setSelectedIds([ids.boxA, ids.boxB, ids.boxC, ids.boxD])
app.stackShapes('horizontal', app.selectedIds, 10)
jest.advanceTimersByTime(1000)
// 200 distance gap between c and d
app.expectShapeToMatch({
id: ids.boxA,
x: 0,
y: 0,
})
app.expectShapeToMatch({
id: ids.boxB,
x: 110,
y: 100,
})
app.expectShapeToMatch({
id: ids.boxC,
x: 220,
y: 400,
})
app.expectShapeToMatch({
id: ids.boxD,
x: 330,
y: 700,
})
})
it('stacks the shapes based on the most common gap', () => {
app.setSelectedIds([ids.boxA, ids.boxB, ids.boxC, ids.boxD])
app.stackShapes('horizontal')
jest.advanceTimersByTime(1000)
// 200 distance gap between c and d
app.expectShapeToMatch({
id: ids.boxA,
x: 0,
y: 0,
})
app.expectShapeToMatch({
id: ids.boxB,
x: 300,
y: 100,
})
app.expectShapeToMatch({
id: ids.boxC,
x: 600,
y: 400,
})
app.expectShapeToMatch({
id: ids.boxD,
x: 900,
y: 700,
})
})
it('stacks the shapes based on the average', () => {
app.updateShapes([{ id: ids.boxD, type: 'geo', x: 540, y: 700 }])
app.setSelectedIds([ids.boxA, ids.boxB, ids.boxC, ids.boxD])
app.stackShapes('horizontal')
jest.advanceTimersByTime(1000)
app.expectShapeToMatch({
id: ids.boxA,
x: 0,
y: 0,
})
app.expectShapeToMatch({
id: ids.boxB,
x: 180,
y: 100,
})
app.expectShapeToMatch({
id: ids.boxC,
x: 360,
y: 400,
})
app.expectShapeToMatch({
id: ids.boxD,
x: 540,
y: 700,
})
})
})
describe('when stacking vertically', () => {
it('stacks the shapes based on a given value', () => {
app.setSelectedIds([ids.boxA, ids.boxB, ids.boxC, ids.boxD])
app.stackShapes('vertical', app.selectedIds, 10)
jest.advanceTimersByTime(1000)
// 200 distance gap between c and d
app.expectShapeToMatch({
id: ids.boxA,
x: 0,
y: 0,
})
app.expectShapeToMatch({
id: ids.boxB,
x: 100,
y: 110,
})
app.expectShapeToMatch({
id: ids.boxC,
x: 400,
y: 220,
})
app.expectShapeToMatch({
id: ids.boxD,
x: 700,
y: 330,
})
})
it('stacks the shapes based on the most common gap', () => {
app.setSelectedIds([ids.boxA, ids.boxB, ids.boxC, ids.boxD])
app.stackShapes('vertical')
jest.advanceTimersByTime(1000)
// 200 distance gap between c and d
app.expectShapeToMatch({
id: ids.boxA,
x: 0,
y: 0,
})
app.expectShapeToMatch({
id: ids.boxB,
x: 100,
y: 300,
})
app.expectShapeToMatch({
id: ids.boxC,
x: 400,
y: 600,
})
app.expectShapeToMatch({
id: ids.boxD,
x: 700,
y: 900,
})
})
it('stacks the shapes based on the average', () => {
app.updateShapes([{ id: ids.boxD, type: 'geo', x: 700, y: 540 }])
app.setSelectedIds([ids.boxA, ids.boxB, ids.boxC, ids.boxD])
app.stackShapes('vertical')
jest.advanceTimersByTime(1000)
app.expectShapeToMatch({
id: ids.boxA,
x: 0,
y: 0,
})
app.expectShapeToMatch({
id: ids.boxB,
x: 100,
y: 180,
})
app.expectShapeToMatch({
id: ids.boxC,
x: 400,
y: 360,
})
app.expectShapeToMatch({
id: ids.boxD,
x: 700,
y: 540,
})
})
})
})