問題は、facebook などの SDK によって実行される非同期内部メソッドを使用してイベント ハンドラーをテストすることです。
単純なテストは次のとおりです。
describe('Listens to someevent', function () {
it('and triggers anotherevent', function () {
var eventSpy = spyOnEvent(document, 'anotherevent');
var data = {
param1: 'param1',
param2: 'param2',
}
this.component.trigger('somevent', data);
runs(function() {
expect(eventSpy).toHaveBeenTriggeredOn(document);
});
});
});
オプションで someevent がトリガーされると、コンポーネント ハンドラーが起動されます。
this.handler = function (e, data) {
SDK.apicall(data, function (err, response) {
if (!err) {
doSomething();
}
// trigger data event
that.trigger(document, 'anotherevent');
});
}
;
};