Commit 8321c71

mo khan <mo@mokhan.ca>
2016-12-02 04:43:02
start to build event aggregator.
1 parent 417d1e9
Changed files (3)
app/infrastructure/__tests__/event_aggregator_spec.js
@@ -0,0 +1,27 @@
+import EventAggregator from '../event-aggregator';
+
+describe("EventAggregator", () => {
+  let subject = null;
+
+  beforeEach(() => {
+    subject = new EventAggregator();
+  });
+
+  describe("publish", () => {
+    class TestSubscriber {
+      notify(event) {
+        this.called = true;
+        this.calledWith = event;
+      }
+    }
+
+    it ("publishes the event to all interested subscribers", () => {
+      let subscriber = new TestSubscriber();
+      subject.subscribe('LOGGED_IN', subscriber);
+      subject.publish({ event: 'LOGGED_IN', username: 'blah' })
+
+      expect(subscriber.called).toBeTruthy();
+      expect(subscriber.calledWith.username).toEqual('blah');
+    });
+  });
+});
app/infrastructure/event-aggregator.js
@@ -0,0 +1,14 @@
+
+export default class EventAggregator {
+  constructor() {
+    this.subscribers = [];
+  }
+
+  subscribe(event, subscriber) {
+    this.subscribers.push(subscriber);
+  }
+
+  publish(event) {
+    this.subscribers.forEach(x => x.notify(event));
+  }
+}
app/reducers/__tests__/stronglifters_spec.js
@@ -1,6 +1,5 @@
 import stronglifters from '../stronglifters';
 
-
 describe('stronglifters', () => {
   let subject = stronglifters;