From f638e3653b362827d4267852d5d8c9db4f53e1cc Mon Sep 17 00:00:00 2001 From: Arshpreet Singh <73437174+GoldGroove06@users.noreply.github.com> Date: Fri, 20 Dec 2024 14:36:02 +0530 Subject: [PATCH] [New Component] Aspect Ratio component (#656) --- src/components/ui/AspectRatio/AspectRatio.tsx | 25 +++++++++++ .../stories/AspectRatio.stories.js | 25 +++++++++++ .../ui/AspectRatio/tests/AspectRatio.test.js | 43 +++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 src/components/ui/AspectRatio/AspectRatio.tsx create mode 100644 src/components/ui/AspectRatio/stories/AspectRatio.stories.js create mode 100644 src/components/ui/AspectRatio/tests/AspectRatio.test.js diff --git a/src/components/ui/AspectRatio/AspectRatio.tsx b/src/components/ui/AspectRatio/AspectRatio.tsx new file mode 100644 index 00000000..95b27a8f --- /dev/null +++ b/src/components/ui/AspectRatio/AspectRatio.tsx @@ -0,0 +1,25 @@ +'use client'; +import React from 'react'; +import { customClassSwitcher } from '~/core'; +import { clsx } from 'clsx'; +const COMPONENT_NAME = 'AspectRatio'; + +export type AspectRatioProps = { + children: React.ReactNode; + customRootClass?: string; + className?: string; + ratio?: string; + props: Record[]; +} + +const AspectRatio = ({ children, customRootClass, className, ratio="1", ...props }: AspectRatioProps) => { + + if (isNaN(Number(ratio)) && !ratio.match(/^(\d+)\/(\d+)$/)) ratio = "1" + if (Number(ratio) <= 0) ratio = "1" + + const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME); + return
{children}
+} +AspectRatio.displayName = COMPONENT_NAME; + +export default AspectRatio; \ No newline at end of file diff --git a/src/components/ui/AspectRatio/stories/AspectRatio.stories.js b/src/components/ui/AspectRatio/stories/AspectRatio.stories.js new file mode 100644 index 00000000..391f501f --- /dev/null +++ b/src/components/ui/AspectRatio/stories/AspectRatio.stories.js @@ -0,0 +1,25 @@ +import AspectRatio from '../AspectRatio'; +import SandboxEditor from '~/components/tools/SandboxEditor/SandboxEditor'; + +// More on how to set up stories at: https://storybook.js.org/docs/react/writing-stories/introduction#default-export +export default { + title: 'Components/AspectRatio', + component: AspectRatio, + render: (args) => + + Landscape photograph + + +}; + +// More on writing stories with args: https://storybook.js.org/docs/react/writing-stories/args +export const Default = { + args: { + className:"", + ratio: "16/9" + } +}; diff --git a/src/components/ui/AspectRatio/tests/AspectRatio.test.js b/src/components/ui/AspectRatio/tests/AspectRatio.test.js new file mode 100644 index 00000000..110bff62 --- /dev/null +++ b/src/components/ui/AspectRatio/tests/AspectRatio.test.js @@ -0,0 +1,43 @@ +import React from 'react'; +import { render, screen } from '@testing-library/react'; +import AspectRatio from '../AspectRatio'; + +describe('AspectRatio', () => { + test('renders AspectRatio component', () => { + render(Content); + expect(screen.getByText('Content')).toBeInTheDocument(); + }); + + test('applies custom classes correctly', () => { + render(Content); + const divElement = screen.getByText('Content'); + expect(divElement).toHaveClass('additional-class'); + }); + + test('applies correct aspect ratio', () => { + render(Content); + expect(screen.getByText('Content').style.aspectRatio).toBe('16/9'); + }); + + test('applies correct aspect ratio when ratio is not provided', () => { + render(Content); + expect(screen.getByText('Content').style.aspectRatio).toBe('1'); + }); + + test('applies correct aspect ratio when ratio is invalid', () => { + render(Content); + expect(screen.getByText('Content').style.aspectRatio).toBe('1'); + }); + + test('applies correct aspect ratio when ratio contains a character', () => { + render(Content); + expect(screen.getByText('Content').style.aspectRatio).toBe('1'); + }); + + test('applies correct aspect ratio when ratio is negative', () => { + render(Content); + expect(screen.getByText('Content').style.aspectRatio).toBe('1'); + }); + + +}); \ No newline at end of file