Skip to content

Commit 5b30e81

Browse files
test cases added for all prompts
1 parent a97a797 commit 5b30e81

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed

packages/inquirer/inquirer.test.ts

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,173 @@ describe('inquirer.prompt(...)', () => {
794794
expect(answers).toEqual({ q1: 'bar', q3: 'bar' });
795795
writeSpy.mockRestore();
796796
});
797+
798+
it('should display skipped confirm question with default true', async () => {
799+
const writeSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
800+
const answers = await inquirer.prompt([
801+
{ type: 'stub', name: 'q1', message: 'Question 1' },
802+
{
803+
type: 'confirm',
804+
name: 'confirmQ',
805+
message: 'Are you sure?',
806+
default: true,
807+
when() {
808+
return { ask: false, display: true };
809+
},
810+
},
811+
]);
812+
813+
const output = writeSpy.mock.calls.map(([text]) => text).join('');
814+
expect(output).toMatch(/Are you sure\?.*Yes/);
815+
expect(output.includes('\x1b[2m')).toBe(true);
816+
expect(answers).toEqual({ q1: 'bar' });
817+
writeSpy.mockRestore();
818+
});
819+
820+
it('should display skipped select question', async () => {
821+
const writeSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
822+
const answers = await inquirer.prompt([
823+
{ type: 'stub', name: 'q1', message: 'Question 1' },
824+
{
825+
type: 'select',
826+
name: 'fruit',
827+
message: 'Select fruit',
828+
choices: [
829+
{ name: 'Apple', value: 'a' },
830+
{ name: 'Banana', value: 'b' },
831+
],
832+
default: 'b',
833+
when() {
834+
return { ask: false, display: true };
835+
},
836+
},
837+
]);
838+
839+
const output = writeSpy.mock.calls.map(([text]) => text).join('');
840+
expect(output).toMatch(/Select fruit.*Banana/);
841+
expect(output.includes('\x1b[2m')).toBe(true);
842+
expect(answers).toEqual({ q1: 'bar' });
843+
writeSpy.mockRestore();
844+
});
845+
846+
it('should display skipped checkbox question', async () => {
847+
const writeSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
848+
849+
const answers = await inquirer.prompt([
850+
{ type: 'stub', name: 'q1', message: 'Question 1' },
851+
{
852+
type: 'checkbox',
853+
name: 'foods',
854+
message: 'Pick foods',
855+
choices: [
856+
{ name: 'Pizza', value: 'p' },
857+
{ name: 'Burger', value: 'b' },
858+
],
859+
default: ['p', 'b'],
860+
when() {
861+
return { ask: false, display: true };
862+
},
863+
},
864+
]);
865+
866+
const output = writeSpy.mock.calls.map(([text]) => text).join('');
867+
expect(output).toMatch(/Pick foods.*Pizza, Burger/);
868+
expect(output.includes('\x1b[2m')).toBe(true);
869+
expect(answers).toEqual({ q1: 'bar' });
870+
writeSpy.mockRestore();
871+
});
872+
873+
it('should display skipped password question', async () => {
874+
const writeSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
875+
876+
const answers = await inquirer.prompt([
877+
{ type: 'stub', name: 'q1', message: 'Question 1' },
878+
{
879+
type: 'password',
880+
name: 'pwd',
881+
message: 'Enter password',
882+
default: 'secret',
883+
when() {
884+
return { ask: false, display: true };
885+
},
886+
},
887+
]);
888+
889+
const output = writeSpy.mock.calls.map(([text]) => text).join('');
890+
expect(output).toMatch(/Enter password.*\[PASSWORD SET\]/);
891+
expect(output.includes('\x1b[2m')).toBe(true);
892+
expect(answers).toEqual({ q1: 'bar' });
893+
writeSpy.mockRestore();
894+
});
895+
896+
it('should display skipped editor question', async () => {
897+
const writeSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
898+
899+
const answers = await inquirer.prompt([
900+
{ type: 'stub', name: 'q1', message: 'Question 1' },
901+
{
902+
type: 'editor',
903+
name: 'ed',
904+
message: 'Write content',
905+
default: 'notes...',
906+
when() {
907+
return { ask: false, display: true };
908+
},
909+
},
910+
]);
911+
912+
const output = writeSpy.mock.calls.map(([text]) => text).join('');
913+
expect(output).toMatch(/Write content.*\[Default Content\]/);
914+
expect(output.includes('\x1b[2m')).toBe(true);
915+
expect(answers).toEqual({ q1: 'bar' });
916+
writeSpy.mockRestore();
917+
});
918+
919+
it('should display skipped input question', async () => {
920+
const writeSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
921+
922+
const answers = await inquirer.prompt([
923+
{ type: 'stub', name: 'q1', message: 'Question 1' },
924+
{
925+
type: 'input',
926+
name: 'name',
927+
message: 'Enter name',
928+
default: 'John',
929+
when() {
930+
return { ask: false, display: true };
931+
},
932+
},
933+
]);
934+
935+
const output = writeSpy.mock.calls.map(([text]) => text).join('');
936+
expect(output).toMatch(/Enter name.*John/);
937+
expect(output.includes('\x1b[2m')).toBe(true);
938+
expect(answers).toEqual({ q1: 'bar' });
939+
writeSpy.mockRestore();
940+
});
941+
942+
it('should display skipped number question', async () => {
943+
const writeSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
944+
945+
const answers = await inquirer.prompt([
946+
{ type: 'stub', name: 'q1', message: 'Question 1' },
947+
{
948+
type: 'number',
949+
name: 'age',
950+
message: 'Enter age',
951+
default: 25,
952+
when() {
953+
return { ask: false, display: true };
954+
},
955+
},
956+
]);
957+
958+
const output = writeSpy.mock.calls.map(([text]) => text).join('');
959+
expect(output).toMatch(/Enter age.*25/);
960+
expect(output.includes('\x1b[2m')).toBe(true);
961+
expect(answers).toEqual({ q1: 'bar' });
962+
writeSpy.mockRestore();
963+
});
797964
});
798965

799966
describe('Prefilling answers', () => {

0 commit comments

Comments
 (0)