File size: 4,902 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { expect } from 'chai'

import { ResourcesFactory } from './resources-factory.js'
import { BaseDatabase, BaseResource } from '../../adapters/index.js'

describe('ResourcesFactory', function () {
  describe('._convertDatabases', function () {
    context('no adapter defined', function () {
      it('throws an error when there are no adapters and database is given', function () {
        expect(() => {
          new ResourcesFactory()._convertDatabases(['one'])
        }).to.throw().property('name', 'NoDatabaseAdapterError')
      })

      it('returns empty array when none databases were given', function () {
        expect(new ResourcesFactory()._convertDatabases([])).to.have.lengthOf(0)
      })
    })

    context('one adapter defined', function () {
      beforeEach(function () {
        this.resourcesInDatabase = 5
        class Database extends BaseDatabase {
          static isAdapterFor(database) { return database === 'supported' }

          resources() { return new Array(5) } // eslint-disable-line class-methods-use-this
        }
        class Resource extends BaseResource {}
        this.resourcesFactory = new ResourcesFactory({}, [{ Database, Resource }])
      })

      it('takes resources from databases', function () {
        expect(
          this.resourcesFactory._convertDatabases(['supported']),
        ).to.have.lengthOf(this.resourcesInDatabase)
      })

      it('throws an error when there are no adapters supporting given database', function () {
        expect(() => {
          this.resourcesFactory._convertDatabases(['not supported'])
        }).to.throw().property('name', 'NoDatabaseAdapterError')
      })
    })
  })

  describe('._convertResources', function () {
    context('there are no adapters', function () {
      it('throws an error when resource is not subclass from BaseResource', function () {
        expect(() => {
          new ResourcesFactory({})._convertResources(['one'])
        }).to.throw().property('name', 'NoResourceAdapterError')
      })

      it('returns given resource when it is subclass from BaseResource', function () {
        class MyResource extends BaseResource {}
        expect(new ResourcesFactory({})._convertResources([new MyResource()])).to.have.lengthOf(1)
      })
    })

    context('there is one adapter', function () {
      beforeEach(function () {
        class Database extends BaseDatabase {}
        class Resource extends BaseResource {
          static isAdapterFor(resource) { return resource === 'supported' }
        }
        this.resourcesFactory = new ResourcesFactory({}, [{ Database, Resource }])
        this.Resource = Resource
      })

      it('throws an error when resource is not handled by the adapter', function () {
        expect(() => {
          this.resourcesFactory._convertResources(['not supported'])
        }).to.throw().property('name', 'NoResourceAdapterError')
      })

      it('throws an error when resource is not handled by the adapter and its provided with a decorator', function () {
        expect(() => {
          this.resourcesFactory._convertResources([{ resource: 'not supported', decorator: 'sth' }])
        }).to.throw().property('name', 'NoResourceAdapterError')
      })

      it('converts given resource to Resource class provided in the adapter', function () {
        const resources = this.resourcesFactory._convertResources(['supported'])
        expect(resources).to.have.lengthOf(1)
        expect(resources[0].resource).to.be.an.instanceOf(this.Resource)
      })

      it('converts to Resource class when resource is provided with options', function () {
        const options = {}
        const resources = this.resourcesFactory._convertResources([{ resource: 'supported', options }])
        expect(resources).to.have.lengthOf(1)
        expect(resources[0].resource).to.be.an.instanceOf(this.Resource)
        expect(resources[0].options).to.deep.equal(options)
      })
    })
  })

  describe('_decorateResources', function () {
    beforeEach(function () {
      this.resourcesFactory = new ResourcesFactory({ options: {} }, [])
      this.assignDecoratorStub = this.sinon.stub(BaseResource.prototype, 'assignDecorator')
    })

    it('assigns ResourceDecorator when no options were given', function () {
      this.resourcesFactory._decorateResources([{ resource: new BaseResource() }])
      expect(this.assignDecoratorStub).to.have.been.calledWith(
        this.sinon.match.any,
        this.sinon.match({}),
      )
    })

    it('assigns ResourceDecorator with options when there were given', function () {
      const options = { id: 'someId' }
      const resource = new BaseResource()
      this.resourcesFactory._decorateResources([{ resource, options }])

      expect(this.assignDecoratorStub).to.have.been.calledWith(
        this.sinon.match.any,
        this.sinon.match(options),
      )
    })
  })
})