decoratorSpec.es7 1.42 KB
Newer Older
YazhouChen's avatar
YazhouChen committed
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
'use strict'

import { expect } from 'chai'
import constitute, { Dependencies, Lazy, Constitutor, Transient } from '../'

describe('@Dependencies', function () {
  it('should decorate classes with metadata', function () {
    class B {}
    class C {}

    @Dependencies(B, Lazy.of(C))
    class A {
      constructor (b, lazyC) {
        this.b = b
        this.c = lazyC()
      }
    }

    expect(A.constitute).to.be.instanceOf(Constitutor)

    const a = constitute(A)

    expect(a).to.be.instanceOf(A)
    expect(a.b).to.be.instanceOf(B)
    expect(a.c).to.be.instanceOf(C)
  })

  it('should decorate classes when passed a Constitutor', function () {
    class B {}
    class C {}

    @Dependencies(Transient.with([B, Lazy.of(C)]))
    class A {
      constructor (b, lazyC) {
        this.b = b
        this.c = lazyC()
      }
    }

    expect(A.constitute).to.be.instanceOf(Transient)

    const a = constitute(A)

    expect(a).to.be.instanceOf(A)
    expect(a.b).to.be.instanceOf(B)
    expect(a.c).to.be.instanceOf(C)
  })

  it('should decorate methods with metadata', function () {
    class B {}
    class C {}

    Dependencies(B, Lazy.of(C))(A)
    function A (b, lazyC) {
      return {
        b,
        c: lazyC()
      }
    }

    expect(A.constitute).to.be.instanceOf(Constitutor)

    const a = constitute(A)

    expect(a).to.be.an('object')
    expect(a.b).to.be.instanceOf(B)
    expect(a.c).to.be.instanceOf(C)
  })
})