-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for transform utility operations
- Loading branch information
1 parent
ff48e8d
commit ee6dac6
Showing
7 changed files
with
269 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
255 changes: 255 additions & 0 deletions
255
apps/builder/app/builder/features/style-panel/sections/transforms/transform-utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,255 @@ | ||
import { afterEach, describe, test, expect } from "@jest/globals"; | ||
import { | ||
addDefaultsForTransormSection, | ||
handleDeleteTransformProperty, | ||
handleHideTransformProperty, | ||
isTransformPanelPropertyExists, | ||
updateRotateOrSkewPropertyValue, | ||
updateTransformTuplePropertyValue, | ||
} from "./transform-utils"; | ||
import type { StyleInfo } from "../../shared/style-info"; | ||
import { | ||
FunctionValue, | ||
toValue, | ||
TupleValue, | ||
type StyleProperty, | ||
type StyleValue, | ||
} from "@webstudio-is/css-engine"; | ||
import { | ||
extractRotatePropertiesFromTransform, | ||
parseCssValue, | ||
} from "@webstudio-is/css-data"; | ||
|
||
let currentStyle: StyleInfo = {}; | ||
const setProperty = (property: StyleProperty) => (value: StyleValue) => { | ||
currentStyle[property] = { value }; | ||
}; | ||
const deleteProperty = (property: StyleProperty) => { | ||
delete currentStyle[property]; | ||
}; | ||
|
||
afterEach(() => { | ||
currentStyle = {}; | ||
}); | ||
|
||
describe("Transform utils CRUD operations", () => { | ||
test("adds a default translate property", () => { | ||
addDefaultsForTransormSection({ | ||
panel: "translate", | ||
currentStyle, | ||
setProperty, | ||
}); | ||
const translate = currentStyle["translate"]?.value; | ||
expect(translate).toEqual({ | ||
type: "tuple", | ||
value: [ | ||
{ | ||
type: "unit", | ||
unit: "px", | ||
value: 0, | ||
}, | ||
{ | ||
type: "unit", | ||
unit: "px", | ||
value: 0, | ||
}, | ||
{ | ||
type: "unit", | ||
unit: "px", | ||
value: 0, | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
test("adds a default scale property", () => { | ||
addDefaultsForTransormSection({ | ||
panel: "scale", | ||
currentStyle, | ||
setProperty, | ||
}); | ||
|
||
const scale = currentStyle["scale"]?.value; | ||
expect(scale).toEqual({ | ||
type: "tuple", | ||
value: [ | ||
{ | ||
type: "unit", | ||
unit: "number", | ||
value: 1, | ||
}, | ||
{ | ||
type: "unit", | ||
unit: "number", | ||
value: 1, | ||
}, | ||
{ | ||
type: "unit", | ||
unit: "number", | ||
value: 1, | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
test("adds a default rotate and scale property", () => { | ||
addDefaultsForTransormSection({ | ||
panel: "rotate", | ||
currentStyle, | ||
setProperty, | ||
}); | ||
|
||
expect(toValue(currentStyle["transform"]?.value)).toBe( | ||
"rotateX(0deg) rotateY(0deg) rotateZ(0deg)" | ||
); | ||
|
||
addDefaultsForTransormSection({ | ||
panel: "skew", | ||
currentStyle, | ||
setProperty, | ||
}); | ||
expect(toValue(currentStyle["transform"]?.value)).toBe( | ||
"skewX(0deg) skewY(0deg) rotateX(0deg) rotateY(0deg) rotateZ(0deg)" | ||
); | ||
}); | ||
|
||
test("checks if any of the transform property exist", () => { | ||
expect( | ||
isTransformPanelPropertyExists({ | ||
currentStyle, | ||
panel: "translate", | ||
}) | ||
).toBe(false); | ||
|
||
addDefaultsForTransormSection({ | ||
panel: "rotate", | ||
currentStyle, | ||
setProperty, | ||
}); | ||
|
||
expect( | ||
isTransformPanelPropertyExists({ currentStyle, panel: "rotate" }) | ||
).toBe(true); | ||
}); | ||
|
||
test("deletes rotate property values from the transform", () => { | ||
setProperty("transform")( | ||
parseCssValue( | ||
"transform", | ||
"rotateX(50deg) rotateY(50deg) rotateZ(50deg) scale(1, 1) translate(10px, 10px)" | ||
) | ||
); | ||
|
||
handleDeleteTransformProperty({ | ||
panel: "rotate", | ||
currentStyle, | ||
setProperty, | ||
deleteProperty, | ||
}); | ||
|
||
expect(toValue(currentStyle["transform"]?.value)).toBe( | ||
"scale(1, 1) translate(10px, 10px)" | ||
); | ||
}); | ||
|
||
test("delete skew property values from the transform", () => { | ||
setProperty("transform")( | ||
parseCssValue( | ||
"transform", | ||
"rotateX(50deg) rotateY(50deg) rotateZ(50deg) skew(10deg) skewX(10deg) skewY(10deg)" | ||
) | ||
); | ||
handleDeleteTransformProperty({ | ||
panel: "skew", | ||
currentStyle, | ||
setProperty, | ||
deleteProperty, | ||
}); | ||
expect(toValue(currentStyle["transform"]?.value)).toBe( | ||
"rotateX(50deg) rotateY(50deg) rotateZ(50deg) skew(10deg)" | ||
); | ||
}); | ||
|
||
test("hide translate and rotate properties", () => { | ||
addDefaultsForTransormSection({ | ||
panel: "translate", | ||
currentStyle, | ||
setProperty, | ||
}); | ||
addDefaultsForTransormSection({ | ||
panel: "rotate", | ||
currentStyle, | ||
setProperty, | ||
}); | ||
|
||
handleHideTransformProperty({ | ||
panel: "translate", | ||
currentStyle, | ||
setProperty, | ||
}); | ||
expect(currentStyle["translate"]?.value?.hidden).toBe(true); | ||
|
||
handleHideTransformProperty({ | ||
panel: "rotate", | ||
currentStyle, | ||
setProperty, | ||
}); | ||
const rotate = extractRotatePropertiesFromTransform( | ||
currentStyle["transform"]?.value as TupleValue | ||
); | ||
|
||
expect(rotate.rotateX?.hidden).toBe(true); | ||
expect(rotate.rotateY?.hidden).toBe(true); | ||
expect(rotate.rotateZ?.hidden).toBe(true); | ||
}); | ||
|
||
test("update translate property value", () => { | ||
const translate = parseCssValue("translate", "0px 0px 0px"); | ||
const newValue = updateTransformTuplePropertyValue( | ||
1, | ||
{ type: "unit", value: 50, unit: "px" }, | ||
translate as TupleValue | ||
); | ||
|
||
expect(toValue(newValue)).toBe("0px 50px 0px"); | ||
}); | ||
|
||
test("update rotate values in transform property", () => { | ||
const transform = parseCssValue( | ||
"transform", | ||
"rotateX(10deg) rotateY(10deg) rotateZ(10deg) skewX(10deg) skewY(10deg)" | ||
); | ||
setProperty("transform")(transform); | ||
|
||
const updatedRotateYValue: FunctionValue = { | ||
type: "function", | ||
name: "rotateY", | ||
args: { | ||
type: "layers", | ||
value: [{ type: "unit", value: 50, unit: "deg" }], | ||
}, | ||
}; | ||
const { rotateX, rotateY, rotateZ } = | ||
extractRotatePropertiesFromTransform(transform); | ||
const newValue = updateRotateOrSkewPropertyValue({ | ||
index: 1, | ||
panel: "rotate", | ||
currentStyle, | ||
value: updatedRotateYValue, | ||
propertyValue: { | ||
type: "tuple", | ||
value: [ | ||
rotateX as FunctionValue, | ||
rotateY as FunctionValue, | ||
rotateZ as FunctionValue, | ||
], | ||
}, | ||
}); | ||
|
||
const result = extractRotatePropertiesFromTransform(newValue); | ||
expect(result.rotateY).toEqual(updatedRotateYValue); | ||
expect(toValue(newValue)).toBe( | ||
"rotateX(10deg) rotateY(50deg) rotateZ(10deg) skewX(10deg) skewY(10deg)" | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters