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

Solve issue related to ContainerRegistry class #17

Merged
merged 38 commits into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
47706c8
add unit tests for ViewManagerExtension
tiagoschenkel Oct 6, 2017
750a3f0
add unit tests for ContainerRegistry class
tiagoschenkel Oct 6, 2017
75692a1
use let instead of const
tiagoschenkel Oct 6, 2017
b570109
turns TreeContainer into a PIXI.Sprite object
tiagoschenkel Oct 6, 2017
a82ba31
improve tests of findParentBinding method
tiagoschenkel Oct 6, 2017
06724ed
add test for addContainer method
tiagoschenkel Oct 6, 2017
779ef13
rewrite addContainer method
tiagoschenkel Oct 6, 2017
a7ad934
add unit test to check same container added twice
tiagoschenkel Oct 6, 2017
6762688
add unit tests for addContainer method
tiagoschenkel Oct 6, 2017
2823e20
implement contains method internally
tiagoschenkel Oct 6, 2017
3611722
add unit tests for addContainer method
tiagoschenkel Oct 6, 2017
d2b467b
add unit tests for addContainer method
tiagoschenkel Oct 6, 2017
48b34e2
fix assert equal statement
tiagoschenkel Oct 6, 2017
7eaf72e
add unit tests for addContainer method
tiagoschenkel Oct 6, 2017
f1c5e1c
add unit tests for addContainer method
tiagoschenkel Oct 6, 2017
b696319
add unit tests for addContainer method
tiagoschenkel Oct 6, 2017
d9bbea3
add unit tests for container registry events
tiagoschenkel Oct 6, 2017
1e994b1
handle ContainerBindingEvent.BINDING_EMPTY event properly
tiagoschenkel Oct 6, 2017
4d05925
add unit test for removal of binding when binding is empty
tiagoschenkel Oct 6, 2017
b7e895c
add unit tests for get bindings
tiagoschenkel Oct 6, 2017
8632cb3
simplifies contains method
tiagoschenkel Oct 6, 2017
d2c57ef
remove unnecessary type casting
tiagoschenkel Oct 7, 2017
c0c59fb
add unit tests for ContainerBinding class
tiagoschenkel Oct 7, 2017
12bd1d8
remove unnecessary import statements
tiagoschenkel Oct 7, 2017
779f684
add unit tests for ContainerBindingEvent class
tiagoschenkel Oct 7, 2017
fc77790
add unit tests for ContainerRegistryEvent class
tiagoschenkel Oct 7, 2017
c7beb9f
add unit tests for ViewManager class
tiagoschenkel Oct 7, 2017
492039f
add unit tests for ViewManagerEvent class
tiagoschenkel Oct 7, 2017
87fefb0
clean container pointer after each test
tiagoschenkel Oct 7, 2017
8fd929c
ensure static properties of events will not change
tiagoschenkel Oct 7, 2017
dde8a0d
use forEach to navigate through arrays
tiagoschenkel Oct 7, 2017
b0071c9
add container to ViewManager test class
tiagoschenkel Oct 7, 2017
a71105c
move contains method to a separate file
tiagoschenkel Oct 7, 2017
8f9ea71
verify if ViewManager throws a error when nested containers are added
tiagoschenkel Oct 7, 2017
4bff6a0
import reflect-metadata with karma to run unit tests
tiagoschenkel Oct 8, 2017
7c47699
import inject and injectable from @robotlegsjs/core
tiagoschenkel Oct 8, 2017
b8caf46
add unit tests for MediatorMapExtension
tiagoschenkel Oct 8, 2017
1d36c9c
add unit test to verify if MediatorMapExtension is unmapped when cont…
tiagoschenkel Oct 8, 2017
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
55 changes: 28 additions & 27 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,40 @@ process.env.NODE_ENV = 'test';

const webpack = require("webpack");
const path = require("path");
const webpackConfig = require('./webpack.config.js')({isTest: true});
const webpackConfig = require('./webpack.config.js')({ isTest: true });

delete webpackConfig.entry;

let frameworks = [
"mocha",
"chai",
"sinon",
"es6-shim"
];

let plugins = [
"karma-webpack",
"karma-sourcemap-writer",
"karma-sourcemap-loader",
"karma-mocha-reporter",
"karma-mocha",
"karma-chai",
"karma-sinon",
"karma-es6-shim",
"karma-remap-istanbul",
"karma-coverage-istanbul-reporter"
];

