|
| 1 | +import { styleStringInArray } from '../../../utils/common-constants' |
| 2 | +import { createBuiltInDependenciesList } from '../../../core/es-modules/package-manager/built-in-dependencies-list' |
| 3 | +import { isRight } from '../../../core/shared/either' |
| 4 | +import * as EP from '../../../core/shared/element-path' |
| 5 | +import { |
| 6 | + getJSXAttributesAtPath, |
| 7 | + jsxSimpleAttributeToValue, |
| 8 | +} from '../../../core/shared/jsx-attribute-utils' |
| 9 | +import { complexDefaultProjectPreParsed } from '../../../sample-projects/sample-project-utils.test-utils' |
| 10 | +import { withUnderlyingTargetFromEditorState } from '../../editor/store/editor-state' |
| 11 | +import { stylePropPathMappingFn } from '../../inspector/common/property-path-hooks' |
| 12 | +import { DefaultStartingFeatureSwitches, renderTestEditorWithModel } from '../ui-jsx.test-utils' |
| 13 | +import { updateEditorStateWithPatches } from './commands' |
| 14 | +import { convertToAbsolute, runConvertToAbsolute } from './convert-to-absolute-command' |
| 15 | +import { isJSXElement } from '../../../core/shared/element-template' |
| 16 | + |
| 17 | +describe('convertToAbsolute', () => { |
| 18 | + it('sets position absolute', async () => { |
| 19 | + const renderResult = await renderTestEditorWithModel( |
| 20 | + complexDefaultProjectPreParsed(), |
| 21 | + 'dont-await-first-dom-report', |
| 22 | + DefaultStartingFeatureSwitches, |
| 23 | + createBuiltInDependenciesList(null), |
| 24 | + ) |
| 25 | + |
| 26 | + const appInstancePath = EP.elementPath([['storyboard-entity', 'scene-1-entity', 'app-entity']]) |
| 27 | + |
| 28 | + const originalEditorState = renderResult.getEditorState().editor |
| 29 | + |
| 30 | + const convertToAbsoluteCommand = convertToAbsolute('always', appInstancePath) |
| 31 | + const result = runConvertToAbsolute( |
| 32 | + renderResult.getEditorState().editor, |
| 33 | + convertToAbsoluteCommand, |
| 34 | + ) |
| 35 | + |
| 36 | + const patchedEditor = updateEditorStateWithPatches( |
| 37 | + renderResult.getEditorState().editor, |
| 38 | + result.editorStatePatches, |
| 39 | + ) |
| 40 | + |
| 41 | + const originalPositionProp = withUnderlyingTargetFromEditorState( |
| 42 | + appInstancePath, |
| 43 | + originalEditorState, |
| 44 | + null, |
| 45 | + (success, element, underlyingTarget, underlyingFilePath) => { |
| 46 | + if (isJSXElement(element)) { |
| 47 | + const jsxAttributeResult = getJSXAttributesAtPath( |
| 48 | + element.props, |
| 49 | + stylePropPathMappingFn('position', styleStringInArray), |
| 50 | + ) |
| 51 | + const currentValue = jsxSimpleAttributeToValue(jsxAttributeResult.attribute) |
| 52 | + if (currentValue !== null && isRight(currentValue)) { |
| 53 | + return currentValue.value |
| 54 | + } else { |
| 55 | + return null |
| 56 | + } |
| 57 | + } else { |
| 58 | + return null |
| 59 | + } |
| 60 | + }, |
| 61 | + ) |
| 62 | + |
| 63 | + const updatedPositionProp = withUnderlyingTargetFromEditorState( |
| 64 | + appInstancePath, |
| 65 | + patchedEditor, |
| 66 | + null, |
| 67 | + (success, element, underlyingTarget, underlyingFilePath) => { |
| 68 | + if (isJSXElement(element)) { |
| 69 | + const jsxAttributeResult = getJSXAttributesAtPath( |
| 70 | + element.props, |
| 71 | + stylePropPathMappingFn('position', styleStringInArray), |
| 72 | + ) |
| 73 | + const currentValue = jsxSimpleAttributeToValue(jsxAttributeResult.attribute) |
| 74 | + if (currentValue !== null && isRight(currentValue)) { |
| 75 | + return currentValue.value |
| 76 | + } else { |
| 77 | + return null |
| 78 | + } |
| 79 | + } else { |
| 80 | + return null |
| 81 | + } |
| 82 | + }, |
| 83 | + ) |
| 84 | + |
| 85 | + expect(originalPositionProp).toEqual(undefined) |
| 86 | + expect(updatedPositionProp).toEqual('absolute') |
| 87 | + }) |
| 88 | +}) |
0 commit comments