import { LABEL } from '@adalo/constants' import { getOriginalLowerEdge, getLowerEdge, getYOffset, getOriginalUpperBound, isSection, SECTION_TYPES, } from './runtime' import { mockDataNode, mockPushNode } from './testutils' describe('PushTree - runtime functions', () => { test('getOriginalLowerEdge', () => { const originalY = 10 const originalHeight = 20 const pushNode = mockPushNode({ originalY, originalHeight }) const result = getOriginalLowerEdge(pushNode) expect(result).toBe(originalY + originalHeight) }) test('getLowerEdge', () => { const originalY = 10 const height = 20 const yOffset = 30 const dataNode = mockDataNode({ height }) const pushNode = mockPushNode({ originalY, yOffset, dataNode }) const result = getLowerEdge(pushNode) expect(result).toBe(originalY + height + yOffset) }) describe('getYOffset', () => { test('with a single pushing node', () => { const dataNode = mockDataNode({ height: 20 }) const pushingNode = mockPushNode({ originalY: 10, originalHeight: 5, yOffset: 30, dataNode, }) const pushNode = mockPushNode({ pushedBy: [pushingNode], }) const originalLowerEdge = getOriginalLowerEdge(pushingNode) const lowerEdge = getLowerEdge(pushingNode) const result = getYOffset(pushNode) const expectedHeight = pushingNode?.dataNode?.height || 0 expect(expectedHeight).not.toBe(undefined) expect(lowerEdge - originalLowerEdge).toBe(result) expect(result).toBe( expectedHeight + pushingNode.yOffset - pushingNode.originalHeight ) }) describe('with two aligned pushing nodes', () => { test('one initially bigger', () => { const finallyTallerData = mockDataNode({ height: 20, }) const finallyTallerNode = mockPushNode({ originalY: 10, originalHeight: 5, yOffset: 30, dataNode: finallyTallerData, }) const initiallyTallerData = mockDataNode({ height: 15, }) const initiallyTallerNode = mockPushNode({ originalY: 10, originalHeight: 10, yOffset: 30, dataNode: initiallyTallerData, }) const pushNode = mockPushNode({ pushedBy: [initiallyTallerNode, finallyTallerNode], }) const originalLowerEdge = getOriginalLowerEdge(initiallyTallerNode) const lowerEdge = getLowerEdge(finallyTallerNode) const result = getYOffset(pushNode) const expectedYOffset = lowerEdge - originalLowerEdge expect(result).toBe(expectedYOffset) }) test('one initially lower', () => { const finallyLowerData = mockDataNode({ height: 20, }) const finallyLowerNode = mockPushNode({ originalY: 10, originalHeight: 5, yOffset: 40, dataNode: finallyLowerData, }) const initiallyLowerData = mockDataNode({ height: 20, }) const initiallyLowerNode = mockPushNode({ originalY: 10, originalHeight: 5, yOffset: 30, dataNode: initiallyLowerData, }) const pushNode = mockPushNode({ pushedBy: [initiallyLowerNode, finallyLowerNode], }) const originalLowerEdge = getOriginalLowerEdge(initiallyLowerNode) const lowerEdge = getLowerEdge(finallyLowerNode) const result = getYOffset(pushNode) const expectedYOffset = lowerEdge - originalLowerEdge expect(result).toBe(expectedYOffset) }) }) }) describe('getOriginalUpperBound', () => { test('bottom element is the upper bound when height is the same', () => { const topNode = mockPushNode({ originalY: 10, originalHeight: 5, }) const midNode = mockPushNode({ originalY: 20, originalHeight: 5, }) const bottomNode = mockPushNode({ originalY: 30, originalHeight: 5, }) const result = getOriginalUpperBound([topNode, midNode, bottomNode]) const bottomOriginalLowerEdge = getOriginalLowerEdge(bottomNode) expect(result).toBe(bottomOriginalLowerEdge) }) test('tallest element is the upper bound when y is the same', () => { const shortNode = mockPushNode({ originalY: 10, originalHeight: 5, }) const avgNode = mockPushNode({ originalY: 10, originalHeight: 10, }) const tallNode = mockPushNode({ originalY: 10, originalHeight: 20, }) const result = getOriginalUpperBound([shortNode, avgNode, tallNode]) const tallOriginalLowerEdge = getOriginalLowerEdge(tallNode) expect(result).toBe(tallOriginalLowerEdge) }) test('the element with the biggest y + height is the upper bound', () => { const lowerNode = mockPushNode({ originalY: 30, originalHeight: 30, }) const nodeA = mockPushNode({ originalY: 31, originalHeight: 28, }) const nodeB = mockPushNode({ originalY: 28, originalHeight: 31, }) const result = getOriginalUpperBound([lowerNode, nodeA, nodeB]) const expectedOriginalLowerEdge = getOriginalLowerEdge(lowerNode) expect(result).toBe(expectedOriginalLowerEdge) }) }) describe('isSection', () => { test.each(Array.from(SECTION_TYPES))('returns true for %s', type => { const pushNode = mockPushNode({ type }) const result = isSection(pushNode) expect(result).toBe(true) }) // not exhaustive test.each([LABEL])('returns false for %s', type => { const pushNode = mockPushNode({ type }) const result = isSection(pushNode) expect(result).toBe(false) }) }) })