forked from KamandPrompt/baat-cheet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ Add tests to verify a new connection. KamandPrompt#140
- Loading branch information
Showing
3 changed files
with
234 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.