avocado/packages/traits/test/trait.js
2021-03-20 08:20:30 -05:00

70 lines
1.3 KiB
JavaScript

import {expect} from 'chai';
import Trait from '../src/trait';
class TestTrait extends Trait {
static defaultParams() {
return {
asd: 'hello',
sdf: 311,
};
}
static defaultState() {
return {
foo: 69,
bar: '420',
};
}
}
// it('optimizes output based on deltas', async () => {
// const testTrait = await TestTrait.load({
// params: {
// asd: 'boo',
// },
// state: {
// bar: 'yee',
// },
// });
// expect(testTrait.toJSON()).to.deep.equal({
// params: {
// asd: 'boo',
// },
// state: {
// bar: 'yee',
// },
// });
// });
it('tracks state differences', () => {
const testTrait = new TestTrait();
testTrait.state.foo = 420;
expect(testTrait.stateDifferences()).to.deep.equal({
foo: {
old: 69,
value: 420,
},
});
testTrait.markAsClean();
expect(testTrait.stateDifferences()).to.deep.equal({});
});
it('can memoize listeners', () => {
class ListenerTrait extends Trait {
listeners() {
return {
heard: () => {},
};
}
}
const testTrait = new ListenerTrait();
const memoized = testTrait.memoizedListeners();
expect(Object.keys(testTrait.listeners())).to.deep.equal(Object.keys(memoized));
expect(testTrait.memoizedListeners()).to.equal(testTrait.memoizedListeners());
});