From fed691d35590d9a0502c5345d91da7a58c416bb6 Mon Sep 17 00:00:00 2001 From: Cornelis Bakker Date: Sat, 9 Jul 2016 21:13:42 +0200 Subject: [PATCH] Upgraded unit tests to QUnit 2.0. Added modules, asserts and descriptions to the tests. --- tests/StronglyTypedEvents.tests.html | 6 +- tests/StronglyTypedEvents.tests.ts | 427 ++++++++++++++------------- 2 files changed, 229 insertions(+), 204 deletions(-) diff --git a/tests/StronglyTypedEvents.tests.html b/tests/StronglyTypedEvents.tests.html index 95d18153..cc09242b 100644 --- a/tests/StronglyTypedEvents.tests.html +++ b/tests/StronglyTypedEvents.tests.html @@ -4,13 +4,13 @@ Strongly Typed Events for TypeScript - +
- + - + \ No newline at end of file diff --git a/tests/StronglyTypedEvents.tests.ts b/tests/StronglyTypedEvents.tests.ts index 89a22a47..fa354676 100644 --- a/tests/StronglyTypedEvents.tests.ts +++ b/tests/StronglyTypedEvents.tests.ts @@ -2,301 +2,326 @@ /// /// -QUnit.test("Testing subscribe / unsubscribe - event as property", (assert) => { +QUnit.module('Event', function () { - class MyEventTester { - private _myEvent: EventDispatcher = new EventDispatcher(); + QUnit.test("Subscribe / unsubscribe - event as property", (assert) => { - get myEvent(): IEvent { - return this._myEvent.asEvent(); - } + assert.expect(2); - signal(str: string): void { - this._myEvent.dispatch(this, str); - } - } + class MyEventTester { + private _myEvent: EventDispatcher = new EventDispatcher(); - var s = new MyEventTester(); - var r = null; + get myEvent(): IEvent { + return this._myEvent.asEvent(); + } - var handler = (sender: MyEventTester, args: string) => { - r = args; - }; + signal(str: string): void { + this._myEvent.dispatch(this, str); + } + } - s.myEvent.subscribe(handler); - s.signal('Test1'); - assert.equal(r, 'Test1'); + let tester = new MyEventTester(); + let eventHandlerResult: string = null; - s.myEvent.unsubscribe(handler); - s.signal('Test2'); - assert.equal(r, 'Test1'); -}); + let handler = (sender: MyEventTester, args: string) => { + eventHandlerResult = args; + }; -QUnit.test("Testing subscribe / unsubscribe - event on interface" , (assert) => { + tester.myEvent.subscribe(handler); + tester.signal('Test1'); + assert.equal(eventHandlerResult, 'Test1', 'The eventHandlerResult should be "Test1".'); - interface IMyEventTester { - myEvent(): IEvent; + tester.myEvent.unsubscribe(handler); + tester.signal('Test2'); + assert.equal(eventHandlerResult, 'Test1', 'The eventHandlerResult should still be "Test1".'); + }); - signal(str: string); - } + QUnit.test("Subscribe / unsubscribe - event on interface", (assert) => { - class MyEventTester implements IMyEventTester { - private _myEvent: EventDispatcher = new EventDispatcher(); + assert.expect(2); - myEvent(): IEvent { - return this._myEvent.asEvent(); - } + interface IMyEventTester { + myEvent(): IEvent; - signal(str: string): void { - this._myEvent.dispatch(this, str); + signal(str: string): void; } - } - var s: IMyEventTester = new MyEventTester(); - var r = null; + class MyEventTester implements IMyEventTester { + private _myEvent: EventDispatcher = new EventDispatcher(); - var handler = (sender: IMyEventTester, args: string) => { - r = args; - }; + myEvent(): IEvent { + return this._myEvent.asEvent(); + } - s.myEvent().subscribe(handler); - s.signal('Test1'); - assert.equal(r, 'Test1'); + signal(str: string): void { + this._myEvent.dispatch(this, str); + } + } - s.myEvent().unsubscribe(handler); - s.signal('Test2'); - assert.equal(r, 'Test1'); -}); + let tester: IMyEventTester = new MyEventTester(); + let eventHandlerResult: string = null; -QUnit.test("Testing event list", (assert) => { + let handler = (sender: IMyEventTester, args: string) => { + eventHandlerResult = args; + }; - var events = new EventList(); - var result: string; + tester.myEvent().subscribe(handler); + tester.signal('Test1'); + assert.equal(eventHandlerResult, 'Test1', 'The eventHandlerResult should be "Test1".'); - events.get('Test1').subscribe((sender, args) => result = args); - events.get('Test1').dispatch(this, 'Testing 123'); - assert.equal(result, 'Testing 123'); + tester.myEvent().unsubscribe(handler); + tester.signal('Test2'); + assert.equal(eventHandlerResult, 'Test1', 'The eventHandlerResult should still be "Test1".'); + }); - events.get('Test2').dispatch(this, 'Testing 456'); - assert.equal(result, 'Testing 123'); + QUnit.test("Event list", (assert) => { - events.get('Test2').subscribe((sender, args) => result = args); - events.get('Test2').dispatch(this, 'Testing 789'); - assert.equal(result, 'Testing 789'); + assert.expect(4); - events.get('Test3').asEvent().subscribe((sender, args) => result = args); - events.get('Test3').dispatch(this, 'Testing 42'); - assert.equal(result, 'Testing 42'); + var events = new EventList(); + var result: string; -}); + events.get('Test1').subscribe((sender: any, args: string) => result = args); + events.get('Test1').dispatch(this, 'Testing 123'); + assert.equal(result, 'Testing 123', 'The result should be "Testing 123".'); -QUnit.test('Testing EventHandlingBase', (assert) => { + events.get('Test2').dispatch(this, 'Testing 456'); + assert.equal(result, 'Testing 123', 'The result should still be "Testing 123".'); - class MyTester extends EventHandlingBase { + events.get('Test2').subscribe((sender: any, args: string) => result = args); + events.get('Test2').dispatch(this, 'Testing 789'); + assert.equal(result, 'Testing 789', 'The result should be "Testing 789".'); - signal(name: string, str: string): void { - this.events.get(name).dispatch(this, str); - } - } + events.get('Test3').asEvent().subscribe((sender: any, args: string) => result = args); + events.get('Test3').dispatch(this, 'Testing 42'); + assert.equal(result, 'Testing 42', 'The result should be "Testing 42".'); + }); - var t = new MyTester(); - var result: string; + QUnit.test('EventHandlingBase', (assert) => { - t.subscribe('Test1', (sender, args) => result = args); - t.signal('Test1', 'Testing 123'); - assert.equal(result, 'Testing 123'); + assert.expect(3); - t.signal('Test2', 'Testing 456'); - assert.equal(result, 'Testing 123'); + class MyTester extends EventHandlingBase { - t.subscribe('Test2', (sender, args) => result = args); - t.signal('Test2', 'Testing 789'); - assert.equal(result, 'Testing 789'); -}); + signal(name: string, str: string): void { + this.events.get(name).dispatch(this, str); + } + } -QUnit.test("Testing subscribe / unsubscribe - simple event as property", (assert) => { + var t = new MyTester(); + var result: string; - class MyEventTester { - private _myEvent: SimpleEventDispatcher = new SimpleEventDispatcher(); + t.subscribe('Test1', (sender: MyTester, args: string) => result = args); + t.signal('Test1', 'Testing 123'); + assert.equal(result, 'Testing 123', 'The result should be "Testing 123".'); - get myEvent(): ISimpleEvent { - return this._myEvent; - } + t.signal('Test2', 'Testing 456'); + assert.equal(result, 'Testing 123', 'The result should still be "Testing 123".'); - signal(str: string): void { - this._myEvent.dispatch(str); - } - } + t.subscribe('Test2', (sender: MyTester, args: string) => result = args); + t.signal('Test2', 'Testing 789'); + assert.equal(result, 'Testing 789', 'The result should be "Testing 789".'); + }); - var s = new MyEventTester(); - var r = null; + QUnit.test('Dispatcher', (assert) => { - var handler = (args: string) => { - r = args; - }; + assert.expect(4); - s.myEvent.subscribe(handler); - s.signal('Test1'); - assert.equal(r, 'Test1'); + class Source { constructor(public name: string) { } } + class Argument { constructor(public name: string) { } } - s.myEvent.unsubscribe(handler); - s.signal('Test2'); - assert.equal(r, 'Test1'); -}); + let dispatcher = new EventDispatcher(); -QUnit.test("Testing subscribe / unsubscribe - simple event on interface", (assert) => { + var s1 = new Source('s1'); + var s2 = new Source('s2'); + var a1 = new Argument('a1'); + var a2 = new Argument('a2'); - interface IMyEventTester { - myEvent(): ISimpleEvent; + dispatcher.subscribe((sender: Source, argument: Argument) => { + assert.deepEqual(sender, s1, "Sender should be s1."); + assert.notDeepEqual(sender, s2, "Sender should not be s2."); - signal(str: string); - } + assert.deepEqual(argument, a1, 'Argument should be a1.'); + assert.notDeepEqual(argument, a2, 'Argument should not be a2.'); + }); - class MyEventTester implements IMyEventTester { - private _myEvent: SimpleEventDispatcher = new SimpleEventDispatcher(); + dispatcher.dispatch(s1, a1); + }); - myEvent(): ISimpleEvent { - return this._myEvent; - } + QUnit.test('Async dispatch', (assert) => { - signal(str: string): void { - this._myEvent.dispatch(str); - } - } + assert.expect(2); - var s: IMyEventTester = new MyEventTester(); - var r = null; + let done = assert.async(); + let dispatcher = new EventDispatcher(); - var handler = (args: string) => { - r = args; - }; + let i = 0; - s.myEvent().subscribe(handler); - s.signal('Test1'); - assert.equal(r, 'Test1'); + dispatcher.subscribe((s, a) => { + i = a; + assert.equal(i, 1, 'i should be 1.'); + done(); + }); - s.myEvent().unsubscribe(handler); - s.signal('Test2'); - assert.equal(r, 'Test1'); + dispatcher.dispatchAsync(null, 1); + assert.equal(i, 0, 'Because of async dispatch, i should be 0.'); + }); }); -QUnit.test("Testing simple event list", (assert) => { +QUnit.module('Simple event', function () { - var events = new SimpleEventList(); - var result: string; + QUnit.test("Subscribe / unsubscribe - simple event as property", (assert) => { - events.get('Test1').subscribe((args) => result = args); - events.get('Test1').dispatch('Testing 123'); - assert.equal(result, 'Testing 123'); + assert.expect(2); - events.get('Test2').dispatch('Testing 456'); - assert.equal(result, 'Testing 123'); + class MyEventTester { + private _myEvent: SimpleEventDispatcher = new SimpleEventDispatcher(); - events.get('Test2').subscribe((args) => result = args); - events.get('Test2').dispatch('Testing 789'); - assert.equal(result, 'Testing 789'); + get myEvent(): ISimpleEvent { + return this._myEvent; + } - events.get('Test3').asEvent().subscribe((args) => result = args); - events.get('Test3').dispatch('Testing 42'); - assert.equal(result, 'Testing 42'); + signal(str: string): void { + this._myEvent.dispatch(str); + } + } -}); + let s = new MyEventTester(); + let result: string = null; -QUnit.test('Testing SimpleEventHandlingBase', (assert) => { + var handler = (args: string) => { + result = args; + }; - class MyTester extends SimpleEventHandlingBase { + s.myEvent.subscribe(handler); + s.signal('Test1'); + assert.equal(result, 'Test1', 'Result should be "Test1".'); - signal(name: string, str: string): void { - this.events.get(name).dispatch(str); - } - } + s.myEvent.unsubscribe(handler); + s.signal('Test2'); + assert.equal(result, 'Test1', 'Result should still be "Test1" because of unsubscribe.'); + }); - var t = new MyTester(); - var result: string; + QUnit.test("Subscribe / unsubscribe - simple event on interface", (assert) => { - t.subscribe('Test1', (args) => result = args); - t.signal('Test1', 'Testing 123'); - assert.equal(result, 'Testing 123'); + assert.expect(2); - t.signal('Test2', 'Testing 456'); - assert.equal(result, 'Testing 123'); + interface IMyEventTester { + myEvent(): ISimpleEvent; - t.subscribe('Test2', (args) => result = args); - t.signal('Test2', 'Testing 789'); - assert.equal(result, 'Testing 789'); -}); + signal(str: string); + } -QUnit.test('Testing event dispatcher', (assert) => { + class MyEventTester implements IMyEventTester { + private _myEvent: SimpleEventDispatcher = new SimpleEventDispatcher(); - class Source { } - class Argument { } + myEvent(): ISimpleEvent { + return this._myEvent; + } - let dispatcher = new EventDispatcher(); + signal(str: string): void { + this._myEvent.dispatch(str); + } + } - var s1 = new Source(); - var s2 = new Source(); - var a1 = new Argument(); - var a2 = new Argument(); + let s: IMyEventTester = new MyEventTester(); + let result: string = null; - dispatcher.subscribe((sender: Source, argument: Argument) => { - assert.equal(sender === s1, true); - assert.equal(sender === s2 ,false); + var handler = (args: string) => { + result = args; + }; - assert.equal(argument === a1, true); - assert.equal(argument === a2, false); + s.myEvent().subscribe(handler); + s.signal('Test1'); + assert.equal(result, 'Test1', 'Result should be "Test1".'); + + s.myEvent().unsubscribe(handler); + s.signal('Test2'); + assert.equal(result, 'Test1', 'Result should still be "Test1" because of unsubscribe.'); }); - dispatcher.dispatch(s1, a1); -}); + QUnit.test("Simple event list", (assert) => { + assert.expect(4); -QUnit.test('Testing simple event dispatcher', (assert) => { + var events = new SimpleEventList(); + var result: string; - class Argument { } + events.get('Test1').subscribe((args) => result = args); + events.get('Test1').dispatch('Testing 123'); + assert.equal(result, 'Testing 123', 'Result should be "Testing 123".'); - let dispatcher = new SimpleEventDispatcher< Argument>(); + events.get('Test2').dispatch('Testing 456'); + assert.equal(result, 'Testing 123', 'Result should still be "Testing 123", because of the unsubscribe.'); - var a1 = new Argument(); - var a2 = new Argument(); + events.get('Test2').subscribe((args) => result = args); + events.get('Test2').dispatch('Testing 789'); + assert.equal(result, 'Testing 789', 'Result should be "Testing 789".'); - dispatcher.subscribe((argument: Argument) => { - assert.equal(argument === a1, true); - assert.equal(argument === a2, false); + events.get('Test3').asEvent().subscribe((args) => result = args); + events.get('Test3').dispatch('Testing 42'); + assert.equal(result, 'Testing 42', 'Result of dispatch of interface should be "Testing 42".'); }); - dispatcher.dispatch(a1); -}); + QUnit.test('SimpleEventHandlingBase', (assert) => { + + assert.expect(3); -QUnit.test('Testing event async dispatch', (assert) => { + class MyTester extends SimpleEventHandlingBase { - let dispatcher = new EventDispatcher(); + signal(name: string, str: string): void { + this.events.get(name).dispatch(str); + } + } - let i = 0; + var t = new MyTester(); + var result: string; - dispatcher.subscribe((s, a) => { - i = a; + t.subscribe('Test1', (args: string) => result = args); + t.signal('Test1', 'Testing 123'); + assert.equal(result, 'Testing 123', 'The result should be "Testing 123".'); - assert.equal(i, 1); + t.signal('Test2', 'Testing 456'); + assert.equal(result, 'Testing 123', 'The result should still be "Testing 123".'); + + t.subscribe('Test2', (args: string) => result = args); + t.signal('Test2', 'Testing 789'); + assert.equal(result, 'Testing 789', 'The result should be "Testing 789".'); }); - dispatcher.dispatchAsync(null, 1); + QUnit.test('Dispatcher', (assert) => { - assert.equal(i, 0); -}); + assert.expect(2); -QUnit.test('Testing simple event async dispatch', (assert) => { + class Argument { constructor(public name: string) { } } - let dispatcher = new SimpleEventDispatcher(); + let dispatcher = new SimpleEventDispatcher(); - let i = 0; + var a1 = new Argument('a1'); + var a2 = new Argument('a2'); - dispatcher.subscribe((a) => { - i = a; + dispatcher.subscribe((argument: Argument) => { + assert.deepEqual(argument, a1, 'Argument should be a1.'); + assert.notDeepEqual(argument, a2, 'Argument should not be a2.'); + }); - assert.equal(i, 1); + dispatcher.dispatch(a1); }); - dispatcher.dispatchAsync(1); + QUnit.test('Async dispatch', (assert) => { - assert.equal(i, 0); -}); + let done = assert.async(); + let dispatcher = new SimpleEventDispatcher(); + + let i = 0; + + dispatcher.subscribe((a) => { + i = a; + assert.equal(i, 1, 'i should be 1.'); + done(); + }); + + dispatcher.dispatchAsync(1); + assert.equal(i, 0, 'Because of async dispatch, i should be 0.'); + }); +}); \ No newline at end of file