Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

socket mock

var socketMock;
var windowMock;
var address = 'ws://test.address';

describe(".init(address, window)", function() {
  beforeEach(function() {
    var WebSocket = jasmine.createSpy();
    WebSocket.and.callFake(function (url) {
      socketMock = {
        url: url,
        readyState: WebSocket.CONNECTING,
        send: jasmine.createSpy(),
        close: jasmine.createSpy().and.callFake(function () {
          socketMock.readyState = WebSocket.CLOSING;
        }),

        // methods to mock the internal behaviour of the real WebSocket
        _open: function () {
          socketMock.readyState = WebSocket.OPEN;
          socketMock.onopen && socketMock.onopen();
        },
        _message: function (msg) {
          socketMock.onmessage && socketMock.onmessage({data: msg});
        },
        _error: function () {
          socketMock.readyState = WebSocket.CLOSED;
          socketMock.onerror && socketMock.onerror();
        },
        _close: function () {
          socketMock.readyState = WebSocket.CLOSED;
          socketMock.onclose && socketMock.onclose();
        }
      };
      return socketMock;
    });
    WebSocket.CONNECTING = 0;
    WebSocket.OPEN = 1;
    WebSocket.CLOSING = 2;
    WebSocket.CLOSED = 3;

    windowMock = {
      WebSocket: WebSocket
    };
    spyOn(subject, 'handleMessage');
  });

  it("should create a WebSocket client which connects to the given address", function() {
    subject.init(address, windowMock);
    expect(windowMock.WebSocket).toHaveBeenCalledWith(address);
  });

  it("should have onmessage method overriden with a function which handles message", function() {
    var message = 'hello socket';
    subject.init(address, window);

    // pretend the socket connected (optional)
    socketMock._open();

    // pretend the socket got a message
    socketMock._message(message)

    expect(subject.handleMessage).toHaveBeenCalledWith(message);
  });
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: how we use usefef in map function 
Javascript :: javascript leetcode solutions 
Javascript :: how to make password star star on input html 
Javascript :: vue expected string with value got number with value 
Javascript :: diagonal difference javascript 
Javascript :: override print command javascript 
Javascript :: how to create a variable with a operator in javascript 
Javascript :: reactjs copytoclipboard box 
Javascript :: js get word before question mark 
Javascript :: react native expo PayloadTooLargeError: request entity too large 
Javascript :: ip address pattern regex javascript 
Javascript :: how to make a box in p5js 
Javascript :: GRAPHQL_VALIDATION_FAILED) GraphQLError: Enum ENUM_MESSAGE_TYPE" cannot represent non-enum value: comment. Did you mean the enum value comment 
Javascript :: how to detect two objects overlapping in javascript 
Javascript :: material icon button ripple 
Javascript :: notion value of empte date property 
Javascript :: javascript multiple enventListeners in one 
Javascript :: save date time without timezone to databse javascript 
Javascript :: what is the difference between throttling and debounce and raf throttling in react 
Javascript :: /home/raj/Desktop/webProjects/node-shop-api/node_modules/whatwg-url/dist/encoding.js:2 const utf8Encoder = new TextEncoder(); ^ ReferenceError: TextEncoder is not defined 
Javascript :: nested array generator for js 
Javascript :: jquery click ony works once on dropdown 
Javascript :: acender lampada javascript 
Javascript :: appendchild js 
Javascript :: scrollto element by id 
Javascript :: undefined ext in fn.dataTable.ext.search.push 
Javascript :: React Redux component getById crud exemple 
Javascript :: focus on child components on single page applications - 2 
Javascript :: mongoose operand find method 
Javascript :: json data find 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =