diff --git a/src/core/display/Container.js b/src/core/display/Container.js index 6a3612c..5399755 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -124,11 +124,6 @@ const index1 = this.getChildIndex(child); const index2 = this.getChildIndex(child2); - if (index1 < 0 || index2 < 0) - { - throw new Error('swapChildren: Both the supplied DisplayObjects must be children of the caller.'); - } - this.children[index1] = child2; this.children[index2] = child; this.onChildrenChange(index1 < index2 ? index1 : index2); diff --git a/src/core/display/Container.js b/src/core/display/Container.js index 6a3612c..5399755 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -124,11 +124,6 @@ const index1 = this.getChildIndex(child); const index2 = this.getChildIndex(child2); - if (index1 < 0 || index2 < 0) - { - throw new Error('swapChildren: Both the supplied DisplayObjects must be children of the caller.'); - } - this.children[index1] = child2; this.children[index2] = child; this.onChildrenChange(index1 < index2 ? index1 : index2); diff --git a/test/core/Container.js b/test/core/Container.js index 9211158..737c7e9 100644 --- a/test/core/Container.js +++ b/test/core/Container.js @@ -1,13 +1,13 @@ 'use strict'; -describe('PIXI.Container', function () +describe('PIXI.Container', () => { - describe('parent', function () + describe('parent', () => { - it('should be present when adding children to Container', function () + it('should be present when adding children to Container', () => { - var container = new PIXI.Container(); - var child = new PIXI.DisplayObject(); + const container = new PIXI.Container(); + const child = new PIXI.DisplayObject(); expect(container.children.length).to.be.equals(0); container.addChild(child); @@ -16,22 +16,22 @@ }); }); - describe('events', function () + describe('events', () => { - it('should trigger "added" and "removed" events on its children', function () + it('should trigger "added" and "removed" events on its children', () => { - var container = new PIXI.Container(); - var child = new PIXI.DisplayObject(); - var triggeredAdded = false; - var triggeredRemoved = false; + const container = new PIXI.Container(); + const child = new PIXI.DisplayObject(); + let triggeredAdded = false; + let triggeredRemoved = false; - child.on('added', function (to) + child.on('added', (to) => { triggeredAdded = true; expect(container.children.length).to.be.equals(1); expect(child.parent).to.be.equals(to); }); - child.on('removed', function (from) + child.on('removed', (from) => { triggeredRemoved = true; expect(container.children.length).to.be.equals(0); @@ -48,35 +48,59 @@ }); }); - describe('addChild', function () + describe('addChild', () => { - it('should remove from current parent', function () + it('should remove from current parent', () => { - var parent = new PIXI.Container(); - var container = new PIXI.Container(); - var child = new PIXI.DisplayObject(); + const parent = new PIXI.Container(); + const container = new PIXI.Container(); + const child = new PIXI.DisplayObject(); - assertRemovedFromParent(parent, container, child, function () { container.addChild(child); }); + assertRemovedFromParent(parent, container, child, () => { container.addChild(child); }); + }); + + it('it should call onChildrenChange', () => + { + const container = new PIXI.Container(); + const child = new PIXI.DisplayObject(); + + assertCallToOnChildrenChanged(container, 0, () => + { + container.addChild(child); + }); }); }); - describe('removeChildAt', function () + describe('removeChildAt', () => { - it('should remove from current parent', function () + it('should remove from current parent', () => { - var parent = new PIXI.Container(); - var child = new PIXI.DisplayObject(); + const parent = new PIXI.Container(); + const child = new PIXI.DisplayObject(); - assertRemovedFromParent(parent, null, child, function () { parent.removeChildAt(0); }); + assertRemovedFromParent(parent, null, child, () => { parent.removeChildAt(0); }); + }); + + it('it should call onChildrenChange', () => + { + const container = new PIXI.Container(); + const child = new PIXI.DisplayObject(); + + container.addChild(child); + + assertCallToOnChildrenChanged(container, 0, () => + { + container.removeChildAt(0); + }); }); }); - describe('addChildAt', function () + describe('addChildAt', () => { - it('should allow placements at start', function () + it('should allow placements at start', () => { - var container = new PIXI.Container(); - var child = new PIXI.DisplayObject(); + const container = new PIXI.Container(); + const child = new PIXI.DisplayObject(); container.addChild(new PIXI.DisplayObject()); container.addChildAt(child, 0); @@ -85,10 +109,10 @@ expect(container.children[0]).to.be.equals(child); }); - it('should allow placements at end', function () + it('should allow placements at end', () => { - var container = new PIXI.Container(); - var child = new PIXI.DisplayObject(); + const container = new PIXI.Container(); + const child = new PIXI.DisplayObject(); container.addChild(new PIXI.DisplayObject()); container.addChildAt(child, 1); @@ -97,33 +121,46 @@ expect(container.children[1]).to.be.equals(child); }); - it('should throw on out-of-bounds', function () + it('should throw on out-of-bounds', () => { - var container = new PIXI.Container(); - var child = new PIXI.DisplayObject(); + const container = new PIXI.Container(); + const child = new PIXI.DisplayObject(); container.addChild(new PIXI.DisplayObject()); - expect(function () { container.addChildAt(child, -1); }).to.throw('The index -1 supplied is out of bounds 1'); - expect(function () { container.addChildAt(child, 2); }).to.throw('The index 2 supplied is out of bounds 1'); + expect(() => container.addChildAt(child, -1)).to.throw('The index -1 supplied is out of bounds 1'); + expect(() => container.addChildAt(child, 2)).to.throw('The index 2 supplied is out of bounds 1'); }); - it('should remove from current parent', function () + it('should remove from current parent', () => { - var parent = new PIXI.Container(); - var container = new PIXI.Container(); - var child = new PIXI.DisplayObject(); + const parent = new PIXI.Container(); + const container = new PIXI.Container(); + const child = new PIXI.DisplayObject(); - assertRemovedFromParent(parent, container, child, function () { container.addChildAt(child, 0); }); + assertRemovedFromParent(parent, container, child, () => { container.addChildAt(child, 0); }); + }); + + it('it should call onChildrenChange', () => + { + const container = new PIXI.Container(); + const child = new PIXI.DisplayObject(); + + container.addChild(new PIXI.DisplayObject()); + + assertCallToOnChildrenChanged(container, 0, () => + { + container.addChildAt(child, 0); + }); }); }); - describe('removeChild', function () + describe('removeChild', () => { - it('should ignore non-children', function () + it('should ignore non-children', () => { - var container = new PIXI.Container(); - var child = new PIXI.DisplayObject(); + const container = new PIXI.Container(); + const child = new PIXI.DisplayObject(); container.addChild(child); @@ -132,11 +169,11 @@ expect(container.children.length).to.be.equals(1); }); - it('should remove all children supplied', function () + it('should remove all children supplied', () => { - var container = new PIXI.Container(); - var child1 = new PIXI.DisplayObject(); - var child2 = new PIXI.DisplayObject(); + const container = new PIXI.Container(); + const child1 = new PIXI.DisplayObject(); + const child2 = new PIXI.DisplayObject(); container.addChild(child1, child2); @@ -146,41 +183,190 @@ expect(container.children.length).to.be.equals(0); }); + + it('it should call onChildrenChange', () => + { + const container = new PIXI.Container(); + const child = new PIXI.DisplayObject(); + + container.addChild(child); + + assertCallToOnChildrenChanged(container, 0, () => + { + container.removeChild(child); + }); + }); }); - describe('getChildIndex', function () + describe('getChildIndex', () => { - it('should return the correct index', function () + it('should return the correct index', () => { - var container = new PIXI.Container(); - var child = new PIXI.DisplayObject(); + const container = new PIXI.Container(); + const child = new PIXI.DisplayObject(); container.addChild(new PIXI.DisplayObject(), child, new PIXI.DisplayObject()); expect(container.getChildIndex(child)).to.be.equals(1); }); - it('should throw when child does not exist', function () + it('should throw when child does not exist', () => { - var container = new PIXI.Container(); - var child = new PIXI.DisplayObject(); + const container = new PIXI.Container(); + const child = new PIXI.DisplayObject(); - expect(function () { container.getChildIndex(child); }) + expect(() => container.getChildIndex(child)) .to.throw('The supplied DisplayObject must be a child of the caller'); }); }); - describe('getChildAt', function () + describe('getChildAt', () => { - it('should throw when out-of-bounds', function () + it('should throw when out-of-bounds', () => { - var container = new PIXI.Container(); + const container = new PIXI.Container(); - expect(function () { container.getChildAt(-1); }).to.throw('getChildAt: Index (-1) does not exist.'); - expect(function () { container.getChildAt(1); }).to.throw('getChildAt: Index (1) does not exist.'); + expect(() => container.getChildAt(-1)).to.throw('getChildAt: Index (-1) does not exist.'); + expect(() => container.getChildAt(1)).to.throw('getChildAt: Index (1) does not exist.'); }); }); + describe('setChildIndex', () => + { + it('should throw on out-of-bounds', () => + { + const container = new PIXI.Container(); + const child = new PIXI.DisplayObject(); + + container.addChild(child); + + expect(() => container.setChildIndex(child, -1)).to.throw('The supplied index is out of bounds'); + expect(() => container.setChildIndex(child, 1)).to.throw('The supplied index is out of bounds'); + }); + + it('should throw when child does not belong', () => + { + const container = new PIXI.Container(); + const child = new PIXI.DisplayObject(); + + container.addChild(new PIXI.DisplayObject()); + + expect(() => container.setChildIndex(child, 0)) + .to.throw('The supplied DisplayObject must be a child of the caller'); + }); + + it('should set index', () => + { + const container = new PIXI.Container(); + const child = new PIXI.DisplayObject(); + + container.addChild(child, new PIXI.DisplayObject(), new PIXI.DisplayObject()); + expect(container.children.indexOf(child)).to.be.equals(0); + + container.setChildIndex(child, 1); + expect(container.children.indexOf(child)).to.be.equals(1); + + container.setChildIndex(child, 2); + expect(container.children.indexOf(child)).to.be.equals(2); + + container.setChildIndex(child, 0); + expect(container.children.indexOf(child)).to.be.equals(0); + }); + + it('it should call onChildrenChange', () => + { + const container = new PIXI.Container(); + const child = new PIXI.DisplayObject(); + + container.addChild(child, new PIXI.DisplayObject()); + + assertCallToOnChildrenChanged(container, 1, () => + { + container.setChildIndex(child, 1); + }); + }); + }); + + describe('swapChildren', () => + { + it('should call onChildrenChange', () => + { + const container = new PIXI.Container(); + const child1 = new PIXI.DisplayObject(); + const child2 = new PIXI.DisplayObject(); + + container.addChild(child1, child2); + + assertCallToOnChildrenChanged(container, 0, () => + { + container.swapChildren(child1, child2); + }); + }); + + it('should not call onChildrenChange if supplied children are equal', () => + { + const container = new PIXI.Container(); + const child = new PIXI.DisplayObject(); + let triggered = false; + + container.addChild(child, new PIXI.DisplayObject()); + + container.onChildrenChange = () => + { + triggered = true; + }; + + container.swapChildren(child, child); + + expect(triggered).to.be.false; + }); + + it('should throw if children do not belong', () => + { + const container = new PIXI.Container(); + const child = new PIXI.Container(); + + container.addChild(child, new PIXI.DisplayObject()); + + expect(() => container.swapChildren(child, new PIXI.DisplayObject())) + .to.throw('The supplied DisplayObject must be a child of the caller'); + expect(() => container.swapChildren(new PIXI.DisplayObject(), child)) + .to.throw('The supplied DisplayObject must be a child of the caller'); + }); + + it('should result in swapped child positions', () => + { + const container = new PIXI.Container(); + const child1 = new PIXI.DisplayObject(); + const child2 = new PIXI.DisplayObject(); + + container.addChild(child1, child2); + + expect(container.children.indexOf(child1)).to.be.equals(0); + expect(container.children.indexOf(child2)).to.be.equals(1); + + container.swapChildren(child1, child2); + + expect(container.children.indexOf(child2)).to.be.equals(0); + expect(container.children.indexOf(child1)).to.be.equals(1); + }); + }); + + function assertCallToOnChildrenChanged(container, smallestIndex, functionToAssert) + { + let triggered = false; + + container.onChildrenChange = (index) => + { + triggered = true; + expect(index).to.be.equals(smallestIndex); + }; + + functionToAssert(); + + expect(triggered).to.be.true; + } + function assertRemovedFromParent(parent, container, child, functionToAssert) { parent.addChild(child);