✅ Add a few unit tests for helpers.js (#4529)

This commit is contained in:
Billy Richardson authored and GitHub committed 2022-05-31 14:17:27 +02:00
1 parent fe84605f24
commit e8be86c15f
2 files changed
+227 -2

No files matched your search

+6 -2
View File
@@ -1555,7 +1555,7 @@ var copyToClipboard = function (text) {
};
var getExternalHostUrl = function () {
var loc = window.location;
var loc = fetchWindowLocation();
var port = "";
if (
(loc.protocol === "http:" && loc.port !== "80") ||
@@ -1576,7 +1576,7 @@ var validateWebcamUrl = function (streamUrl) {
if (lower.startsWith("//")) {
// protocol relative
toParse = window.location.protocol + streamUrl;
toParse = fetchWindowLocation().protocol + streamUrl;
} else if (lower.startsWith("/")) {
// host relative
toParse = getExternalHostUrl() + streamUrl;
@@ -1751,3 +1751,7 @@ var startWebRTC = function (videoElement, streamUrl, iceServers) {
negotiateWebRTC(streamUrl);
return pc;
};
var fetchWindowLocation = function () {
return window.location;
};
+221
View File
@@ -189,3 +189,224 @@ QUnit.cases(
"As expected: " + String(params.expected)
);
});
QUnit.module("determineWebcamStreamType");
QUnit.cases(
(function () {
var cases = [];
var params = [
{
streamUrl: undefined,
exceptionExpected: "Empty streamUrl. Cannot determine stream type.",
expectedResult: undefined
},
{
streamUrl: "invalidUrl",
exceptionExpected: "Invalid streamUrl. Cannot determine stream type.",
expectedResult: undefined
},
{
streamUrl: "webrtc://localhost/stream",
exceptionExpected: undefined,
expectedResult: "webrtc"
},
{
streamUrl: "webrtcs://localhost/stream",
exceptionExpected: undefined,
expectedResult: "webrtc"
},
{
streamUrl: "https://localhost/stream.m3u8",
exceptionExpected: undefined,
expectedResult: "hls"
},
{
streamUrl: "https://localhost/stream",
exceptionExpected: undefined,
expectedResult: "mjpg"
}
];
var param, i;
for (i = 0; i < params.length; i++) {
param = params[i];
param["title"] =
String(param.streamUrl) +
String(param.exceptionExpected) +
String(param.expectedResult);
cases.push(param);
}
return cases;
})()
).test("determineWebcamStreamType", function (params, assert) {
var exceptionMessage = undefined;
var result = undefined;
try {
result = determineWebcamStreamType(params.streamUrl);
} catch (ex) {
exceptionMessage = ex;
}
assert.equal(
params.exceptionExpected,
exceptionMessage,
"Exception expected: " + String(params.exceptionExpected)
);
assert.equal(
params.expectedResult,
result,
"As expected: " + String(params.expectedResult)
);
});
QUnit.module("validateWebcamUrl");
QUnit.cases(
(function () {
var cases = [];
var params = [
{
streamUrl: undefined,
expected: false,
windowLocationOverride: undefined
},
{
streamUrl: "//localhost/stream",
expected: new URL("https://localhost/stream"),
windowLocationOverride: {protocol: "https:"}
},
{
streamUrl: "/stream",
expected: new URL("https://localhost/stream"),
windowLocationOverride: {
protocol: "https:",
port: "443",
hostname: "localhost"
}
},
{
streamUrl: "http://localhost/stream",
expected: new URL("http://localhost/stream"),
windowLocationOverride: undefined
},
{
streamUrl: "https://localhost/stream",
expected: new URL("https://localhost/stream"),
windowLocationOverride: undefined
},
{
streamUrl: "webrtc://localhost/stream",
expected: new URL("webrtc://localhost/stream"),
windowLocationOverride: undefined
},
{
streamUrl: "webrtcs://localhost/stream",
expected: new URL("webrtcs://localhost/stream"),
windowLocationOverride: undefined
},
{
streamUrl: "invalid",
expected: false,
windowLocationOverride: undefined
},
{
streamUrl: "//exception",
expected: false,
windowLocationOverride: {protocol: ":"}
}
];
var param, i;
for (i = 0; i < params.length; i++) {
param = params[i];
param["title"] = String(param.streamUrl) + String(param.expected);
cases.push(param);
}
return cases;
})()
).test("validateWebcamUrl", function (params, assert) {
if (params.windowLocationOverride) {
fetchWindowLocation = function () {
return params.windowLocationOverride;
};
}
var result = validateWebcamUrl(params.streamUrl);
assert.equal(
params.expected.toString(),
result.toString(),
"As expected: " + String(params.expected)
);
});
QUnit.module("getExternalHostUrl");
QUnit.cases(
(function () {
var cases = [];
var params = [
{
windowLocation: {
protocol: "https:",
port: "443",
hostname: "localhost"
},
expected: "https://localhost"
},
{
windowLocation: {
protocol: "https:",
port: "8000",
hostname: "localhost"
},
expected: "https://localhost:8000"
},
{
windowLocation: {
protocol: "http:",
port: "80",
hostname: "localhost"
},
expected: "http://localhost"
},
{
windowLocation: {
protocol: "http:",
port: "8000",
hostname: "localhost"
},
expected: "http://localhost:8000"
}
];
var param, i;
for (i = 0; i < params.length; i++) {
param = params[i];
param["title"] =
String(param.windowLocation.protocol) +
"//" +
String(param.windowLocation.hostname) +
String(param.windowLocation.port);
cases.push(param);
}
return cases;
})()
).test("getExternalHostUrl", function (params, assert) {
if (params.windowLocation) {
fetchWindowLocation = function () {
return params.windowLocation;
};
}
var result = getExternalHostUrl();
assert.equal(params.expected, result, "As expected: " + String(params.expected));
});