import {expect} from 'chai'; import Property from '../src/property'; describe('Property', () => { it("dies if meta isn't an object", () => { expect(() => { Property('property', 420)(class {}); }).to.throw(); }); it('dies if duplicate properties are defined', () => { expect(() => { Property('property')(class {property() {}}); }).to.throw(); }); it('accepts initializer', () => { let hasInitialized = false; class Class extends Property('property', { initialize() { hasInitialized = this instanceof Class; }, })(class {}) {}; const object = new Class(); expect(hasInitialized).to.be.true; }); it('accepts defaults', () => { class Class extends Property('property', { default: 420, })(class {}) {}; const object = new Class(); expect(object.property).to.equal(420); }); it('accepts getter', () => { class Class extends Property('property', { default: 69, get() { return 420; }, })(class {}) {}; const object = new Class(); expect(object.property).to.equal(420); }); it('accepts setter', () => { let s; class Class extends Property('property', { set(value) { s = value; }, })(class {}) {}; const object = new Class(); object.property = 420; expect(s).to.equal(420); }); it('tracks property values', () => { let emitted = false; class Class extends Property('property', { track: true, })(class {}) { emit(type, o, v) {emitted = ('propertyChanged' === type && 420 === v)} }; const object = new Class(); expect(emitted).to.be.false; object.property = 420; expect(emitted).to.be.true; }); it('accepts emitter', () => { let emitted = false; class Class extends Property('property', { emit: (type, o, v) => { emitted = ('propertyChanged' === type && 420 === v); }, track: true, })(class {}) {}; const object = new Class(); expect(emitted).to.be.false; object.property = 420; expect(emitted).to.be.true; }); it('accepts comparator', () => { let emitted = false; const emit = (type, o, v) => { emitted = ('propertyChanged' === type); }; class Class extends Property('property', { default: [0, 0], emit, eq: (l, r) => l[0] === r[0] && l[1] === r[1], track: true, })(class {}) {}; const object = new Class(); expect(emitted).to.be.false; object.property = [0, 0]; expect(emitted).to.be.false; object.property = [1, 0]; expect(emitted).to.be.true; }); });