Skip to content

Commit

Permalink
test(server): added mock server implementation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
knagaitsev committed Jul 3, 2019
1 parent 479724c commit fbb855a
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 0 deletions.
74 changes: 74 additions & 0 deletions test/server/__snapshots__/serverMode-option.test.js.snap
Expand Up @@ -7,3 +7,77 @@ Array [
"close",
]
`;

exports[`serverMode option server should close client with bad headers 1`] = `
Array [
Array [
[Function],
],
]
`;

exports[`serverMode option server should close client with bad headers 2`] = `
Array [
Array [
Object {
"foo": "bar",
},
"{\\"type\\":\\"error\\",\\"data\\":\\"Invalid Host/Origin header\\"}",
],
]
`;

exports[`serverMode option server should close client with bad headers 3`] = `
Array [
Array [
Object {
"foo": "bar",
},
],
]
`;

exports[`serverMode option server should use server implementation correctly 1`] = `
Array [
Object {
"foo": "bar",
},
]
`;

exports[`serverMode option server should use server implementation correctly 2`] = `
Array [
Array [
[Function],
],
]
`;

exports[`serverMode option server should use server implementation correctly 3`] = `
Array [
Object {
"foo": "bar",
},
"{\\"type\\":\\"liveReload\\"}",
]
`;

exports[`serverMode option server should use server implementation correctly 4`] = `
Array [
Object {
"foo": "bar",
},
"{\\"type\\":\\"ok\\"}",
]
`;

exports[`serverMode option server should use server implementation correctly 5`] = `
Array [
Array [
Object {
"foo": "bar",
},
[Function],
],
]
`;
87 changes: 87 additions & 0 deletions test/server/serverMode-option.test.js
Expand Up @@ -305,4 +305,91 @@ describe('serverMode option', () => {
});
});
});

describe('server', () => {
let MockSockJSServer;
beforeEach((done) => {
jest.mock('../../lib/servers/SockJSServer');
// eslint-disable-next-line global-require
mockedTestServer = require('../helpers/test-server');
// eslint-disable-next-line global-require
MockSockJSServer = require('../../lib/servers/SockJSServer');

server = mockedTestServer.start(
config,
{
port,
},
done
);
});

afterEach((done) => {
mockedTestServer.close(done);
jest.resetAllMocks();
jest.resetModules();

server = null;
});

it('should use server implementation correctly', () => {
const mockServerInstance = MockSockJSServer.mock.instances[0];

const connectionObj = {
foo: 'bar',
};
// this simulates a client connecting to the server
mockServerInstance.onConnection.mock.calls[0][0](connectionObj, {
host: `localhost:${port}`,
origin: `http://localhost:${port}`,
});

expect(server.sockets.length).toEqual(1);
expect(server.sockets).toMatchSnapshot();

// this simulates a client leaving the server
mockServerInstance.onConnectionClose.mock.calls[0][1](connectionObj);

expect(server.sockets.length).toEqual(0);

// check that the dev server was passed to the socket server implementation constructor
expect(MockSockJSServer.mock.calls[0].length).toEqual(1);
expect(MockSockJSServer.mock.calls[0][0].options.port).toEqual(port);

expect(mockServerInstance.onConnection.mock.calls).toMatchSnapshot();
expect(mockServerInstance.send.mock.calls.length).toEqual(3);
// call 0 to the send() method is liveReload
expect(mockServerInstance.send.mock.calls[0]).toMatchSnapshot();
// call 1 to the send() method is hash data, so we skip it
// call 2 to the send() method is the "ok" message
expect(mockServerInstance.send.mock.calls[2]).toMatchSnapshot();
// close should not be called because the server never forcefully closes
// a successful client connection
expect(mockServerInstance.close.mock.calls.length).toEqual(0);
expect(mockServerInstance.onConnectionClose.mock.calls).toMatchSnapshot();
});

it('should close client with bad headers', () => {
const mockServerInstance = MockSockJSServer.mock.instances[0];

// this simulates a client connecting to the server
mockServerInstance.onConnection.mock.calls[0][0](
{
foo: 'bar',
},
{
host: null,
}
);
expect(server.sockets.length).toEqual(0);
expect(MockSockJSServer.mock.calls[0].length).toEqual(1);
expect(MockSockJSServer.mock.calls[0][0].options.port).toEqual(port);
expect(mockServerInstance.onConnection.mock.calls).toMatchSnapshot();
// the only call to send() here should be an invalid header message
expect(mockServerInstance.send.mock.calls).toMatchSnapshot();
expect(mockServerInstance.close.mock.calls).toMatchSnapshot();
// onConnectionClose should never get called since the client should be closed first
expect(mockServerInstance.onConnectionClose.mock.calls.length).toEqual(0);
});
});
});

0 comments on commit fbb855a

Please sign in to comment.