-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #915 from ambrosus/AMB-4978
tests: implement additional tests for utils
- Loading branch information
Showing
11 changed files
with
160 additions
and
6 deletions.
There are no files selected for viewing
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,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); | ||
}); | ||
}); |
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,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 | ||
}); | ||
}); | ||
}); |
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,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 | ||
}); | ||
}); |
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,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); | ||
}); | ||
}); |
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,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(); | ||
}); | ||
}); |
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,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.
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,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); | ||
}); | ||
}); | ||
}); |
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.
Empty file.