uci-utils-class-merge/test/test.merge.js

64 lines
1.5 KiB
JavaScript

import merge from '../src/merge.js'
import chai, { expect } from 'chai'
import eventemitter from 'chai-eventemitter2'
import { EventEmitter as Emitter } from 'events'
chai.use(eventemitter())
describe('Class Merge', function () {
class Colored {
initializer () { this._color = 'white' }
get color () { return this._color }
set color (v) { this._color = v }
}
class ZCoord {
initializer () { this._z = 0 }
get z () { return this._z }
set z (v) { this._z = v }
}
class Shape {
constructor (x, y) { this._x = x; this._y = y }
get x () { return this._x }
set x (v) { this._x = v }
get y () { return this._y }
set y (v) { this._y = v }
}
var Rectangle = class Rectangle extends merge(Shape, Colored, ZCoord, Emitter) {}
var rect = new Rectangle(7, 42)
rect.z = 1000
rect.color = 'red'
it('Should merge three classes', function () {
expect(rect.x).to.be.equal(7)
expect(rect.y).to.be.equal(42)
expect(rect.z).to.be.equal(1000)
expect(rect.color).to.be.equal('red')
})
it('Should handle more complex mixin, i.e. with eventEmitter', function () {
expect(rect).to.be.an.eventEmitter
expect(rect)
.to.emit('foo')
.to.emit('bar', {count: 2})
.to.emit('baz', {withArgs: ['X', 'Y', 'Z']})
.to.emit('error', {count: 0})
.on(() =>
{
rect.emit('foo')
rect.emit('bar')
rect.emit('bar')
rect.emit('baz', 'X', 'Y', 'Z')
})
})
})