|
| 1 | +import { Entity, YGPositionType, YGUnit } from '../../packages/@dcl/ecs/dist' |
| 2 | +import { components } from '../../packages/@dcl/ecs/src' |
| 3 | +import { InteractableArea, ReactEcs } from '../../packages/@dcl/react-ecs/src' |
| 4 | +import { CANVAS_ROOT_ENTITY } from '../../packages/@dcl/react-ecs/src/components/uiTransform' |
| 5 | +import { resetInteractableArea } from '../../packages/@dcl/react-ecs/src/components/utils' |
| 6 | +import { setupEngine } from './utils' |
| 7 | + |
| 8 | +describe('InteractableArea React Ecs', () => { |
| 9 | + afterEach(() => { |
| 10 | + resetInteractableArea() |
| 11 | + }) |
| 12 | + |
| 13 | + it('positions itself absolutely using the renderer-reported interactable area', async () => { |
| 14 | + const { engine, uiRenderer } = setupEngine() |
| 15 | + const UiTransform = components.UiTransform(engine) |
| 16 | + const UiCanvasInformation = components.UiCanvasInformation(engine) |
| 17 | + const entityIndex = engine.addEntity() as number |
| 18 | + const rootDivEntity = (entityIndex + 1) as Entity |
| 19 | + |
| 20 | + UiCanvasInformation.create(engine.RootEntity, { |
| 21 | + devicePixelRatio: 1, |
| 22 | + width: 1920, |
| 23 | + height: 1080, |
| 24 | + screenInsetArea: undefined, |
| 25 | + interactableArea: { top: 0, left: 480, right: 0, bottom: 0 } |
| 26 | + }) |
| 27 | + |
| 28 | + uiRenderer.setUiRenderer(() => <InteractableArea />) |
| 29 | + await engine.update(1) |
| 30 | + |
| 31 | + expect(UiTransform.get(rootDivEntity)).toMatchObject({ |
| 32 | + parent: CANVAS_ROOT_ENTITY, |
| 33 | + positionType: YGPositionType.YGPT_ABSOLUTE, |
| 34 | + positionTop: 0, |
| 35 | + positionLeft: 480, |
| 36 | + positionRight: 0, |
| 37 | + positionBottom: 0, |
| 38 | + positionTopUnit: YGUnit.YGU_POINT, |
| 39 | + positionLeftUnit: YGUnit.YGU_POINT, |
| 40 | + positionRightUnit: YGUnit.YGU_POINT, |
| 41 | + positionBottomUnit: YGUnit.YGU_POINT |
| 42 | + }) |
| 43 | + |
| 44 | + uiRenderer.destroy() |
| 45 | + }) |
| 46 | + |
| 47 | + it('updates its position when the interactable area changes', async () => { |
| 48 | + const { engine, uiRenderer } = setupEngine() |
| 49 | + const UiTransform = components.UiTransform(engine) |
| 50 | + const UiCanvasInformation = components.UiCanvasInformation(engine) |
| 51 | + const entityIndex = engine.addEntity() as number |
| 52 | + const rootDivEntity = (entityIndex + 1) as Entity |
| 53 | + |
| 54 | + UiCanvasInformation.create(engine.RootEntity, { |
| 55 | + devicePixelRatio: 1, |
| 56 | + width: 1920, |
| 57 | + height: 1080, |
| 58 | + screenInsetArea: undefined, |
| 59 | + interactableArea: { top: 0, left: 0, right: 0, bottom: 0 } |
| 60 | + }) |
| 61 | + |
| 62 | + uiRenderer.setUiRenderer(() => <InteractableArea />) |
| 63 | + await engine.update(1) |
| 64 | + |
| 65 | + expect(UiTransform.get(rootDivEntity)).toMatchObject({ |
| 66 | + positionTop: 0, |
| 67 | + positionLeft: 0, |
| 68 | + positionRight: 0, |
| 69 | + positionBottom: 0 |
| 70 | + }) |
| 71 | + |
| 72 | + const next = UiCanvasInformation.getMutable(engine.RootEntity) |
| 73 | + next.interactableArea = { top: 10, left: 480, right: 20, bottom: 30 } |
| 74 | + await engine.update(1) |
| 75 | + |
| 76 | + expect(UiTransform.get(rootDivEntity)).toMatchObject({ |
| 77 | + positionTop: 10, |
| 78 | + positionLeft: 480, |
| 79 | + positionRight: 20, |
| 80 | + positionBottom: 30 |
| 81 | + }) |
| 82 | + |
| 83 | + uiRenderer.destroy() |
| 84 | + }) |
| 85 | + |
| 86 | + it('falls back to zero insets when UiCanvasInformation is not yet available', async () => { |
| 87 | + const { engine, uiRenderer } = setupEngine() |
| 88 | + const UiTransform = components.UiTransform(engine) |
| 89 | + const entityIndex = engine.addEntity() as number |
| 90 | + const rootDivEntity = (entityIndex + 1) as Entity |
| 91 | + |
| 92 | + uiRenderer.setUiRenderer(() => <InteractableArea />) |
| 93 | + await engine.update(1) |
| 94 | + |
| 95 | + expect(UiTransform.get(rootDivEntity)).toMatchObject({ |
| 96 | + positionType: YGPositionType.YGPT_ABSOLUTE, |
| 97 | + positionTop: 0, |
| 98 | + positionLeft: 0, |
| 99 | + positionRight: 0, |
| 100 | + positionBottom: 0 |
| 101 | + }) |
| 102 | + |
| 103 | + uiRenderer.destroy() |
| 104 | + }) |
| 105 | + |
| 106 | + it('forwards user uiTransform props but ignores positionType / position overrides', async () => { |
| 107 | + const { engine, uiRenderer } = setupEngine() |
| 108 | + const UiTransform = components.UiTransform(engine) |
| 109 | + const UiCanvasInformation = components.UiCanvasInformation(engine) |
| 110 | + const entityIndex = engine.addEntity() as number |
| 111 | + const rootDivEntity = (entityIndex + 1) as Entity |
| 112 | + |
| 113 | + UiCanvasInformation.create(engine.RootEntity, { |
| 114 | + devicePixelRatio: 1, |
| 115 | + width: 1920, |
| 116 | + height: 1080, |
| 117 | + screenInsetArea: undefined, |
| 118 | + interactableArea: { top: 10, left: 10, right: 10, bottom: 10 } |
| 119 | + }) |
| 120 | + |
| 121 | + uiRenderer.setUiRenderer(() => ( |
| 122 | + <InteractableArea |
| 123 | + uiTransform={ |
| 124 | + { |
| 125 | + // user-provided overrides for position* should be ignored by typing, |
| 126 | + // but we cast through `any` to assert runtime behaviour as well. |
| 127 | + positionType: 'relative', |
| 128 | + position: { top: 999, left: 999, right: 999, bottom: 999 }, |
| 129 | + padding: 4 |
| 130 | + } as any |
| 131 | + } |
| 132 | + /> |
| 133 | + )) |
| 134 | + await engine.update(1) |
| 135 | + |
| 136 | + expect(UiTransform.get(rootDivEntity)).toMatchObject({ |
| 137 | + positionType: YGPositionType.YGPT_ABSOLUTE, |
| 138 | + positionTop: 10, |
| 139 | + positionLeft: 10, |
| 140 | + positionRight: 10, |
| 141 | + positionBottom: 10 |
| 142 | + }) |
| 143 | + |
| 144 | + uiRenderer.destroy() |
| 145 | + }) |
| 146 | +}) |
0 commit comments