Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: tests #1030

Merged
merged 1 commit into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/identity/components/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Children, useMemo } from 'react';
import { defaultAvatarSVG } from '../../internal/svg/defaultAvatarSVG';
import { defaultLoadingSVG } from '../../internal/svg/defaultLoadingSVG';
import { findComponent } from '../../internal/utils/findComponent';
import { cn } from '../../styles/theme';
import { useAvatar } from '../hooks/useAvatar';
import { useName } from '../hooks/useName';
Expand Down Expand Up @@ -46,8 +47,7 @@ export function Avatar({
);

const badge = useMemo(() => {
// @ts-ignore
return Children.toArray(children).find(({ type }) => type === Badge);
return Children.toArray(children).find(findComponent(Badge));
}, [children]);

const defaultAvatar = defaultComponent || defaultAvatarSVG;
Expand Down
5 changes: 2 additions & 3 deletions src/identity/components/Name.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Children, useMemo } from 'react';
import { findComponent } from '../../internal/utils/findComponent';
import { cn, text } from '../../styles/theme';
import { useName } from '../hooks/useName';
import type { NameReact } from '../types';
Expand Down Expand Up @@ -33,9 +34,7 @@ export function Name({
});

const badge = useMemo(() => {
// @ts-ignore
// istanbul ignore next
return Children.toArray(children).find(({ type }) => type === Badge);
return Children.toArray(children).find(findComponent(Badge));
}, [children]);

if (isLoading) {
Expand Down
31 changes: 12 additions & 19 deletions src/internal/utils/findComponent.test.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,35 @@
import type { ReactNode } from 'react';
import { describe, expect, it } from 'vitest';
/**
* @vitest-environment jsdom
*/
import { Avatar } from '../../identity/components/Avatar';
import { Name } from '../../identity/components/Name';
import { findComponent } from './findComponent';

const ToThe = () => <div>Name Component</div>;
const Moon = () => <div>Age Component</div>;

describe('findComponent', () => {
it('should find the Name component in the array', () => {
const childrenArray: ReactNode[] = [
<div key="1">Random div</div>,
<ToThe key="2" />,
<Moon key="3" />,
<Name key="2" address="0x123456789" />,
<Avatar key="3" address="0x123456789" />,
];

const foundNameComponent = childrenArray.find(findComponent(ToThe));
const foundNameComponent = childrenArray.find(findComponent(Name));
expect(foundNameComponent).toBeDefined();
expect(foundNameComponent?.type).toBe(ToThe);
expect(foundNameComponent?.type).toBe(Name);
});

it('should find the Age component in the array', () => {
it('should find the Avatar component in the array', () => {
const childrenArray: ReactNode[] = [
<div key="1">Random div</div>,
<ToThe key="2" />,
<Moon key="3" />,
<Name key="2" address="0x123456789" />,
<Avatar key="3" address="0x123456789" />,
];

const foundAgeComponent = childrenArray.find(findComponent(Moon));
const foundAgeComponent = childrenArray.find(findComponent(Avatar));
expect(foundAgeComponent).toBeDefined();
expect(foundAgeComponent?.type).toBe(Moon);
expect(foundAgeComponent?.type).toBe(Avatar);
});

it('should return undefined if the component is not in the array', () => {
const childrenArray: ReactNode[] = [<div key="1">Random div</div>];

const foundNameComponent = childrenArray.find(findComponent(ToThe));
const foundNameComponent = childrenArray.find(findComponent(Name));
expect(foundNameComponent).toBeUndefined();
});
});
16 changes: 5 additions & 11 deletions src/swap/components/Swap.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Children, useMemo } from 'react';
import { findComponent } from '../../internal/utils/findComponent';
import { background, cn, text } from '../../styles/theme';
import type { SwapReact } from '../types';
import { SwapAmountInput } from './SwapAmountInput';
Expand All @@ -7,7 +8,6 @@ import { SwapMessage } from './SwapMessage';
import { SwapProvider } from './SwapProvider';
import { SwapToggleButton } from './SwapToggleButton';

// istanbul ignore next
export function Swap({
address,
experimental = { useAggregator: true },
Expand All @@ -18,16 +18,10 @@ export function Swap({
const { inputs, toggleButton, swapButton, swapMessage } = useMemo(() => {
const childrenArray = Children.toArray(children);
return {
// @ts-ignore
inputs: childrenArray.filter(({ type }) => type === SwapAmountInput),
// @ts-ignore
toggleButton: childrenArray.find(({ type }) => type === SwapToggleButton),
swapButton: childrenArray.find(
// @ts-ignore
({ type }) => type.name === SwapButton.name,
),
// @ts-ignore
swapMessage: childrenArray.find(({ type }) => type === SwapMessage),
inputs: childrenArray.filter(findComponent(SwapAmountInput)),
toggleButton: childrenArray.find(findComponent(SwapToggleButton)),
swapButton: childrenArray.find(findComponent(SwapButton)),
swapMessage: childrenArray.find(findComponent(SwapMessage)),
};
}, [children]);

Expand Down
7 changes: 3 additions & 4 deletions src/wallet/components/Wallet.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Children, useEffect, useMemo, useRef } from 'react';
import { findComponent } from '../../internal/utils/findComponent';
import type { WalletReact } from '../types';
import { ConnectWallet } from './ConnectWallet';
import { WalletDropdown } from './WalletDropdown';
Expand All @@ -11,10 +12,8 @@ const WalletContent = ({ children }: WalletReact) => {
const { connect, dropdown } = useMemo(() => {
const childrenArray = Children.toArray(children);
return {
// @ts-ignore
connect: childrenArray.filter(({ type }) => type === ConnectWallet),
// @ts-ignore
dropdown: childrenArray.filter(({ type }) => type === WalletDropdown),
connect: childrenArray.find(findComponent(ConnectWallet)),
dropdown: childrenArray.find(findComponent(WalletDropdown)),
};
}, [children]);

Expand Down
8 changes: 4 additions & 4 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export default defineConfig({
],
reportOnFailure: true,
thresholds: {
statements: 99.24,
branches: 98.02,
functions: 93.33,
lines: 99.24,
statements: 99.28,
branches: 98.27,
functions: 94.47,
lines: 99.28,
},
},
environment: 'jsdom',
Expand Down