Skip to content

Commit

Permalink
✅ Add tests to verify a new connection. KamandPrompt#140
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeconias authored and LalitNM committed Oct 19, 2020
1 parent 8c81d4a commit f65498a
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 163 deletions.
111 changes: 111 additions & 0 deletions app.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const { assert } = require("chai");
const io = require("socket.io-client");

const PORT = process.env.PORT || 3000;
const BAAT_HOST = process.env.BAAT_HOST || "http://localhost";

const SERVER = `${BAAT_HOST}:${PORT}`;

describe("App.js", () => {
let socket;
let client;

beforeEach(() => {
socket = io(SERVER);
client = socket.connect();
});

afterEach(() => {
if (client) client.disconnect();
if (socket) socket.close();
});

it("should test if client is connected", (done) => {
client.on("connect", () => {
assert.isTrue(client.connected);
client.disconnect();
socket.close();
done();
});
});

it("should test if name is send", (done) => {
client.on("connect", () => {
assert.isTrue(client.connected);

client.on("user invalid", (data) => {
assert.isObject(data);
assert.hasAllKeys(data, "username");
assert.strictEqual(data.username, " is invalid.");
done();
});

client.emit("set username", undefined);
});
});

it("should test if broadcast notify all users (except sender)", (done) => {
const localSocket = io(SERVER);
const localClient = localSocket.connect();

client.on("connect", () => {
assert.isTrue(client.connected);

client.emit("set username", "baatCheet");

client.disconnect();
socket.close();
});

localClient.on("connect", () => {
assert.isTrue(localClient.connected);

localClient.on("user joined", (data) => {
assert.isObject(data);
assert.hasAnyKeys(data, "username");
assert.strictEqual(data.username, "baatCheet");

localClient.disconnect();
localSocket.close();

done();
});

});
});

it("should test if user exist", (done) => {
const socket = io(SERVER);
const socketTwo = io(SERVER);
const client = socket.connect();
const clienTwo = socketTwo.connect();

client.on("connect", () => {
assert.isTrue(client.connected);

client.on("user set", (data) => {
assert.isObject(data);
assert.hasAnyKeys(data, "username");
assert.strictEqual(data.username, "uniqueName");
});

client.emit("set username", "uniqueName");
});

clienTwo.on("connect", () => {
assert.isTrue(clienTwo.connected);

clienTwo.on("user exists", (data) => {
assert.strictEqual(data, "uniqueName");

client.disconnect();
clienTwo.disconnect();
socket.close();
socketTwo.close();
done();
});

clienTwo.emit("set username", "uniqueName");
});
});
});
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"description": "An anonymous chat site",
"main": "index.js",
"scripts": {
"start": "node app.js"
"start": "node app.js",
"test:watch": "mocha --watch ./*.test.js",
"test": "mocha ./*.test.js"
},
"engines": {
"npm": "3.10.10",
Expand All @@ -26,7 +28,7 @@
},
"devDependencies": {
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"mocha": "^8.2.0"
"mocha": "^8.2.0",
"socket.io-client": "^2.3.1"
}
}
Loading

0 comments on commit f65498a

Please sign in to comment.