75 lines
2.0 KiB
TypeScript
Executable File
75 lines
2.0 KiB
TypeScript
Executable File
import { toCents, formatPrice, isValidEmail } from '../../src/utils';
|
|
|
|
describe('toCents', () => {
|
|
it('converts a dollar string to integer cents', () => {
|
|
expect(toCents('19.99')).toBe(1999);
|
|
});
|
|
|
|
it('converts a plain number to integer cents', () => {
|
|
expect(toCents(5)).toBe(500);
|
|
});
|
|
|
|
it('rounds to the nearest cent', () => {
|
|
expect(toCents('19.999')).toBe(2000);
|
|
});
|
|
|
|
it('throws on non-numeric input', () => {
|
|
expect(() => toCents('not-a-number')).toThrow('invalid price');
|
|
});
|
|
|
|
it('throws on a negative price', () => {
|
|
expect(() => toCents(-5)).toThrow('invalid price');
|
|
});
|
|
});
|
|
|
|
describe('formatPrice', () => {
|
|
it('formats cents as a dollar string', () => {
|
|
expect(formatPrice(1999)).toBe('$19.99');
|
|
});
|
|
|
|
it('pads to two decimal places', () => {
|
|
expect(formatPrice(500)).toBe('$5.00');
|
|
});
|
|
});
|
|
|
|
describe('isValidEmail', () => {
|
|
it('accepts a well-formed email', () => {
|
|
expect(isValidEmail('thom@example.com')).toBe(true);
|
|
});
|
|
|
|
it('rejects a string with no @', () => {
|
|
expect(isValidEmail('not-an-email')).toBe(false);
|
|
});
|
|
|
|
it('rejects an email with no domain', () => {
|
|
expect(isValidEmail('thom@')).toBe(false);
|
|
});
|
|
|
|
it('accepts a multi-label domain', () => {
|
|
expect(isValidEmail('thom@mail.example.co.uk')).toBe(true);
|
|
});
|
|
|
|
it('rejects a domain with no dot', () => {
|
|
expect(isValidEmail('thom@localhost')).toBe(false);
|
|
});
|
|
|
|
it('rejects an empty domain label', () => {
|
|
expect(isValidEmail('thom@example..com')).toBe(false);
|
|
expect(isValidEmail('thom@.com')).toBe(false);
|
|
});
|
|
|
|
it('rejects more than one @', () => {
|
|
expect(isValidEmail('thom@foo@example.com')).toBe(false);
|
|
});
|
|
|
|
it('rejects an address longer than 254 characters', () => {
|
|
expect(isValidEmail(`${'a'.repeat(250)}@example.com`)).toBe(false);
|
|
});
|
|
|
|
it('rejects a long dotless domain without super-linear backtracking', () => {
|
|
const start = Date.now();
|
|
expect(isValidEmail(`thom@${'a'.repeat(60_000)}`)).toBe(false);
|
|
expect(Date.now() - start).toBeLessThan(1000);
|
|
});
|
|
});
|