-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
mixin.ts
79 lines (63 loc) · 2.07 KB
/
mixin.ts
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
// Copyright IBM Corp. 2013,2017. All Rights Reserved.
// Node module: @loopback/repository
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {expect} from '@loopback/testlab';
import {Class} from '../../../';
import {MixinBuilder} from '../../../';
// tslint:disable:no-any
class BaseClass {
baseProp: string = 'baseProp';
static staticMethod1(): string {
return 'static';
}
method1() {}
}
function Mixin1<T extends Class<any>>(superClass: T) {
return class extends superClass {
mixinProp1: string = 'mixinProp1';
static staticMixinMethod1(): string {
return 'mixin1.static';
}
mixinMethod1(): string {
return 'mixin1';
}
};
}
function Mixin2<T extends Class<any>>(superClass: T) {
return class extends superClass {
mixinProp2: string = 'mixinProp2';
static staticMixinMethod2(): string {
return 'mixin2.static';
}
mixinMethod2() {
return 'mixin2';
}
};
}
describe('mixin builder', () => {
let newClass: any;
before(() => {
newClass = MixinBuilder.mix(BaseClass).with(Mixin1, Mixin2);
});
it('allows multiple classes to be mixed in', () => {
expect(newClass.staticMethod1).to.eql(BaseClass.staticMethod1);
expect(newClass.prototype.method1).to.eql(BaseClass.prototype.method1);
expect(newClass.staticMixinMethod1).to.not.null();
expect(newClass.staticMixinMethod2).to.not.null();
expect(newClass.staticMethod1()).to.eql('static');
expect(newClass.staticMixinMethod1()).to.eql('mixin1.static');
expect(newClass.staticMixinMethod2()).to.eql('mixin2.static');
const x = new newClass();
expect(typeof x.mixinMethod1).to.eql('function');
expect(typeof x.mixinMethod2).to.eql('function');
expect(x.mixinMethod1()).to.eql('mixin1');
expect(x.mixinMethod2()).to.eql('mixin2');
expect(x.mixinProp1).to.eql('mixinProp1');
expect(x.mixinProp2).to.eql('mixinProp2');
});
it('allows inheritance', () => {
const x = new newClass();
expect(x instanceof BaseClass).to.true();
});
});