File size: 3,186 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
import chai, { expect } from 'chai'
import chaiAsPromised from 'chai-as-promised'
import sinon from 'sinon'

import BulkDeleteAction from './bulk-delete-action.js'
import { ActionContext, ActionRequest, ActionHandler, BulkActionResponse } 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 { RecordJSON } from '../../../frontend/interfaces/index.js'
import { CurrentAdmin } from '../../../current-admin.interface.js'

chai.use(chaiAsPromised)

describe('BulkDeleteAction', function () {
  let data: ActionContext
  const request = {} as ActionRequest
  let response: any

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

    beforeEach(async function () {
      data = {
        _admin: sinon.createStubInstance(AdminJS),
        translateMessage: sinon.stub<any, string>().returns('translatedMessage'),
        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(
        (BulkDeleteAction.handler as ActionHandler<BulkActionResponse>)(request, response, data),
      ).to.rejectedWith(NotFoundError)
    })

    context('2 records were 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

        data.records = [record]
      })

      it('returns all records for get request', async function () {
        request.method = 'get'

        await expect(
          (BulkDeleteAction.handler as ActionHandler<BulkActionResponse>)(request, response, data),
        ).to.eventually.deep.equal({
          records: [recordJSON],
        })
      })

      it('deletes all records for post request', async function () {
        request.method = 'post'

        await (
          BulkDeleteAction.handler as ActionHandler<BulkActionResponse>
        )(request, response, data)

        expect(data.resource.delete).to.have.been.calledOnce
      })

      it('returns deleted records, notice and redirectUrl for post request', async function () {
        request.method = 'post'

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

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