2023-01-12 03:50:31 +00:00
|
|
|
// Copyright (C) 2023 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2023-09-28 04:43:38 +01:00
|
|
|
import { describe, beforeEach, it, expect, vi } from 'vitest';
|
|
|
|
|
|
|
|
import { decimalShift, formatPrice, hexToBase64 } from '@/utils/strings';
|
2023-01-12 03:50:31 +00:00
|
|
|
|
|
|
|
describe('decimalShift', (): void => {
|
2023-03-17 03:27:27 +00:00
|
|
|
it('handles empty strings', (): void => {
|
|
|
|
expect(decimalShift('', 0)).toBe('0');
|
|
|
|
expect(decimalShift('', 2)).toBe('0');
|
|
|
|
expect(decimalShift('', -2)).toBe('0');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shifts integers correctly', (): void => {
|
2023-01-12 03:50:31 +00:00
|
|
|
['', '-'].forEach(sign => {
|
|
|
|
const decimal = sign+'123';
|
|
|
|
expect(decimalShift(decimal, 0)).toBe(sign+'123');
|
|
|
|
expect(decimalShift(decimal, 2)).toBe(sign+'1.23');
|
|
|
|
expect(decimalShift(decimal, 5)).toBe(sign+'0.00123');
|
|
|
|
expect(decimalShift(decimal, -2)).toBe(sign+'12300');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-17 03:27:27 +00:00
|
|
|
it('shifts decimals correctly', (): void => {
|
2023-01-12 03:50:31 +00:00
|
|
|
['', '-'].forEach(sign => {
|
|
|
|
const decimal = sign+'1.23';
|
|
|
|
expect(decimalShift(decimal, 0)).toBe(sign+'1.23');
|
|
|
|
expect(decimalShift(decimal, -2)).toBe(sign+'123');
|
|
|
|
expect(decimalShift(decimal, 3)).toBe(sign+'0.00123');
|
|
|
|
expect(decimalShift(decimal, -4)).toBe(sign+'12300');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-17 03:27:27 +00:00
|
|
|
it('trims unnecessary characters', (): void => {
|
2023-01-12 03:50:31 +00:00
|
|
|
['', '-'].forEach(sign => {
|
|
|
|
expect(decimalShift(sign+'0.0012300', -2)).toBe(sign+'0.123');
|
|
|
|
expect(decimalShift(sign+'12300', 2)).toBe(sign+'123');
|
|
|
|
expect(decimalShift(sign+'000.000', 1)).toBe('0');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-03-17 03:27:27 +00:00
|
|
|
|
|
|
|
describe('formatPrice', (): void => {
|
|
|
|
it('handles empty strings', (): void => {
|
|
|
|
expect(formatPrice('')).toBe('$0');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('formats correctly', (): void => {
|
|
|
|
['', '-'].forEach(sign => {
|
|
|
|
expect(formatPrice(sign+'123')).toBe(sign+'$123');
|
|
|
|
expect(formatPrice(sign+'1.002')).toBe(sign+'$1.002');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('adds zeros when necessary', (): void => {
|
|
|
|
['', '-'].forEach(sign => {
|
|
|
|
expect(formatPrice(sign+'12.3')).toBe(sign+'$12.30');
|
|
|
|
expect(formatPrice(sign+'.123')).toBe(sign+'$0.123');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('trims unnecessary characters', (): void => {
|
|
|
|
['', '-'].forEach(sign => {
|
|
|
|
expect(formatPrice(sign+'0.0')).toBe('$0');
|
|
|
|
expect(formatPrice(sign+'00123.00')).toBe(sign+'$123');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-09-28 04:43:38 +01:00
|
|
|
|
|
|
|
describe('hexToBase64', () => {
|
|
|
|
it('rejects non-hex strings', () => {
|
|
|
|
expect(() => hexToBase64('foobar')).toThrowError();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects short strings', () => {
|
|
|
|
expect(() => hexToBase64('abc')).toThrowError();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles empty strings', () => {
|
|
|
|
expect(hexToBase64('')).toBe('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('encodes properly', () => {
|
|
|
|
expect(hexToBase64('14fb9c03d97e')).toBe('FPucA9l-');
|
|
|
|
expect(hexToBase64('14fb9c03d9')).toBe('FPucA9k=');
|
|
|
|
expect(hexToBase64('14fb9c03')).toBe('FPucAw==');
|
|
|
|
});
|
|
|
|
});
|