module.exports = function(config) {

var configuration = {
basePath: "",
frameworks: [
"mocha",
"chai",
"sinon",
"es6-shim"
],
frameworks: frameworks,
files: [
"./test/**/**/**.test.ts",
{
pattern: '**/*.map',
served: true,
included: false,
watched: true
}
{ pattern: "node_modules/reflect-metadata/Reflect.js", include: true },
{ pattern: "node_modules/bluebird/js/browser/bluebird.js", include: true },
{ pattern: "./test/**/**/**.test.ts", include: true },
{ pattern: '**/*.map', served: true, included: false, watched: true }
],
preprocessors: {
"./**/**/**/**.ts": ["sourcemap"],
Expand All @@ -34,18 +46,7 @@ module.exports = function(config) {
webpackMiddleware: {
noInfo: true
},
plugins: [
"karma-webpack",
"karma-sourcemap-writer",
"karma-sourcemap-loader",
"karma-remap-istanbul",
"karma-mocha-reporter",
"karma-mocha",
"karma-chai",
"karma-sinon",
"karma-es6-shim",
"karma-coverage-istanbul-reporter"
],
plugins: plugins,
reporters: (
config.singleRun ?
["dots", "mocha", "coverage-istanbul"] :
Expand All @@ -65,7 +66,7 @@ module.exports = function(config) {
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome']
browsers: []
};

if (process.env.TRAVIS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
// in accordance with the terms of the license agreement accompanying it.
// ------------------------------------------------------------------------------

import { injectable, inject } from "inversify";

import { IConfig } from "@robotlegsjs/core";
import { injectable, inject, IConfig } from "@robotlegsjs/core";

import { IContextView } from "../api/IContextView";
import { IViewManager } from "../../viewManager/api/IViewManager";
Expand Down
10 changes: 7 additions & 3 deletions src/robotlegs/bender/extensions/mediatorMap/impl/Mediator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
// in accordance with the terms of the license agreement accompanying it.
// ------------------------------------------------------------------------------

import { injectable, inject } from "inversify";

import { IEventMap, IEventDispatcher, Event } from "@robotlegsjs/core";
import {
injectable,
inject,
IEventMap,
IEventDispatcher,
Event
} from "@robotlegsjs/core";

import { IMediator } from "../api/IMediator";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// ------------------------------------------------------------------------------

import {
injectable,
inject,
IContext,
ILogger,
ITypeMatcher,
Expand All @@ -23,8 +25,6 @@ import { MediatorViewHandler } from "./MediatorViewHandler";
import { NullMediatorUnmapper } from "./NullMediatorUnmapper";
import { MediatorMapper } from "./MediatorMapper";

import { injectable, inject } from "inversify";

/**
* @private
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class ContainerBinding extends EventDispatcher {
public handleView(view: any, type: FunctionConstructor): void {
let length: number = this._handlers.length;
for (let i: number = 0; i < length; i++) {
let handler: IViewHandler = <IViewHandler>this._handlers[i];
let handler: IViewHandler = this._handlers[i];
handler.handleView(view, type);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// in accordance with the terms of the license agreement accompanying it.
// ------------------------------------------------------------------------------

import { Event } from "@robotlegsjs/core";

/**
* @private
*/
Expand Down Expand Up @@ -33,7 +35,7 @@ export class ContainerBindingEvent extends Event {
/**
* @inheritDoc
*/
public clone(): Event {
public clone(): ContainerBindingEvent {
return new ContainerBindingEvent(this.type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import { EventDispatcher } from "@robotlegsjs/core";

import { contains } from "./contains";

import { ContainerBinding } from "./ContainerBinding";
import { ContainerBindingEvent } from "./ContainerBindingEvent";
import { ContainerRegistryEvent } from "./ContainerRegistryEvent";
Expand Down Expand Up @@ -60,20 +62,18 @@ export class ContainerRegistry extends EventDispatcher {
*/
public addContainer(container: any): ContainerBinding {
let binding = this._bindingByContainer.get(container);
if (binding) return binding;

binding = this.createBinding(container);
this._bindingByContainer.set(container, binding);
if (!binding) {
binding = this.createBinding(container);
this._bindingByContainer.set(container, binding);
}
return binding;
}

/**
* @private
*/
public removeContainer(container: any): ContainerBinding {
const binding: ContainerBinding = this._bindingByContainer.get(
container
);
let binding: ContainerBinding = this._bindingByContainer.get(container);

if (binding) {
this.removeBinding(binding);
Expand All @@ -90,7 +90,7 @@ export class ContainerRegistry extends EventDispatcher {
public findParentBinding(target: any): ContainerBinding {
let parent: any = target.parent;
while (parent) {
const binding: ContainerBinding = this._bindingByContainer.get(
let binding: ContainerBinding = this._bindingByContainer.get(
parent
);
if (binding) {
Expand Down Expand Up @@ -119,7 +119,7 @@ export class ContainerRegistry extends EventDispatcher {
// Add a listener so that we can remove this binding when it has no handlers
binding.addEventListener(
ContainerBindingEvent.BINDING_EMPTY,
this.onBindingEmpty
this.onBindingEmpty.bind(this)
);

// If the new binding doesn't have a parent it is a Root
Expand All @@ -132,11 +132,13 @@ export class ContainerRegistry extends EventDispatcher {
// A. Don't have a parent, OR
// B. Have a parent that is not contained within the new binding
this._bindingByContainer.forEach(childBinding => {
if (container.contains(childBinding.container)) {
if (contains(container, childBinding.container)) {
if (!childBinding.parent) {
this.removeRootBinding(childBinding);
childBinding.parent = binding;
} else if (!container.contains(childBinding.parent.container)) {
} else if (
!contains(container, childBinding.parent.container)
) {
childBinding.parent = binding;
}
}
Expand Down
55 changes: 25 additions & 30 deletions src/robotlegs/bender/extensions/viewManager/impl/ViewManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
// in accordance with the terms of the license agreement accompanying it.
// ------------------------------------------------------------------------------

import { injectable } from "inversify";
import { injectable, EventDispatcher } from "@robotlegsjs/core";

import { EventDispatcher } from "@robotlegsjs/core";
import { contains } from "./contains";

import { IViewHandler } from "../api/IViewHandler";
import { IViewManager } from "../api/IViewManager";
Expand Down Expand Up @@ -73,11 +73,10 @@ export class ViewManager extends EventDispatcher implements IViewManager {
}

this._containers.push(container);

for (let i in this._handlers) {
let handler: IViewHandler = this._handlers[i];
this._handlers.forEach(handler => {
this._registry.addContainer(container).addHandler(handler);
}
});

this.dispatchEvent(
new ViewManagerEvent(ViewManagerEvent.CONTAINER_ADD, container)
);
Expand All @@ -88,17 +87,19 @@ export class ViewManager extends EventDispatcher implements IViewManager {
*/
public removeContainer(container: any): void {
let index: number = this._containers.indexOf(container);

if (index === -1) {
return;
}

this._containers.splice(index, 1);

let binding: ContainerBinding = this._registry.getBinding(container);
for (let i in this._handlers) {
let handler: IViewHandler = this._handlers[i];

this._handlers.forEach(handler => {
binding.removeHandler(handler);
}
});

this.dispatchEvent(
new ViewManagerEvent(ViewManagerEvent.CONTAINER_REMOVE, container)
);
Expand All @@ -113,11 +114,9 @@ export class ViewManager extends EventDispatcher implements IViewManager {
}

this._handlers.push(handler);

for (let i in this._containers) {
let container: any = this._containers[i];
this._containers.forEach(container => {
this._registry.addContainer(container).addHandler(handler);
}
});

this.dispatchEvent(
new ViewManagerEvent(ViewManagerEvent.HANDLER_ADD, null, handler)
Expand All @@ -136,10 +135,9 @@ export class ViewManager extends EventDispatcher implements IViewManager {

this._handlers.splice(index, 1);

for (let i in this._containers) {
let container: any = this._containers[i];
this._containers.forEach(container => {
this._registry.getBinding(container).removeHandler(handler);
}
});

this.dispatchEvent(
new ViewManagerEvent(ViewManagerEvent.HANDLER_REMOVE, null, handler)
Expand All @@ -150,36 +148,33 @@ export class ViewManager extends EventDispatcher implements IViewManager {
* @inheritDoc
*/
public removeAllHandlers(): void {
for (let i in this._containers) {
let container: any = this._containers[i];
var binding: ContainerBinding = this._registry.getBinding(
container
);
for (let j in this._handlers) {
let handler: IViewHandler = this._handlers[j];
let binding: ContainerBinding = null;
this._containers.forEach(container => {
binding = this._registry.getBinding(container);
this._handlers.forEach(handler => {
binding.removeHandler(handler);
}
}
});
});
}

/*============================================================================*/
/* Private Functions */
/*============================================================================*/

private validContainer(container: any): boolean {
for (let i in this._containers) {
let registeredContainer: any = this._containers[i];
this._containers.forEach(registeredContainer => {
if (container === registeredContainer) {
return false;
}

if (
registeredContainer.contains(container) ||
container.contains(registeredContainer)
contains(registeredContainer, container) ||
contains(container, registeredContainer)
) {
throw new Error("Containers can not be nested");
}
}
});

return true;
}
}
35 changes: 35 additions & 0 deletions src/robotlegs/bender/extensions/viewManager/impl/contains.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// ------------------------------------------------------------------------------
// Copyright (c) 2017 RobotlegsJS. All Rights Reserved.
//
// NOTICE: You are permitted to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
// ------------------------------------------------------------------------------

/*============================================================================*/
/* Public Functions */
/*============================================================================*/

/**
* Determines whether the specified child object is a child of the containter instance or the instance itself.
* The search includes the entire display list including the containter instance.
* Grandchildren, great-grandchildren, and so on each return true.
*
* @param container The container.
* @param child The child object to test.
*
* @return true if the child object is a child of the container or the container itself; otherwise false.
*/
export function contains(container: any, child: any): boolean {
let found: boolean = false;
if (container === child || container.children.indexOf(child) >= 0) {
found = true;
} else {
for (let c of container.children) {
if (this.contains(c, child)) {
found = true;
break;
}
}
}
return found;
}
3 changes: 2 additions & 1 deletion test/entry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "reflect-metadata";
/// <reference types="mocha" />

import "bluebird/js/browser/bluebird";
import "es6-symbol/implement";
import "es6-map/implement";
Loading