Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adithyanotfound committed Oct 31, 2024
1 parent 5899f05 commit 597e244
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/setup/askForTalawaApiUrl/setupTalawaWebSocketUrl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import fs from 'fs';
import inquirer from 'inquirer';
import { askForTalawaApiUrl } from './askForTalawaApiUrl';

jest.mock('fs');
jest.mock('inquirer', () => ({
prompt: jest.fn(),
}));

describe('WebSocket URL Configuration', () => {
beforeEach(() => {
jest.resetAllMocks();
});

test('should convert http URL to ws WebSocket URL', async () => {
const endpoint = 'http://example.com/graphql';
const websocketUrl = endpoint.replace(/^http(s)?:\/\//, 'ws$1://');

expect(websocketUrl).toBe('ws://example.com/graphql');
});

test('should convert https URL to wss WebSocket URL', async () => {
const endpoint = 'https://example.com/graphql';
const websocketUrl = endpoint.replace(/^http(s)?:\/\//, 'ws$1://');

expect(websocketUrl).toBe('wss://example.com/graphql');
});

test('should retain default WebSocket URL if no new endpoint is provided', async () => {
jest
.spyOn(inquirer, 'prompt')
.mockResolvedValueOnce({ endpoint: 'http://localhost:4000/graphql/' });
await askForTalawaApiUrl();

const writeFileSyncSpy = jest.spyOn(fs, 'writeFileSync');
expect(writeFileSyncSpy).not.toHaveBeenCalled();
});
});

0 comments on commit 597e244

Please sign in to comment.