From 2bc165379a299a87001bd84d3647cc70d2297106 Mon Sep 17 00:00:00 2001
From: David Twigger <david.twigger@raisepartner.com>
Date: Wed, 11 Jan 2023 13:28:30 +0100
Subject: [PATCH] Add unit test for hostNameRegexPattern

---
 test/cypress/unit/util-frontend.spec.js | 33 +++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
 create mode 100644 test/cypress/unit/util-frontend.spec.js

diff --git a/test/cypress/unit/util-frontend.spec.js b/test/cypress/unit/util-frontend.spec.js
new file mode 100644
index 000000000..6abedf821
--- /dev/null
+++ b/test/cypress/unit/util-frontend.spec.js
@@ -0,0 +1,33 @@
+import { hostNameRegexPattern } from "../../../src/util-frontend";
+
+describe("Test util-frontend.js", () => {
+
+    describe("hostNameRegexPattern()", () => {
+        it('should return a valid regex for non mqtt hostnames', () => {
+            const regex = new RegExp(hostNameRegexPattern(false));
+
+            expect(regex.test("www.test.com")).to.be.true;
+            expect(regex.test("127.0.0.1")).to.be.true;
+            expect(regex.test("192.168.1.156")).to.be.true;
+            
+            ["mqtt", "mqtts", "ws", "wss"].forEach(schema => {
+                expect(regex.test(`${schema}://www.test.com`)).to.be.false;
+                expect(regex.test(`${schema}://127.0.0.1`)).to.be.false;
+            });
+        });
+        it('should return a valid regex for mqtt hostnames', () => {
+            const hostnameString = hostNameRegexPattern(false);
+            console.log('*********', hostnameString, '***********');
+            const regex = new RegExp(hostNameRegexPattern(true));
+
+            expect(regex.test("www.test.com")).to.be.true;
+            expect(regex.test("127.0.0.1")).to.be.true;
+            expect(regex.test("192.168.1.156")).to.be.true;
+            
+            ["mqtt", "mqtts", "ws", "wss"].forEach(schema => {
+                expect(regex.test(`${schema}://www.test.com`)).to.be.true;
+                expect(regex.test(`${schema}://127.0.0.1`)).to.be.true;
+            });
+        });
+    });
+});
\ No newline at end of file