Skip to content

Commit

Permalink
Merge pull request #915 from ambrosus/AMB-4978
Browse files Browse the repository at this point in the history
tests: implement additional tests for utils
  • Loading branch information
ArturHoncharuk authored Dec 19, 2024
2 parents 3ef7493 + e9a905d commit 268f05e
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 6 deletions.
20 changes: 20 additions & 0 deletions src/utils/__tests__/clipboard.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as ExpoClipboard from 'expo-clipboard';
import { Clipboard } from '@utils/clipboard';

jest.mock('expo-clipboard');

describe('Clipboard Utility', () => {
it('should copy string to clipboard', async () => {
const testString = 'Hello, World!';
await Clipboard.copyToClipboard(testString);
expect(ExpoClipboard.setStringAsync).toHaveBeenCalledWith(testString);
});

it('should get string from clipboard', async () => {
const mockString = 'Hello, World!';
(ExpoClipboard.getStringAsync as jest.Mock).mockResolvedValue(mockString);

const result = await Clipboard.getClipboardString();
expect(result).toBe(mockString);
});
});
34 changes: 34 additions & 0 deletions src/utils/__tests__/css-shadow-to-native.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { parseCSSShadow, cssShadowToNative } from '@utils/css-shadow-to-native';

describe('parseCSSShadow', () => {
it('should parse a valid CSS shadow string', () => {
const shadow = '10px 10px 5px 0px rgba(0, 0, 0, 0.5)';
const result = parseCSSShadow(shadow);
expect(result).toEqual({
offsetX: 10,
offsetY: 10,
blurRadius: 5,
spreadRadius: 0,
color: 'rgba(0, 0, 0, 0.5)'
});
});

it('should throw an error for an invalid shadow string', () => {
const shadow = 'invalid shadow string';
expect(() => parseCSSShadow(shadow)).toThrow('Invalid shadow format');
});
});

describe('cssShadowToNative', () => {
it('should convert CSS shadow to native style', () => {
const shadow = '10px 10px 5px 0px rgba(0, 0, 0, 0.5)';
const result = cssShadowToNative(shadow);
expect(result).toEqual({
shadowColor: 'rgba(0, 0, 0, 0.5)',
shadowOffset: { width: 0, height: 0 },
shadowOpacity: 1,
shadowRadius: 5,
elevation: 5
});
});
});
17 changes: 17 additions & 0 deletions src/utils/__tests__/currency.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { CurrencyUtils } from '@utils/currency';

describe('CurrencyUtils', () => {
test('toCrypto should convert USD to crypto correctly', () => {
const usd = 100;
const rate = 2;
const result = CurrencyUtils.toCrypto(usd, rate);
expect(result).toBe(50); // 100 / 2 = 50
});

test('toUSD should convert crypto to USD correctly', () => {
const crypto = 50;
const rate = 2;
const result = CurrencyUtils.toUSD(crypto, rate);
expect(result).toBe(100); // 50 * 2 = 100
});
});
19 changes: 19 additions & 0 deletions src/utils/__tests__/device-specification.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Dimensions } from 'react-native';
import { isSmallScreen } from '@utils/deviceSpecification';

jest.mock('react-native', () => ({
Dimensions: {
get: jest.fn().mockReturnValue({ width: 375, height: 667 })
}
}));

describe('isSmallScreen', () => {
const setDimensions = (width: number, height: number) => {
(Dimensions.get as jest.Mock).mockReturnValue({ width, height });
};

it('should return true for small screen height', () => {
setDimensions(375, 667);
expect(isSmallScreen).toBe(true);
});
});
18 changes: 18 additions & 0 deletions src/utils/__tests__/object.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getObjectKeyByValue } from '@utils/object';

describe('getObjectKeyByValue', () => {
it('should return the correct key for a given value', () => {
const obj = { a: 'apple', b: 'banana', c: 'cherry' };
expect(getObjectKeyByValue(obj, 'banana')).toBe('b');
});

it('should return undefined for a value that does not exist', () => {
const obj = { a: 'apple', b: 'banana', c: 'cherry' };
expect(getObjectKeyByValue(obj, 'orange')).toBeUndefined();
});

it('should return undefined for an empty object', () => {
const obj = {};
expect(getObjectKeyByValue(obj, 'anyValue')).toBeUndefined();
});
});
27 changes: 27 additions & 0 deletions src/utils/__tests__/sort.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { sortListByKey } from '@utils/sort';

describe('sortListByKey', () => {
it('should sort an array of objects in ascending order by the specified key', () => {
const input = [{ id: 2 }, { id: 1 }, { id: 3 }];
const expected = [{ id: 1 }, { id: 2 }, { id: 3 }];
expect(sortListByKey(input, 'id', 'asc')).toEqual(expected);
});

it('should sort an array of objects in descending order by the specified key', () => {
const input = [{ id: 2 }, { id: 1 }, { id: 3 }];
const expected = [{ id: 3 }, { id: 2 }, { id: 1 }];
expect(sortListByKey(input, 'id', 'desc')).toEqual(expected);
});

it('should handle an empty array', () => {
const input: any[] = [];
const expected: any[] = [];
expect(sortListByKey(input, 'id', 'asc')).toEqual(expected);
});

it('should handle an array with a single element', () => {
const input = [{ id: 1 }];
const expected = [{ id: 1 }];
expect(sortListByKey(input, 'id', 'asc')).toEqual(expected);
});
});
File renamed without changes.
24 changes: 24 additions & 0 deletions src/utils/__tests__/validators.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { StringValidators } from '@utils/validators';

describe('StringValidators', () => {
describe('isStringAddress', () => {
it('should return true for valid Ethereum addresses', () => {
expect(
StringValidators.isStringAddress(
'0x32Be343B94f860124dC4fEe278FDCBD38C102D88'
)
).toBe(true);
expect(
StringValidators.isStringAddress(
'0x5B38Da6a701c568545dCfcB03FEa146bD2C9C1D9'
)
).toBe(true);
});

it('should return false for invalid Ethereum addresses', () => {
expect(StringValidators.isStringAddress('invalidAddress')).toBe(false);
expect(StringValidators.isStringAddress('0x123')).toBe(false);
expect(StringValidators.isStringAddress('')).toBe(false);
});
});
});
2 changes: 1 addition & 1 deletion src/utils/css-shadow-to-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type CSSShadow = {
color: string;
};

function parseCSSShadow(shadow: string): CSSShadow {
export function parseCSSShadow(shadow: string): CSSShadow {
const match = shadow.match(
/(-?\d+px)\s(-?\d+px)\s(\d+px)\s(\d+px)\srgba?\((\d+),\s*(\d+),\s*(\d+),\s*(\d?.?\d*)\)/
);
Expand Down
5 changes: 0 additions & 5 deletions src/utils/deviceSpecification.tsx

This file was deleted.

Empty file removed src/utils/tokens.ts
Empty file.

0 comments on commit 268f05e

Please sign in to comment.