Skip to content

Commit

Permalink
refracted jest/vitest issue -(PalisadoesFoundation#2749)
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanrule committed Dec 28, 2024
1 parent 3d1f1f0 commit b240fcb
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/setup/askForTalawaApiUrl/askForTalawaApiUrl.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import inquirer from 'inquirer';
import { askForTalawaApiUrl } from './askForTalawaApiUrl';
import { vi, it, describe, expect, beforeEach } from 'vitest';

vi.mock('inquirer', async (importOriginal) => {
const actual = await importOriginal<typeof inquirer>(); // Adding explicit type
return {
...actual,
prompt: vi.fn(),
};
});

describe('askForTalawaApiUrl', () => {
beforeEach(() => {
vi.clearAllMocks();
});

it('should return the provided endpoint when user enters it', async () => {
const mockPrompt = vi.spyOn(inquirer, 'prompt').mockResolvedValueOnce({
endpoint: 'http://example.com/graphql/',
});

const result = await askForTalawaApiUrl();

expect(mockPrompt).toHaveBeenCalledWith([
{
type: 'input',
name: 'endpoint',
message: 'Enter your talawa-api endpoint:',
default: 'http://localhost:4000/graphql/',
},
]);

expect(result).toBe('http://example.com/graphql/');
});

it('should return the default endpoint when the user does not enter anything', async () => {
const mockPrompt = vi
.spyOn(inquirer, 'prompt')
.mockImplementation(async (questions: any) => {
const answers: Record<string, string | undefined> = {};
questions.forEach(
(question: { name: string | number; default: any }) => {
answers[question.name] = question.default;
},
);
return answers;
});

const result = await askForTalawaApiUrl();

expect(mockPrompt).toHaveBeenCalledWith([
{
type: 'input',
name: 'endpoint',
message: 'Enter your talawa-api endpoint:',
default: 'http://localhost:4000/graphql/',
},
]);

expect(result).toBe('http://localhost:4000/graphql/');
});
});

0 comments on commit b240fcb

Please sign in to comment.