forked from PalisadoesFoundation/talawa-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refracted jest/vitest issue -(PalisadoesFoundation#2749)
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/'); | ||
}); | ||
}); |