Skip to content

Commit f6960e3

Browse files
committed
add unit tests for SignalCommandTrigger class
1 parent becdc4c commit f6960e3

File tree

5 files changed

+344
-0
lines changed

5 files changed

+344
-0
lines changed
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) 2017 RobotlegsJS. All Rights Reserved.
3+
//
4+
// NOTICE: You are permitted to use, modify, and distribute this file
5+
// in accordance with the terms of the license agreement accompanying it.
6+
// ------------------------------------------------------------------------------
7+
8+
import "../../../../../entry.ts";
9+
10+
import sinon = require("sinon");
11+
12+
import { assert } from "chai";
13+
14+
import {
15+
ISignal,
16+
MonoSignal,
17+
OnceSignal,
18+
Signal,
19+
DeluxeSignal,
20+
PrioritySignal
21+
} from "@robotlegsjs/signals";
22+
23+
import {
24+
injectable,
25+
IInjector,
26+
RobotlegsInjector,
27+
CommandMapper
28+
} from "@robotlegsjs/core";
29+
30+
import { SignalCommandTrigger } from "../../../../../../src/robotlegs/bender/extensions/signalCommandMap/impl/SignalCommandTrigger";
31+
32+
import { CallbackCommand } from "../support/CallbackCommand";
33+
import { CallbackParametersCommand } from "../support/CallbackParametersCommand";
34+
import { NullCommand } from "../support/NullCommand";
35+
import { ParametersSignal } from "../support/ParametersSignal";
36+
37+
describe("SignalCommandTrigger", () => {
38+
let signal: ISignal;
39+
let injector: IInjector;
40+
let subject: SignalCommandTrigger;
41+
42+
beforeEach(() => {
43+
signal = new Signal();
44+
injector = new RobotlegsInjector();
45+
subject = new SignalCommandTrigger(injector, ISignal);
46+
});
47+
48+
afterEach(() => {
49+
signal.removeAll();
50+
signal = null;
51+
injector = null;
52+
subject = null;
53+
});
54+
55+
it("createMapper_returns_a_command_mapper", () => {
56+
let mapper: any = subject.createMapper();
57+
58+
assert.instanceOf(mapper, CommandMapper);
59+
});
60+
61+
it("test_activate_adds_a_listener", () => {
62+
let signalMock = sinon.mock(signal);
63+
64+
signalMock.expects("add").once();
65+
66+
injector.bind(ISignal).toConstantValue(signal);
67+
subject.activate();
68+
69+
signalMock.restore();
70+
signalMock.verify();
71+
});
72+
73+
it("test_activate_maps_signal_if_was_not_mapped_before", () => {
74+
subject = new SignalCommandTrigger(injector, Signal);
75+
subject.activate();
76+
});
77+
78+
it("test_deactivate_removes_listener", () => {
79+
let signalMock = sinon.mock(signal);
80+
81+
signalMock.expects("add").once();
82+
signalMock.expects("remove").once();
83+
84+
injector.bind(ISignal).toConstantValue(signal);
85+
subject.activate();
86+
subject.deactivate();
87+
88+
signalMock.restore();
89+
signalMock.verify();
90+
});
91+
92+
it("test_doesnt_throw_error_when_deactivating_without_signal", () => {
93+
let signalMock = sinon.mock(signal);
94+
95+
signalMock.expects("add").never();
96+
signalMock.expects("remove").never();
97+
98+
injector.bind(ISignal).toConstantValue(signal);
99+
subject.deactivate();
100+
101+
signalMock.restore();
102+
signalMock.verify();
103+
});
104+
105+
it("toString_returns_a_string", () => {
106+
assert.isString(subject.toString());
107+
});
108+
109+
it("command_is_triggered_when_signal_dispatch_without_parameters", () => {
110+
let triggered: boolean = false;
111+
let mapper: CommandMapper;
112+
113+
injector
114+
.bind("Function")
115+
.toFunction(() => {
116+
triggered = true;
117+
})
118+
.whenTargetNamed("executeCallback");
119+
120+
injector.bind(ISignal).toConstantValue(signal);
121+
mapper = subject.createMapper();
122+
mapper.toCommand(CallbackCommand);
123+
signal.dispatch();
124+
125+
assert.isTrue(triggered);
126+
});
127+
128+
it("command_is_not_triggered_when_trigger_is_deactivated", () => {
129+
let triggered: boolean = false;
130+
let mapper: CommandMapper;
131+
132+
injector
133+
.bind("Function")
134+
.toFunction(() => {
135+
triggered = true;
136+
})
137+
.whenTargetNamed("executeCallback");
138+
139+
injector.bind(ISignal).toConstantValue(signal);
140+
mapper = subject.createMapper();
141+
mapper.toCommand(CallbackCommand);
142+
subject.deactivate();
143+
signal.dispatch();
144+
145+
assert.isFalse(triggered);
146+
});
147+
148+
it("command_is_triggered_and_receives_parameters_from_signal_without_value_classes", () => {
149+
let expected: any[] = [
150+
true,
151+
999,
152+
"I'm a string!",
153+
ISignal,
154+
{ x: 5, y: 5 },
155+
new Date(),
156+
[1, 2, 3, 4, 5, 6, 7, 8, 9]
157+
];
158+
let actual: any[] = [];
159+
160+
let mapper: CommandMapper;
161+
162+
injector
163+
.bind("Function")
164+
.toFunction((parameter: any) => {
165+
actual.push(parameter);
166+
})
167+
.whenTargetNamed("reportParameter");
168+
169+
injector.bind(ISignal).toConstantValue(signal);
170+
mapper = subject.createMapper();
171+
mapper.toCommand(CallbackParametersCommand);
172+
signal.dispatch.apply(signal, expected);
173+
174+
assert.deepEqual(actual, expected);
175+
});
176+
177+
it("command_is_triggered_and_receives_parameters_from_signal_with_value_classes", () => {
178+
let expected: any[] = [
179+
true,
180+
999,
181+
"I'm a string!",
182+
ISignal,
183+
{ x: 5, y: 5 },
184+
new Date(),
185+
[1, 2, 3, 4, 5, 6, 7, 8, 9]
186+
];
187+
let actual: any[] = [];
188+
189+
let mapper: CommandMapper;
190+
191+
injector
192+
.bind("Function")
193+
.toFunction((parameter: any) => {
194+
actual.push(parameter);
195+
})
196+
.whenTargetNamed("reportParameter");
197+
198+
signal = new ParametersSignal();
199+
injector.bind(ISignal).toConstantValue(signal);
200+
mapper = subject.createMapper();
201+
mapper.toCommand(CallbackParametersCommand);
202+
signal.dispatch.apply(signal, expected);
203+
204+
assert.deepEqual(actual, expected);
205+
});
206+
207+
it("mapping_processor_is_called", () => {
208+
let processors: Function[] = [];
209+
let callCount: number = 0;
210+
let mapper: CommandMapper;
211+
212+
processors.push((mapping: any) => {
213+
callCount++;
214+
});
215+
216+
injector.bind(ISignal).toConstantValue(signal);
217+
subject = new SignalCommandTrigger(injector, ISignal, processors);
218+
mapper = subject.createMapper();
219+
mapper.toCommand(NullCommand);
220+
signal.dispatch();
221+
222+
assert.equal(callCount, 1);
223+
});
224+
225+
it("mapping_processors_are_called", () => {
226+
let processors: Function[] = [];
227+
let callCount: number = 0;
228+
let mapper: CommandMapper;
229+
230+
processors.push((mapping: any) => {
231+
callCount++;
232+
});
233+
processors.push((mapping: any) => {
234+
callCount++;
235+
});
236+
processors.push((mapping: any) => {
237+
callCount++;
238+
});
239+
240+
injector.bind(ISignal).toConstantValue(signal);
241+
subject = new SignalCommandTrigger(injector, ISignal, processors);
242+
mapper = subject.createMapper();
243+
mapper.toCommand(NullCommand);
244+
signal.dispatch();
245+
246+
assert.equal(callCount, 3);
247+
});
248+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) 2017 RobotlegsJS. All Rights Reserved.
3+
//
4+
// NOTICE: You are permitted to use, modify, and distribute this file
5+
// in accordance with the terms of the license agreement accompanying it.
6+
// ------------------------------------------------------------------------------
7+
8+
import { injectable, inject, named } from "inversify";
9+
10+
import { ICommand } from "@robotlegsjs/core";
11+
12+
@injectable()
13+
export class CallbackCommand implements ICommand {
14+
@inject("Function")
15+
@named("executeCallback")
16+
public callback: Function;
17+
18+
public execute(): void {
19+
this.callback();
20+
}
21+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) 2017 RobotlegsJS. All Rights Reserved.
3+
//
4+
// NOTICE: You are permitted to use, modify, and distribute this file
5+
// in accordance with the terms of the license agreement accompanying it.
6+
// ------------------------------------------------------------------------------
7+
8+
import { injectable, inject, named } from "inversify";
9+
10+
import { ICommand } from "@robotlegsjs/core";
11+
12+
@injectable()
13+
export class CallbackParametersCommand implements ICommand {
14+
@inject(Boolean) public booleanValue: boolean;
15+
16+
@inject(Number) public numValue: number;
17+
18+
@inject(String) public stringValue: string;
19+
20+
@inject(Symbol) public symbolValue: symbol;
21+
22+
@inject(Object) public objectValue: object;
23+
24+
@inject(Date) public dateValue: Date;
25+
26+
@inject(Array) public arrayValue: any[];
27+
28+
@inject("Function")
29+
@named("reportParameter")
30+
public reportParameter: Function;
31+
32+
public execute(): void {
33+
this.reportParameter(this.booleanValue);
34+
this.reportParameter(this.numValue);
35+
this.reportParameter(this.stringValue);
36+
this.reportParameter(this.symbolValue);
37+
this.reportParameter(this.objectValue);
38+
this.reportParameter(this.dateValue);
39+
this.reportParameter(this.arrayValue);
40+
}
41+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) 2017 RobotlegsJS. All Rights Reserved.
3+
//
4+
// NOTICE: You are permitted to use, modify, and distribute this file
5+
// in accordance with the terms of the license agreement accompanying it.
6+
// ------------------------------------------------------------------------------
7+
8+
import { injectable, inject, named } from "inversify";
9+
10+
import { ICommand } from "@robotlegsjs/core";
11+
12+
@injectable()
13+
export class NullCommand implements ICommand {
14+
public execute(): void {
15+
// do nothing.
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) 2017 RobotlegsJS. All Rights Reserved.
3+
//
4+
// NOTICE: You are permitted to use, modify, and distribute this file
5+
// in accordance with the terms of the license agreement accompanying it.
6+
// ------------------------------------------------------------------------------
7+
8+
import { injectable } from "inversify";
9+
10+
import { Signal } from "@robotlegsjs/signals";
11+
12+
@injectable()
13+
export class ParametersSignal extends Signal {
14+
constructor() {
15+
super(Boolean, Number, String, Symbol, Object, Date, Array);
16+
}
17+
}

0 commit comments

Comments
 (0)