Server : nginx/1.18.0 System : Linux localhost 6.14.3-x86_64-linode168 #1 SMP PREEMPT_DYNAMIC Mon Apr 21 19:47:55 EDT 2025 x86_64 User : www-data ( 33) PHP Version : 8.0.16 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, Directory : /var/www/ecommerce/node_modules/pusher-js/spec/javascripts/unit/node/ |
var Mocks = require('mocks');
var TimelineSender = require('core/timeline/timeline_sender').default;
var Runtime = require('runtime').default;
describe("TimelineSender", function() {
var timeline, onSend, sender;
beforeEach(function(){
timeline = Mocks.getTimeline();
timeline.isEmpty.andReturn(false);
timeline.send.andCallFake(function(sendXHR, callback) {
sendXHR({ events: [1, 2, 3]}, callback);
});
onSend = jasmine.createSpy("onSend");
sender = new TimelineSender(timeline, {
host: "example.com",
path: "/timeline"
});
});
describe("XHR", function(){
var xhrRequest;
beforeEach(function() {
spyOn(Runtime, "createXHR").andCallFake(function() {
xhrRequest = Mocks.getXHR();
return xhrRequest;
});
});
describe("on construction", function() {
it("should expose options", function() {
sender = new TimelineSender(timeline, {
host: "localhost",
port: 666
});
expect(sender.options).toEqual({
host: "localhost",
port: 666
});
});
});
describe("on send", function() {
it("should send a non-empty timeline", function() {
sender.send(false, onSend);
expect(Runtime.createXHR.calls.length).toEqual(1);
var encodedParams = 'WzEsMiwzXQ%3D%3D';
expect(xhrRequest.open).toHaveBeenCalledWith(
"GET",
'http://example.com/timeline/2?events=WzEsMiwzXQ%3D%3D',
true);
expect(xhrRequest.send).toHaveBeenCalled();
});
it("should not send an empty timeline", function() {
timeline.isEmpty.andReturn(true);
sender.send(false, onSend);
expect(Runtime.createXHR).not.toHaveBeenCalled();
});
it("should use returned hostname for subsequent requests", function() {
sender.send(false);
xhrRequest.readyState = 4;
xhrRequest.status = 200;
xhrRequest.responseText = JSON.stringify({host: "returned.example.com"});
xhrRequest.onreadystatechange();
sender.send(false);
expect(xhrRequest.open).toHaveBeenCalledWith(
'GET',
'http://returned.example.com/timeline/2?events=WzEsMiwzXQ%3D%3D',
true
);
});
});
});
});