File size: 1,217 Bytes
83a59ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
import assert from 'node:assert/strict'
import { test } from 'node:test'
import { getErrorMessages } from './errors'

const validationData = {
  messages_per_minute: [
    'The messages_per_minute field value can not be greater than 30',
  ],
}

test('formats fetch validation errors while retaining field keys', () => {
  const errors = getErrorMessages({
    status: 422,
    data: { data: validationData },
    response: new Response(null, { status: 422 }),
  })

  assert.deepEqual(errors.get('messages_per_minute'), [
    'The messages per minute value can not be greater than 30',
  ])
})

test('still formats Axios validation errors', () => {
  const errors = getErrorMessages({
    response: { status: 422, data: { data: validationData } },
  })

  assert.deepEqual(errors.get('messages_per_minute'), [
    'The messages per minute value can not be greater than 30',
  ])
})

test('does not turn non-validation failures into field errors', () => {
  for (const error of [
    null,
    undefined,
    new Error('Network error'),
    { status: 500, data: { data: validationData } },
    { status: 422, data: { message: 'Validation failed' } },
  ]) {
    assert.equal(getErrorMessages(error).size(), 0)
  }
})