File size: 2,035 Bytes
de6cac5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { describe, it, expect } from 'vitest'
import { validateAmount, validateIban, isValidSaudiIban, validateMerchant } from './validate'

describe('validateAmount', () => {
  it('accepts a valid 2-decimal amount', () => expect(validateAmount('150.50')).toBeUndefined())
  it('accepts an integer amount', () => expect(validateAmount('200')).toBeUndefined())
  it('rejects empty', () => expect(validateAmount('')).toBe('error.amountEmpty'))
  it('rejects zero', () => expect(validateAmount('0')).toBe('error.amountZero'))
  it('rejects negative', () => expect(validateAmount('-5')).toBe('error.amountInvalid'))
  it('rejects non-numeric', () => expect(validateAmount('abc')).toBe('error.amountInvalid'))
  it('rejects >2 decimals', () => expect(validateAmount('1.234')).toBe('error.amountInvalid'))
  it('rejects too large', () => expect(validateAmount('2000000')).toBe('error.amountTooLarge'))
  it('rejects lone dot', () => expect(validateAmount('.')).toBe('error.amountInvalid'))
})

describe('Saudi IBAN', () => {
  const VALID = 'SA0380000000608010167519'
  it('accepts a valid Saudi IBAN', () => expect(isValidSaudiIban(VALID)).toBe(true))
  it('accepts with spaces', () => expect(isValidSaudiIban('SA03 8000 0000 6080 1016 7519')).toBe(true))
  it('rejects wrong checksum', () => expect(isValidSaudiIban('SA0380000000608010167518')).toBe(false))
  it('rejects wrong country', () => expect(isValidSaudiIban('GB0380000000608010167519')).toBe(false))
  it('rejects short', () => expect(isValidSaudiIban('SA038000')).toBe(false))
  it('validateIban maps invalid -> key', () => expect(validateIban('SA00')).toBe('error.ibanInvalid'))
  it('validateIban maps empty -> key', () => expect(validateIban('')).toBe('error.payeeEmpty'))
  it('validateIban passes valid', () => expect(validateIban(VALID)).toBeUndefined())
})

describe('validateMerchant', () => {
  it('rejects empty', () => expect(validateMerchant('   ')).toBe('error.merchantEmpty'))
  it('accepts text', () => expect(validateMerchant('Blue Bottle')).toBeUndefined())
})