Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clone original defaults with Component.mixin #253

Merged
merged 1 commit into from
May 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ define(
var newComponent = define(); //TODO: fix pretty print
var newPrototype = Object.create(Component.prototype);
newPrototype.mixedIn = [].concat(Component.prototype.mixedIn);
newPrototype.defaults = utils.merge(Component.prototype.defaults);
compose.mixin(newPrototype, arguments);
newComponent.prototype = newPrototype;
newComponent.prototype.constructor = newComponent;
Expand Down
65 changes: 50 additions & 15 deletions test/spec/constructor_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ define(['lib/component'], function (defineComponent) {
describe('Component.mixin', function () {

var testString1, testString2, testString3 = "";
var TestComponent, AugmentedComponent;
var TestComponent, AugmentedComponent1, AugmentedComponent2;

function baseMixin() {
this.defaultAttrs({
core: 35
});
this.fn1 = function() {
testString1 += "testString1"; return testString1
};
Expand All @@ -63,7 +66,22 @@ define(['lib/component'], function (defineComponent) {
};
}

function augmentingMixin() {
function augmentingMixin1() {
this.defaultAttrs({
attr1: {thing: 4}
});
this.before('fn1', function() {
testString1 += "augmented "
});
this.fn3 = function() {
testString3 += "testString3"; return testString3
};
}

function augmentingMixin2() {
this.defaultAttrs({
attr1: {thing: 5}
});
this.before('fn1', function() {
testString1 += "augmented "
});
Expand All @@ -85,7 +103,8 @@ define(['lib/component'], function (defineComponent) {

afterEach(function () {
TestComponent.teardownAll();
AugmentedComponent && AugmentedComponent.teardownAll();
AugmentedComponent1 && AugmentedComponent1.teardownAll();
AugmentedComponent2 && AugmentedComponent2.teardownAll();
});

it('is a function', function () {
Expand All @@ -100,9 +119,9 @@ define(['lib/component'], function (defineComponent) {

initData();

AugmentedComponent = TestComponent.mixin(augmentingMixin);
AugmentedComponent1 = TestComponent.mixin(augmentingMixin1);

var instance2 = (new AugmentedComponent).initialize(document.body);
var instance2 = (new AugmentedComponent1).initialize(document.body);
expect(instance1.fn1()).toBe('testString1');
expect(instance1.fn2()).toBe('testString2');
expect(instance1.fn3).not.toBeDefined();
Expand All @@ -115,24 +134,40 @@ define(['lib/component'], function (defineComponent) {
});

it('cannot re-add an original mixin to an augmented component', function () {
AugmentedComponent = TestComponent.mixin(augmentingMixin, baseMixin);
expect(AugmentedComponent.prototype.mixedIn.length).toBe(TestComponent.prototype.mixedIn.length + 1);
expect(AugmentedComponent.prototype.mixedIn.filter(function(e) {return e === baseMixin}).length).toBe(1);
console.log(AugmentedComponent.prototype.mixedIn)
AugmentedComponent1 = TestComponent.mixin(augmentingMixin1, baseMixin);
expect(AugmentedComponent1.prototype.mixedIn.length).toBe(TestComponent.prototype.mixedIn.length + 1);
expect(AugmentedComponent1.prototype.mixedIn.filter(function(e) {return e === baseMixin}).length).toBe(1);
});

it('can be repeatedly called even when augmenting mixins share properties', function () {
AugmentedComponent1 = TestComponent.mixin(augmentingMixin1, baseMixin);
AugmentedComponent2 = TestComponent.mixin(augmentingMixin2, baseMixin);
expect(AugmentedComponent1.prototype.mixedIn.length).toBe(TestComponent.prototype.mixedIn.length + 1);
expect(AugmentedComponent2.prototype.mixedIn.length).toBe(TestComponent.prototype.mixedIn.length + 1);
expect(AugmentedComponent1.prototype.mixedIn.filter(function(e) {return e === baseMixin}).length).toBe(1);
expect(AugmentedComponent2.prototype.mixedIn.filter(function(e) {return e === baseMixin}).length).toBe(1);
expect(typeof AugmentedComponent1.prototype.fn3).toBe("function");
expect(typeof AugmentedComponent2.prototype.fn3).toBe("function");
expect(AugmentedComponent1.prototype.defaults.attr1).toEqual({thing: 4});
expect(AugmentedComponent1.prototype.defaults.core).toEqual(35);
expect(AugmentedComponent2.prototype.defaults.attr1).toEqual({thing: 5});
expect(AugmentedComponent2.prototype.defaults.core).toEqual(35);
expect(AugmentedComponent1.prototype.fn3 === AugmentedComponent2.prototype.fn3).toBe(false);
expect(AugmentedComponent1.prototype.defaults.attr1 === AugmentedComponent2.prototype.defaults.attr1).toBe(false);
});

it('(the AugmentedComponent) can be further augmented', function () {
AugmentedComponent = TestComponent.mixin(augmentingMixin, baseMixin);
AugmentedComponent1 = TestComponent.mixin(augmentingMixin1, baseMixin);
var anotherMixin = function() {};
var MoreAugmentedComponent = AugmentedComponent.mixin(anotherMixin);
expect(AugmentedComponent.prototype.mixedIn.length).toBe(TestComponent.prototype.mixedIn.length + 1);
expect(MoreAugmentedComponent.prototype.mixedIn.length).toBe(AugmentedComponent.prototype.mixedIn.length + 1);
var MoreAugmentedComponent = AugmentedComponent1.mixin(anotherMixin);
expect(AugmentedComponent1.prototype.mixedIn.length).toBe(TestComponent.prototype.mixedIn.length + 1);
expect(MoreAugmentedComponent.prototype.mixedIn.length).toBe(AugmentedComponent1.prototype.mixedIn.length + 1);
expect(MoreAugmentedComponent.prototype.mixedIn.filter(function(e) {return e === baseMixin}).length).toBe(1);
});

it('(the AugmentedComponent) can describe itself', function () {
AugmentedComponent = TestComponent.mixin(augmentingMixin, baseMixin);
expect(AugmentedComponent.toString()).toBe('testComponent, baseMixin, augmentingMixin');
AugmentedComponent1 = TestComponent.mixin(augmentingMixin1, baseMixin);
expect(AugmentedComponent1.toString()).toBe('testComponent, baseMixin, augmentingMixin1');
});

});
Expand Down