Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brendan-defi committed Dec 13, 2024
1 parent 975abad commit 514515d
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 36 deletions.
34 changes: 0 additions & 34 deletions src/core-react/internal/hooks/useAddressTokenHoldings.ts

This file was deleted.

97 changes: 97 additions & 0 deletions src/internal/components/QrCode/useDotsPath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { describe, it, expect } from 'vitest';
import { useDotsPath } from './useDotsPath';
import { renderHook } from '@testing-library/react';

describe('useDotsPath', () => {
const defaultProps = {
matrix: [
[1, 0, 1],
[0, 1, 0],
[1, 0, 1],
],
size: 300,
logoSize: 60,
logoMargin: 5,
logoBorderRadius: 0,
hasLogo: false,
};

it('should generate path for simple matrix without logo', () => {
const { result } = renderHook(() => useDotsPath(defaultProps));

expect(result.current).toContain('M');
expect(result.current).toContain('A');
});

it('should skip logo area when hasLogo is true', () => {
const withLogo = {
...defaultProps,
hasLogo: true,
matrix: [
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
],
};

const { result: withLogoResult } = renderHook(() => useDotsPath(withLogo));
const { result: withoutLogoResult } = renderHook(() =>
useDotsPath({ ...withLogo, hasLogo: false }),
);

expect(withLogoResult.current.length).toBeLessThan(
withoutLogoResult.current.length,
);
});

it('should handle round logos', () => {
const withRoundLogo = {
...defaultProps,
size: 300,
hasLogo: true,
logoBorderRadius: 40,
logoSize: 60,
logoMargin: 5,
// Using a larger matrix that's more typical for QR codes
matrix: Array(25).fill(Array(25).fill(1)),
};

const { result: roundLogoResult } = renderHook(() =>
useDotsPath(withRoundLogo),
);
const { result: squareLogoResult } = renderHook(() =>
useDotsPath({ ...withRoundLogo, logoBorderRadius: 0 }),
);

expect(roundLogoResult.current).not.toBe(squareLogoResult.current);
// expect(roundLogoResult.current).not.toBe('');
// expect(squareLogoResult.current).not.toBe('');
});

it('should skip masked cells in corners', () => {
const largeMatrix = Array(21).fill(Array(21).fill(1));
const { result } = renderHook(() =>
useDotsPath({
...defaultProps,
matrix: largeMatrix,
}),
);

const points = result.current.split('M').filter(Boolean);

expect(points.length).toBeLessThan(21 * 21);
});

it('should handle empty matrix', () => {
const { result } = renderHook(() =>
useDotsPath({
...defaultProps,
matrix: [],
}),
);

expect(result.current).toBe('');
});
});
86 changes: 86 additions & 0 deletions src/internal/components/QrCode/useLogo.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { describe, it, expect } from 'vitest';
import { renderHook } from '@testing-library/react';
import { useLogo } from './useLogo';

describe('useLogo', () => {
const defaultProps = {
size: 300,
logo: undefined,
logoSize: 60,
logoBackgroundColor: 'white',
logoMargin: 5,
logoBorderRadius: 0,
};

it('should render default logo when no logo provided', () => {
const { result } = renderHook(() => useLogo(defaultProps));

expect(result.current.props.transform).toContain('translate');
expect(result.current.type).toBe('g');

const imageElement = result.current.props.children[2].props.children;
expect(imageElement.props.href).toContain('data:image/svg+xml');
expect(imageElement.props.href).toContain(encodeURIComponent('<'));
});

it('should handle custom React SVG element', () => {
const customLogo = (
<svg>
<circle cx="50" cy="50" r="40" />
</svg>
);
const { result } = renderHook(() =>
useLogo({ ...defaultProps, logo: customLogo }),
);

const imageElement = result.current.props.children[2].props.children;
expect(imageElement.props.href).toContain('data:image/svg+xml');
expect(imageElement.props.href).toContain(encodeURIComponent('circle'));
});

it('should apply correct positioning and sizing', () => {
const { result } = renderHook(() => useLogo(defaultProps));

const expectedPosition = (300 - 60 - 5 * 2) / 2;
expect(result.current.props.transform).toBe(
`translate(${expectedPosition}, ${expectedPosition})`,
);

const backgroundRect = result.current.props.children[1].props.children;
expect(backgroundRect.props.width).toBe(70);
expect(backgroundRect.props.height).toBe(70);
});

it('should apply border radius correctly', () => {
const borderRadius = 10;
const { result } = renderHook(() =>
useLogo({ ...defaultProps, logoBorderRadius: borderRadius }),
);

const clipPathRect =
result.current.props.children[0].props.children.props.children;
expect(clipPathRect.props.rx).toBe(borderRadius);
expect(clipPathRect.props.ry).toBe(borderRadius);

const backgroundRect = result.current.props.children[1].props.children;
expect(backgroundRect.props.rx).toBe(borderRadius);
expect(backgroundRect.props.ry).toBe(borderRadius);
});

it('should apply correct background color', () => {
const backgroundColor = '#ff0000';
const { result } = renderHook(() =>
useLogo({ ...defaultProps, logoBackgroundColor: backgroundColor }),
);

const backgroundRect = result.current.props.children[1].props.children;
expect(backgroundRect.props.fill).toBe(backgroundColor);
});

it('should preserve aspect ratio in image', () => {
const { result } = renderHook(() => useLogo(defaultProps));

const imageElement = result.current.props.children[2].props.children;
expect(imageElement.props.preserveAspectRatio).toBe('xMidYMid slice');
});
});
14 changes: 14 additions & 0 deletions src/internal/utils/getAddressTokenBalances.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, it, expect } from 'vitest';
import { getAddressTokenBalances } from './getAddressTokenBalances';

describe('getAddressTokenBalances', () => {
it('should return an empty array for an invalid address', async () => {
const result = await getAddressTokenBalances('invalid-address');
expect(result).toEqual([]);
});

it('should return an empty array for a null address', async () => {
const result = await getAddressTokenBalances(null);
expect(result).toEqual([]);
});
});
4 changes: 2 additions & 2 deletions src/internal/utils/getAddressTokenBalances.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type { TokenBalanceWithFiatValue } from '../../wallet/components/island/W
export async function getAddressTokenBalances(
address: `0x${string}`,
): Promise<TokenBalanceWithFiatValue[]> {
if (!address || typeof address !== 'string') {
throw new Error('Invalid address');
if (!address || address.slice(0, 2) !== '0x' || address.length !== 42) {
return [];
}

const tokenBalances: TokenBalanceWithFiatValue[] = [
Expand Down

0 comments on commit 514515d

Please sign in to comment.