Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1415 from framer/fix/remove-transformtemplate
Handle removal of transformTemplate
- Loading branch information
Showing
5 changed files
with
126 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { render } from "../../../jest.setup" | ||
import { motion } from "../../" | ||
import * as React from "react" | ||
import sync from "framesync" | ||
|
||
describe("transformTemplate", () => { | ||
it("applies transformTemplate on initial render", () => { | ||
const { container } = render( | ||
<motion.div | ||
initial={{ x: 10 }} | ||
transformTemplate={({ x }, generated) => | ||
`translateY(${x}) ${generated}` | ||
} | ||
/> | ||
) | ||
expect(container.firstChild).toHaveStyle( | ||
"transform: translateY(10px) translateX(10px) translateZ(0)" | ||
) | ||
}) | ||
|
||
it("applies updated transformTemplate", () => { | ||
const { container, rerender } = render( | ||
<motion.div | ||
initial={{ x: 10 }} | ||
transformTemplate={({ x }, generated) => | ||
`translateY(${x}) ${generated}` | ||
} | ||
/> | ||
) | ||
expect(container.firstChild).toHaveStyle( | ||
"transform: translateY(10px) translateX(10px) translateZ(0)" | ||
) | ||
|
||
rerender( | ||
<motion.div | ||
initial={{ x: 10 }} | ||
transformTemplate={({ x }, generated) => { | ||
const newX = typeof x === "string" ? parseFloat(x) : x | ||
return `translateY(${(newX as number) * 2}px) ${generated}` | ||
}} | ||
/> | ||
) | ||
expect(container.firstChild).toHaveStyle( | ||
"transform: translateY(20px) translateX(10px) translateZ(0)" | ||
) | ||
}) | ||
|
||
it("renders transform with transformTemplate", () => { | ||
const { container } = render( | ||
<motion.div | ||
transformTemplate={(_, generated) => | ||
`translateY(20px) ${generated}` | ||
} | ||
style={{ x: 10 }} | ||
/> | ||
) | ||
expect(container.firstChild).toHaveStyle( | ||
"transform: translateY(20px) translateX(10px) translateZ(0)" | ||
) | ||
}) | ||
|
||
it("renders transformTemplate without any transform", () => { | ||
const { container } = render( | ||
<motion.div transformTemplate={() => `translateY(20px)`} /> | ||
) | ||
expect(container.firstChild).toHaveStyle("transform: translateY(20px)") | ||
}) | ||
|
||
it("removes transformTemplate if prop is removed and transform is changed", async () => { | ||
const { container, rerender } = render( | ||
<motion.div | ||
transformTemplate={() => `translateY(20px)`} | ||
style={{ x: 10 }} | ||
/> | ||
) | ||
expect(container.firstChild).toHaveStyle("transform: translateY(20px)") | ||
rerender(<motion.div style={{ x: 20 }} />) | ||
|
||
await new Promise((resolve) => sync.postRender(resolve)) | ||
|
||
expect(container.firstChild).toHaveStyle( | ||
"transform: translateX(20px) translateZ(0)" | ||
) | ||
}) | ||
|
||
it("removes transformTemplate if prop is removed and transform is not changed", async () => { | ||
const { container, rerender } = render( | ||
<motion.div | ||
transformTemplate={() => `translateY(20px)`} | ||
style={{ x: 10 }} | ||
/> | ||
) | ||
expect(container.firstChild).toHaveStyle("transform: translateY(20px)") | ||
rerender(<motion.div style={{ x: 10 }} />) | ||
|
||
await new Promise((resolve) => sync.postRender(resolve)) | ||
|
||
expect(container.firstChild).toHaveStyle( | ||
"transform: translateX(10px) translateZ(0)" | ||
) | ||
}) | ||
|
||
it("removes transformTemplate if prop is removed", async () => { | ||
const { container, rerender } = render( | ||
<motion.div transformTemplate={() => `translateY(20px)`} /> | ||
) | ||
expect(container.firstChild).toHaveStyle("transform: translateY(20px)") | ||
rerender(<motion.div />) | ||
|
||
await new Promise((resolve) => sync.postRender(resolve)) | ||
|
||
expect(container.firstChild).toHaveStyle("transform: none") | ||
}) | ||
}) |
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