From 40181779b2870b4b4715f0b1185f8333ebac81a3 Mon Sep 17 00:00:00 2001
From: adithya <adithyav102@gmail.com>
Date: Mon, 21 Oct 2024 23:48:03 +0530
Subject: [PATCH] Improved test coverage

---
 .../askForTalawaWebSocketUrl.test.ts          | 80 ++++++++++++++++++-
 .../askForTalawaWebSocketUrl.ts               |  2 +-
 2 files changed, 80 insertions(+), 2 deletions(-)

diff --git a/src/setup/askForTalawaWebSocketUrl/askForTalawaWebSocketUrl.test.ts b/src/setup/askForTalawaWebSocketUrl/askForTalawaWebSocketUrl.test.ts
index 5677e4d07a..e7acd31b56 100644
--- a/src/setup/askForTalawaWebSocketUrl/askForTalawaWebSocketUrl.test.ts
+++ b/src/setup/askForTalawaWebSocketUrl/askForTalawaWebSocketUrl.test.ts
@@ -1,10 +1,88 @@
-import inquirer from 'inquirer';
+import inquirer, { InputQuestion, QuestionCollection } from 'inquirer';
 import { askForTalawaWebSocketUrl } from './askForTalawaWebSocketUrl';
+import { isValidWebSocketUrl } from './askForTalawaWebSocketUrl';
 
 jest.mock('inquirer', () => ({
   prompt: jest.fn(),
 }));
 
+describe('Checkes validity of WebSocket URL', () => {
+  test('should return true for a valid ws:// URL', () => {
+    const validUrl = 'ws://example.com/graphql/';
+    expect(isValidWebSocketUrl(validUrl)).toBe(true);
+  });
+
+  test('should return true for a valid wss:// URL', () => {
+    const validUrl = 'wss://example.com/graphql/';
+    expect(isValidWebSocketUrl(validUrl)).toBe(true);
+  });
+
+  test('should return false for an invalid WebSocket URL', () => {
+    const invalidUrl = 'http://example.com/graphql/';
+    expect(isValidWebSocketUrl(invalidUrl)).toBe(false);
+  });
+
+  test('should return false for a malformed URL', () => {
+    const malformedUrl = 'not-a-valid-url';
+    expect(isValidWebSocketUrl(malformedUrl)).toBe(false);
+  });
+
+  test('should return false for an empty string', () => {
+    const emptyUrl = '';
+    expect(isValidWebSocketUrl(emptyUrl)).toBe(false);
+  });
+});
+
+describe('askForTalawaWebSocketUrl validation', () => {
+  beforeEach(() => {
+    jest.clearAllMocks();
+  });
+
+  test('should return an error message for invalid WebSocket URLs', async () => {
+    const mockPrompt = jest
+      .spyOn(inquirer, 'prompt')
+      .mockImplementation(async (questions: QuestionCollection) => {
+        const validate = (questions as InputQuestion[])[0].validate!;
+        const result = validate('http://invalid-url');
+        expect(result).toBe(
+          'Please enter a valid WebSocket URL (starting with ws:// or wss://).',
+        );
+        return { endpoint: 'http://invalid-url' };
+      });
+
+    await askForTalawaWebSocketUrl();
+    expect(mockPrompt).toHaveBeenCalled();
+  });
+
+  test('should return true for valid WebSocket URLs', async () => {
+    const mockPrompt = jest
+      .spyOn(inquirer, 'prompt')
+      .mockImplementation(async (questions: QuestionCollection) => {
+        const validate = (questions as InputQuestion[])[0].validate!;
+        const result = validate('ws://valid-url.com');
+        expect(result).toBe(true);
+        return { endpoint: 'ws://valid-url.com' };
+      });
+
+    await askForTalawaWebSocketUrl();
+    expect(mockPrompt).toHaveBeenCalled();
+  });
+
+  test('should return true for empty input (default behavior)', async () => {
+    const mockPrompt = jest
+      .spyOn(inquirer, 'prompt')
+      .mockImplementation(async (questions: QuestionCollection) => {
+        const validate = (questions as InputQuestion[])[0].validate!;
+        const result = validate('');
+        expect(result).toBe(true);
+        return { endpoint: '' };
+      });
+
+    await askForTalawaWebSocketUrl();
+    expect(mockPrompt).toHaveBeenCalled();
+  });
+});
+
 describe('askForTalawaWebSocketUrl', () => {
   beforeEach(() => {
     jest.clearAllMocks();
diff --git a/src/setup/askForTalawaWebSocketUrl/askForTalawaWebSocketUrl.ts b/src/setup/askForTalawaWebSocketUrl/askForTalawaWebSocketUrl.ts
index ae9d657105..ae593738f6 100644
--- a/src/setup/askForTalawaWebSocketUrl/askForTalawaWebSocketUrl.ts
+++ b/src/setup/askForTalawaWebSocketUrl/askForTalawaWebSocketUrl.ts
@@ -1,6 +1,6 @@
 import inquirer from 'inquirer';
 
-function isValidWebSocketUrl(url: string): boolean {
+export function isValidWebSocketUrl(url: string): boolean {
   try {
     const parsedUrl = new URL(url);
     return parsedUrl.protocol === 'ws:' || parsedUrl.protocol === 'wss:';