File size: 4,076 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import chai, { expect } from 'chai'
import chaiAsPromised from 'chai-as-promised'
import sinon from 'sinon'

import DeleteAction from './delete-action.js'
import { ActionContext, ActionRequest, ActionHandler, RecordActionResponse } from '../action.interface.js'
import BaseRecord from '../../adapters/record/base-record.js'
import AdminJS from '../../../adminjs.js'
import ViewHelpers from '../../utils/view-helpers/view-helpers.js'
import BaseResource from '../../adapters/resource/base-resource.js'
import ActionDecorator from '../../decorators/action/action-decorator.js'
import NotFoundError from '../../utils/errors/not-found-error.js'
import { ValidationError } from '../../utils/errors/validation-error.js'
import { RecordJSON } from '../../../frontend/interfaces/index.js'
import { CurrentAdmin } from '../../../current-admin.interface.js'

chai.use(chaiAsPromised)

describe('DeleteAction', function () {
  let data: ActionContext
  const request = {
    params: {},
    method: 'post',
  } as ActionRequest
  let response: any

  describe('.handler', function () {
    afterEach(function () {
      sinon.restore()
    })

    beforeEach(async function () {
      data = {
        _admin: sinon.createStubInstance(AdminJS),
        h: sinon.createStubInstance(ViewHelpers),
        resource: sinon.createStubInstance(BaseResource),
        action: sinon.createStubInstance(ActionDecorator) as unknown as ActionDecorator,
      } as unknown as ActionContext
    })

    it('throws error when no records are given', async function () {
      await expect(
        (DeleteAction.handler as ActionHandler<RecordActionResponse>)(request, response, data),
      ).to.rejectedWith(NotFoundError)
    })

    context('A record has been selected', function () {
      let record: BaseRecord
      let recordJSON: RecordJSON

      beforeEach(function () {
        recordJSON = { id: 'someId' } as RecordJSON
        record = sinon.createStubInstance(BaseRecord, {
          toJSON: sinon.stub<[(CurrentAdmin)?]>().returns(recordJSON),
        }) as unknown as BaseRecord

        request.params.recordId = recordJSON.id
        data.record = record
      })

      it('returns deleted record, notice and redirectUrl', async function () {
        const actionResponse = await (
          DeleteAction.handler as ActionHandler<RecordActionResponse>
        )(request, response, data)

        expect(actionResponse).to.have.property('notice')
        expect(actionResponse).to.have.property('redirectUrl')
        expect(actionResponse).to.have.property('record')
      })

      context('ValidationError is thrown by Resource.delete', function () {
        it('returns error notice', async function () {
          const errorMessage = 'test validation error'
          data.resource = sinon.createStubInstance(BaseResource, {
            delete: sinon.stub().rejects(new ValidationError({}, { message: errorMessage })) as any,
          })

          const actionResponse = await (
            DeleteAction.handler as ActionHandler<RecordActionResponse>
          )(request, response, data)

          expect(actionResponse).to.have.property('notice')
          expect(actionResponse.notice).to.deep.equal({
            message: errorMessage,
            type: 'error',
          })
          expect(actionResponse).to.have.property('record')
        })

        it('returns error notice with default message when ValidationError has no baseError', async function () {
          data.resource = sinon.createStubInstance(BaseResource, {
            delete: sinon.stub().rejects(new ValidationError({})) as any,
          })

          const actionResponse = await (
            DeleteAction.handler as ActionHandler<RecordActionResponse>
          )(request, response, data)

          expect(actionResponse).to.have.property('notice')
          expect(actionResponse.notice).to.deep.equal({
            message: 'thereWereValidationErrors',
            type: 'error',
          })
          expect(actionResponse).to.have.property('record')
        })
      })
    })
  })
})