-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
286 additions
and
210 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
100 changes: 100 additions & 0 deletions
100
src/swap/components/SwapSettingsSlippageLayout.test.tsx
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,100 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import type React from 'react'; | ||
import { describe, expect, it, vi } from 'vitest'; | ||
import { SwapSettingsSlippageDescription } from './SwapSettingsSlippageDescription'; | ||
import { SwapSettingsSlippageInput } from './SwapSettingsSlippageInput'; | ||
import { SwapSettingsSlippageLayout } from './SwapSettingsSlippageLayout'; | ||
import { SwapSettingsSlippageTitle } from './SwapSettingsSlippageTitle'; | ||
|
||
vi.mock('./SwapSettingsSlippageTitle', () => ({ | ||
SwapSettingsSlippageTitle: ({ children }: { children: React.ReactNode }) => ( | ||
<div data-testid="mock-title">{children}</div> | ||
), | ||
})); | ||
|
||
vi.mock('./SwapSettingsSlippageDescription', () => ({ | ||
SwapSettingsSlippageDescription: ({ | ||
children, | ||
}: { children: React.ReactNode }) => ( | ||
<div data-testid="mock-description">{children}</div> | ||
), | ||
})); | ||
|
||
vi.mock('./SwapSettingsSlippageInput', () => ({ | ||
SwapSettingsSlippageInput: () => <div data-testid="mock-input">Input</div>, | ||
})); | ||
|
||
vi.mock('../../styles/theme', () => ({ | ||
cn: (...args: string[]) => args.join(' '), | ||
})); | ||
|
||
describe('SwapSettingsSlippageLayout', () => { | ||
it('renders with all child components', () => { | ||
render( | ||
<SwapSettingsSlippageLayout> | ||
<SwapSettingsSlippageTitle>Title</SwapSettingsSlippageTitle> | ||
<SwapSettingsSlippageDescription> | ||
Description | ||
</SwapSettingsSlippageDescription> | ||
<SwapSettingsSlippageInput /> | ||
</SwapSettingsSlippageLayout>, | ||
); | ||
expect( | ||
screen.getByTestId('ockSwapSettingsLayout_container'), | ||
).toBeInTheDocument(); | ||
expect(screen.getByTestId('mock-title')).toBeInTheDocument(); | ||
expect(screen.getByTestId('mock-description')).toBeInTheDocument(); | ||
expect(screen.getByTestId('mock-input')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders with only some child components', () => { | ||
render( | ||
<SwapSettingsSlippageLayout> | ||
<SwapSettingsSlippageTitle>Title</SwapSettingsSlippageTitle> | ||
</SwapSettingsSlippageLayout>, | ||
); | ||
expect( | ||
screen.getByTestId('ockSwapSettingsLayout_container'), | ||
).toBeInTheDocument(); | ||
expect(screen.getByTestId('mock-title')).toBeInTheDocument(); | ||
expect(screen.queryByTestId('mock-description')).not.toBeInTheDocument(); | ||
expect(screen.queryByTestId('mock-input')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('applies custom className', () => { | ||
render( | ||
<SwapSettingsSlippageLayout className="custom-class"> | ||
<SwapSettingsSlippageTitle>Title</SwapSettingsSlippageTitle> | ||
</SwapSettingsSlippageLayout>, | ||
); | ||
const container = screen.getByTestId('ockSwapSettingsLayout_container'); | ||
expect(container.className).toContain('custom-class'); | ||
}); | ||
|
||
it('renders without any child components', () => { | ||
render(<SwapSettingsSlippageLayout />); | ||
expect( | ||
screen.getByTestId('ockSwapSettingsLayout_container'), | ||
).toBeInTheDocument(); | ||
expect(screen.queryByTestId('mock-title')).not.toBeInTheDocument(); | ||
expect(screen.queryByTestId('mock-description')).not.toBeInTheDocument(); | ||
expect(screen.queryByTestId('mock-toggle')).not.toBeInTheDocument(); | ||
expect(screen.queryByTestId('mock-input')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('renders with correct layout structure', () => { | ||
render( | ||
<SwapSettingsSlippageLayout> | ||
<SwapSettingsSlippageTitle>Title</SwapSettingsSlippageTitle> | ||
<SwapSettingsSlippageDescription> | ||
Description | ||
</SwapSettingsSlippageDescription> | ||
<SwapSettingsSlippageInput /> | ||
</SwapSettingsSlippageLayout>, | ||
); | ||
const container = screen.getByTestId('ockSwapSettingsLayout_container'); | ||
expect(container.children[0]).toHaveTextContent('Title'); | ||
expect(container.children[1]).toHaveTextContent('Description'); | ||
expect(container.children[2].children[0]).toHaveTextContent('Input'); | ||
}); | ||
}); |
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,40 @@ | ||
import { Children, useMemo } from 'react'; | ||
import { findComponent } from '../../internal/utils/findComponent'; | ||
import { cn } from '../../styles/theme'; | ||
import type { SwapSettingsSlippageLayoutReact } from '../types'; | ||
import { SwapSettingsSlippageDescription } from './SwapSettingsSlippageDescription'; | ||
import { SwapSettingsSlippageInput } from './SwapSettingsSlippageInput'; | ||
import { SwapSettingsSlippageTitle } from './SwapSettingsSlippageTitle'; | ||
|
||
export function SwapSettingsSlippageLayout({ | ||
children, | ||
className, | ||
}: SwapSettingsSlippageLayoutReact) { | ||
const { title, description, input } = useMemo(() => { | ||
const childrenArray = Children.toArray(children); | ||
return { | ||
title: childrenArray.find(findComponent(SwapSettingsSlippageTitle)), | ||
description: childrenArray.find( | ||
findComponent(SwapSettingsSlippageDescription), | ||
), | ||
input: childrenArray.find(findComponent(SwapSettingsSlippageInput)), | ||
}; | ||
}, [children]); | ||
|
||
return ( | ||
<div | ||
className={cn( | ||
'right-0 z-10 w-[21.75rem] rounded-lg border border-gray-300 bg-gray-50', | ||
'px-3 py-3 shadow-lg dark:border-gray-700 dark:bg-gray-950', | ||
className, | ||
)} | ||
data-testid="ockSwapSettingsLayout_container" | ||
> | ||
{title} | ||
{description} | ||
<div className="flex items-center justify-between gap-2"> | ||
{input && <div className="flex-grow">{input}</div>} | ||
</div> | ||
</div> | ||
); | ||
} |
Oops, something went wrong.