Skip to content

Commit

Permalink
Add unit tests for the Binding class
Browse files Browse the repository at this point in the history
  • Loading branch information
stratedge committed Dec 1, 2019
1 parent 2de169b commit 0109f59
Show file tree
Hide file tree
Showing 13 changed files with 241 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class Binding implements BindingInterface {
* @return {boolean}
*/
public isInstance (): boolean {
return this.isFactory() === false && this.hasResolver() === false
return this.hasResolver() === false
}

/**
Expand All @@ -104,7 +104,7 @@ export class Binding implements BindingInterface {
* @return {boolean}
*/
public isSingleton (): boolean {
return this.isFactory() === false && this.hasResolver() === true
return this.hasResolver() === true && this.isFactory() === false
}

/**
Expand Down Expand Up @@ -134,7 +134,7 @@ export class Binding implements BindingInterface {
* @param {boolean} hasResolved
* @return {this}
*/
protected setHasResolved (hasResolved: boolean): this {
public setHasResolved (hasResolved: boolean): this {
this._hasResolved = hasResolved
return this
}
Expand All @@ -145,7 +145,7 @@ export class Binding implements BindingInterface {
* @param {any} value
* @return {this}
*/
protected setValue (value: any): this {
public setValue (value: any): this {
this._value = value
return this
}
Expand Down
16 changes: 16 additions & 0 deletions src/interfaces/BindingInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,20 @@ export interface BindingInterface {
* @return {any}
*/
resolve (container: ContainerInterface): any

/**
* Set the value of the hasResolved property.
*
* @param {boolean} hasResolved
* @return {this}
*/
setHasResolved (hasResolved: boolean): this

/**
* Set the value of the value property.
*
* @param {any} value
* @return {this}
*/
setValue (value: any): this
}
23 changes: 23 additions & 0 deletions tests/Binding/constructor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Binding } from '../../src/Binding'

describe('Binding: constructor()', function () {
it('should set default values', function () {
const binding = new Binding()

this.assert.isNull(binding.getValue())
this.assert.isNull(binding.getResolver())
this.assert.isFalse(binding.isFactory())
})

it('should set provided values', function () {
const value = {}
const resolver = () => {}
const isFactory = true

const binding = new Binding(value, resolver, isFactory)

this.assert.equal(binding.getValue(), value)
this.assert.equal(binding.getResolver(), resolver)
this.assert.isTrue(binding.isFactory())
})
})
10 changes: 10 additions & 0 deletions tests/Binding/getResolver.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Binding } from '../../src/Binding'

describe('Binding: getResolver()', function () {
it('should return the resolver', function () {
const resolver = () => {}
const binding = new Binding(null, resolver)

this.assert.equal(binding.getResolver(), resolver)
})
})
10 changes: 10 additions & 0 deletions tests/Binding/getValue.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Binding } from '../../src/Binding'

describe('Binding: getValue()', function () {
it('should return the value', function () {
const value = {}
const binding = new Binding(value)

this.assert.equal(binding.getValue(), value)
})
})
13 changes: 13 additions & 0 deletions tests/Binding/hasResolved.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Binding } from '../../src/Binding'

describe('Binding: hasResolved()', function () {
it('should return the hasResolved value', function () {
const binding = new Binding()

this.assert.isFalse(binding.hasResolved())

binding.setHasResolved(true)

this.assert.isTrue(binding.hasResolved())
})
})
15 changes: 15 additions & 0 deletions tests/Binding/hasResolver.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Binding } from '../../src/Binding'

describe('Binding: hasResolver()', function () {
it('should return false if there is no resolver', function () {
const binding = new Binding()

this.assert.isFalse(binding.hasResolver())
})

it('should return true if there is a resolver', function () {
const binding = new Binding(null, () => {})

this.assert.isTrue(binding.hasResolver())
})
})
15 changes: 15 additions & 0 deletions tests/Binding/isFactory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Binding } from '../../src/Binding'

describe('Binding: isFactory()', function () {
it('should return false if the binding is not a factory', function () {
const binding = new Binding()

this.assert.isFalse(binding.isFactory())
})

it('should return true if the binding is a factory', function () {
const binding = new Binding(null, () => {}, true)

this.assert.isTrue(binding.isFactory())
})
})
15 changes: 15 additions & 0 deletions tests/Binding/isInstance.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Binding } from '../../src/Binding'

describe('Binding: isInstance()', function () {
it('should return false if the binding has a resolver', function () {
const binding = new Binding(null, () => {})

this.assert.isFalse(binding.isInstance())
})

it('should return true if the binding has no resolver', function () {
const binding = new Binding()

this.assert.isTrue(binding.isInstance())
})
})
21 changes: 21 additions & 0 deletions tests/Binding/isSingleton.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Binding } from '../../src/Binding'

describe('Binding: isSingleton()', function () {
it('should return false if the binding has no resolver', function () {
const binding = new Binding(null, null, false)

this.assert.isFalse(binding.isSingleton())
})

it('should return false if the binding is a factory', function () {
const binding = new Binding(null, () => {}, true)

this.assert.isFalse(binding.isSingleton())
})

it('should return true if the binding has a resolved and is not a factory', function () {
const binding = new Binding(null, () => {}, false)

this.assert.isTrue(binding.isSingleton())
})
})
61 changes: 61 additions & 0 deletions tests/Binding/resolve.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Container } from '../../src/Container'
import { Binding } from '../../src/Binding'

describe('Binding: resolve()', function () {
describe('for an instance', function () {
it('should return the value', function () {
const value = {}
const binding = new Binding(value)

const container = new Container()

this.assert.equal(binding.resolve(container), value)
})
})

describe('for a singleton', function () {
it('should return the resolver\'s value', function () {
const value = {}
const resolver = () => value
const binding = new Binding(null, resolver)

const container = new Container()

this.assert.equal(binding.resolve(container), value)
})

it('should return the resolver\'s value for subsequent resolutions', function () {
const value = {}
const resolver = () => value
const binding = new Binding(null, resolver)

const container = new Container()

binding.resolve(container)

this.assert.equal(binding.resolve(container), value)
})
})

describe('for a factory', function () {
it('should return the resolver\'s value', function () {
const resolver = () => Object.create({})
const binding = new Binding(null, resolver, true)

const container = new Container()

this.assert.deepEqual(binding.resolve(container), {})
})

it('should return a new value for subsequent resolutions', function () {
const resolver = () => Object.create({})
const binding = new Binding(null, resolver, true)

const container = new Container()

const value = binding.resolve(container)

this.assert.notEqual(binding.resolve(container), value)
})
})
})
19 changes: 19 additions & 0 deletions tests/Binding/setHasResolved.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Binding } from '../../src/Binding'

describe('Binding: setHasResolved()', function () {
it('should set the hasResolved property value', function () {
const binding = new Binding()

this.assert.isFalse(binding.hasResolved())

binding.setHasResolved(true)

this.assert.isTrue(binding.hasResolved())
})

it('should return the binding', function () {
const binding = new Binding()

this.assert.equal(binding.setHasResolved(true), binding)
})
})
19 changes: 19 additions & 0 deletions tests/Binding/setValue.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Binding } from '../../src/Binding'

describe('Binding: setValue()', function () {
it('should set the value property value', function () {
const binding = new Binding()

this.assert.isNull(binding.getValue())

binding.setValue(true)

this.assert.isTrue(binding.getValue())
})

it('should return the binding', function () {
const binding = new Binding()

this.assert.equal(binding.setValue(true), binding)
})
})

0 comments on commit 0109f59

Please sign in to comment.