diff --git a/src/app/article/article-default-view-component.spec.ts b/src/app/article/article-default-view-component.spec.ts index 0d0299d..7ffae0e 100644 --- a/src/app/article/article-default-view-component.spec.ts +++ b/src/app/article/article-default-view-component.spec.ts @@ -1,7 +1,6 @@ import {Input, provide, Component} from 'ng-forward'; import {ArticleViewComponent, ArticleDefaultViewComponent} from './article-default-view.component'; import {ComponentTestHelper, createClass} from './../../spec/component-test-helper'; -import {ModelEvent, ArticleEventType} from "./../shared/models/events"; import * as helpers from "../../spec/helpers"; @@ -85,8 +84,7 @@ describe("Components", () => { * Simulate the ArticleService ArticleEvent.removed event */ function simulateRemovedEvent() { - let event: ModelEvent = ModelEvent.event(ArticleEventType.removed); - helper.component.articleService.notifyArticleRemovedListeners(article); + helper.component.articleService["notifyArticleRemovedListeners"](article); } }); diff --git a/src/app/article/article-default-view.component.ts b/src/app/article/article-default-view.component.ts index 52f1298..2f3a684 100644 --- a/src/app/article/article-default-view.component.ts +++ b/src/app/article/article-default-view.component.ts @@ -5,7 +5,6 @@ import {MacroDirective} from "./macro/macro.directive"; import {ArticleToolbarHotspotComponent} from "../hotspot/article-toolbar-hotspot.component"; import {ArticleContentHotspotComponent} from "../hotspot/article-content-hotspot.component"; import {ArticleService} from "./../../lib/ng-noosfero-api/http/article.service"; -import {ModelEvent, ArticleEventType} from "./../shared/models/events"; /** * @ngdoc controller @@ -26,8 +25,7 @@ export class ArticleDefaultViewComponent { constructor(private $state: ng.ui.IStateService, public articleService: ArticleService) { // Subscribe to the Article Removed Event - let event = ModelEvent.event(ArticleEventType.removed); - this.articleService.subscribe(event, (article: noosfero.Article) => { + this.articleService.subscribeToArticleRemoved((article: noosfero.Article) => { if (this.article.parent) { this.$state.transitionTo('main.profile.page', { page: this.article.parent.path, profile: this.article.profile.identifier }); } else { diff --git a/src/app/shared/models/events.spec.ts b/src/app/shared/models/events.spec.ts deleted file mode 100644 index 8918f21..0000000 --- a/src/app/shared/models/events.spec.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { EventEmitter } from "ng-forward"; -import {ModelEvent, ArticleEventType} from "./events"; -import {HashMap} from "./../utils/hashmap"; -import {ArrayUtils} from "./../utils/arrays"; - -describe("Events", () => { - - describe("Event Type Tests", () => { - - it("verify event type is correctly created", (done) => { - let eventType1 = ArticleEventType.removed; - expect(eventType1.type).toBe("removed"); - expect(ArrayUtils.arraysEqual(eventType1.types, ["added", "removed"])).toBeTruthy(); - done(); - }); - - it("different event types of same type should be equal", (done) => { - let eventType1 = ArticleEventType.removed; - let eventType2 = ArticleEventType.removed; - expect(eventType1).toBe(eventType2); - expect(eventType1 === eventType2).toBeTruthy(); - done(); - }); - - it("different events types of different types should not be equal", (done) => { - let eventType1 = ArticleEventType.removed; - let eventType2 = ArticleEventType.added; - expect(eventType1).not.toBe(eventType2); - expect(eventType1 === eventType2).not.toBeTruthy(); - done(); - }); - - it("different events of same type should be equal", (done) => { - let event1 = ModelEvent.event(ArticleEventType.added); - let event2 = ModelEvent.event(ArticleEventType.added); - expect(event1.equals(event2)).toBeTruthy(); - done(); - }); - - }); - - describe("Event HashMap Tests", () => { - let events: HashMap>; - beforeEach((done) => { - events = new HashMap>(); - done(); - }); - - it("verify event HashMap contains the correct event", () => { - let expected = new EventEmitter(); - events.put(ModelEvent.event(ArticleEventType.added), expected); - let actual = events.get(ModelEvent.event(ArticleEventType.added)); - expect(expected === actual).toBeTruthy(); - }); - - it("verify event HashMap does not contain the wrong event", () => { - events.put(ModelEvent.event(ArticleEventType.added), new EventEmitter()); - let actual = events.get(ModelEvent.event(ArticleEventType.removed)); - expect(actual).not.toBeTruthy(); - }); - - it("verify HashMap has been cleared", () => { - events.put(ModelEvent.event(ArticleEventType.added), new EventEmitter()); - events.clear(); - let actual = events.get(ModelEvent.event(ArticleEventType.added)); - expect(actual).not.toBeTruthy(); - }); - - }); - -}); diff --git a/src/app/shared/models/events.ts b/src/app/shared/models/events.ts deleted file mode 100644 index 9b96ddc..0000000 --- a/src/app/shared/models/events.ts +++ /dev/null @@ -1,62 +0,0 @@ -import {ArrayUtils} from "./../utils/arrays"; - -export abstract class EventType { - /** - * All possible types for the event - */ - types = new Array(); - - /** - * The event type - */ - type: string; - - constructor(types: Array, type: string) { - this.types = types; - this.type = type; - } - - equals(other: EventType) { - return ArrayUtils.arraysEqual(this.types, other.types) && this.type === other.type; - } - -} - -export class ArticleEventType extends EventType { - static types = ["added", "removed"]; - static removed: ArticleEventType = new ArticleEventType("removed"); - static added: ArticleEventType = new ArticleEventType("added"); - - constructor(type: string) { - super(ArticleEventType.types, type); - } -} - -/** - * A model event have a type (ModelEventType), and optionally with a model element. - * Therefore, it is possible to have events related to specific model elements. - * If the model element element is not provided, then the event is a generic event - * of the given type. - */ -export class ModelEvent { - private type: EventType; - private id: number; - - static event(type: EventType, model?: noosfero.RestModel): ModelEvent { - if (model) { - return new ModelEvent(type, model.id); - } else { - return new ModelEvent(type); - } - } - - constructor(type: EventType, id?: number) { - this.type = type; - this.id = id; - } - - equals(other: ModelEvent) { - return other.type.equals(this.type) && other.id === this.id; - } - -} diff --git a/src/app/shared/models/index.ts b/src/app/shared/models/index.ts index 14616d4..181d29e 100644 --- a/src/app/shared/models/index.ts +++ b/src/app/shared/models/index.ts @@ -1,3 +1,2 @@ /* Module Index Entry - generated using the script npm run generate-index */ export * from "./interfaces"; -export * from "./events"; diff --git a/src/lib/ng-noosfero-api/http/article.service.spec.ts b/src/lib/ng-noosfero-api/http/article.service.spec.ts index fd993ec..7c142c2 100644 --- a/src/lib/ng-noosfero-api/http/article.service.spec.ts +++ b/src/lib/ng-noosfero-api/http/article.service.spec.ts @@ -25,6 +25,7 @@ describe("Services", () => { $httpBackend.expectDELETE(`/api/v1/articles/${articleId}`).respond(200, { success: "true" }); articleService.removeArticle({id: articleId}); $httpBackend.flush(); + $httpBackend.verifyNoOutstandingExpectation(); done(); }); diff --git a/src/lib/ng-noosfero-api/http/article.service.ts b/src/lib/ng-noosfero-api/http/article.service.ts index ba95636..b9c38f4 100644 --- a/src/lib/ng-noosfero-api/http/article.service.ts +++ b/src/lib/ng-noosfero-api/http/article.service.ts @@ -2,21 +2,15 @@ import { Injectable, Inject, EventEmitter } from "ng-forward"; import {RestangularService} from "./restangular_service"; import {ProfileService} from "./profile.service"; import {NoosferoRootScope} from "./../../../app/shared/models/interfaces"; -import {ModelEvent, ArticleEventType} from "./../../../app/shared/models/events"; -import {HashMap} from "./../../../app/shared/utils/hashmap"; @Injectable() @Inject("Restangular", "$q", "$log", ProfileService) export class ArticleService extends RestangularService { - private events: HashMap> = new HashMap>(); - - // This event is not tyed to any specific model element. - private removed: ModelEvent = ModelEvent.event(ArticleEventType.removed); + private articleRemoved: EventEmitter = new EventEmitter(); constructor(Restangular: restangular.IService, $q: ng.IQService, $log: ng.ILogService, protected profileService: ProfileService) { super(Restangular, $q, $log); - this.events.put(this.removed, new EventEmitter()); } getResourcePath() { @@ -42,22 +36,17 @@ export class ArticleService extends RestangularService { /** * Notify listeners that this article has been removed */ - notifyArticleRemovedListeners(article: noosfero.Article) { - let listener = this.events.get(this.removed); - listener.next(article); + private notifyArticleRemovedListeners(article: noosfero.Article) { + // let listener = this.events.get(this.removed); + // listener.next(article); + this.articleRemoved.next(article); } /** - * Subscribe a listener a given article event - */ - subscribe(eventToSubscribe: ModelEvent, fn: Function) { - // Find the requested event in map - let event: EventEmitter = this.events.get(eventToSubscribe); - if (event) { - event.subscribe(fn); - } else { - throw new Error(`The event: ${eventToSubscribe} not exists`); - } + * subscribes to the ArticleRemoved event emitter + */ + subscribeToArticleRemoved(fn: Function) { + this.articleRemoved.subscribe(fn); } updateArticle(article: noosfero.Article) { diff --git a/src/spec/mocks.ts b/src/spec/mocks.ts index 4cf74b1..0098fdc 100644 --- a/src/spec/mocks.ts +++ b/src/spec/mocks.ts @@ -76,17 +76,20 @@ export var mocks: any = { isAuthenticated: () => { } }, articleService: { - events: [ + articleRemovedFn: null, + subscribeToArticleRemoved: (fn: Function) => { + mocks.articleService.articleRemovedFn = fn; + }, + articleRemoved: { - event: Function, subscribe: (fn: Function) => { - mocks.articleService.events[0].event = fn; + mocks.articleService.articleRemovedFn = fn; }, next: (param: any) => { - mocks.articleService.events[0].event(param); + mocks.articleService.articleRemovedFn(param); } } - ], + , removeArticle: (article: noosfero.Article) => { return { catch: (func?: Function) => { @@ -94,10 +97,10 @@ export var mocks: any = { }; }, notifyArticleRemovedListeners: (article: noosfero.Article) => { - mocks.articleService.events[0].next(article); + mocks.articleService.articleRemoved.next(article); }, subscribe: (eventType: any, fn: Function) => { - mocks.articleService.events[0].subscribe(fn); + mocks.articleService.articleRemoved.subscribe(fn); }, getByProfile: (profileId: number, params?: any) => { return { -- libgit2 0.21.2