Skip to content

Commit 85c885d

Browse files
authored
test: add tests for password prompt (#274)
1 parent 7cd4f76 commit 85c885d

File tree

2 files changed

+370
-0
lines changed

2 files changed

+370
-0
lines changed

packages/prompts/src/__snapshots__/index.test.ts.snap

Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,146 @@ exports[`prompts (isCI = false) > multiselect > sliding window loops upwards 1`]
708708
]
709709
`;
710710

711+
exports[`prompts (isCI = false) > password > renders and clears validation errors 1`] = `
712+
[
713+
"[?25l",
714+
"│
715+
◆ foo
716+
│ _
717+
└
718+
",
719+
"",
720+
"",
721+
"",
722+
"│ ▪_",
723+
"",
724+
"",
725+
"",
726+
"",
727+
"▲ foo
728+
│ ▪
729+
└ Password must be at least 2 characters
730+
",
731+
"",
732+
"",
733+
"",
734+
"◆ foo
735+
│ ▪▪_
736+
└
737+
",
738+
"",
739+
"",
740+
"",
741+
"◇ foo
742+
│ ▪▪",
743+
"
744+
",
745+
"[?25h",
746+
]
747+
`;
748+
749+
exports[`prompts (isCI = false) > password > renders cancelled value 1`] = `
750+
[
751+
"[?25l",
752+
"│
753+
◆ foo
754+
│ _
755+
└
756+
",
757+
"",
758+
"",
759+
"",
760+
"│ ▪_",
761+
"",
762+
"",
763+
"",
764+
"",
765+
"■ foo
766+
│ ▪
767+
│",
768+
"
769+
",
770+
"[?25h",
771+
]
772+
`;
773+
774+
exports[`prompts (isCI = false) > password > renders custom mask 1`] = `
775+
[
776+
"[?25l",
777+
"│
778+
◆ foo
779+
│ _
780+
└
781+
",
782+
"",
783+
"",
784+
"",
785+
"│ *_",
786+
"",
787+
"",
788+
"",
789+
"",
790+
"│ **_",
791+
"",
792+
"",
793+
"",
794+
"",
795+
"◇ foo
796+
│ **",
797+
"
798+
",
799+
"[?25h",
800+
]
801+
`;
802+
803+
exports[`prompts (isCI = false) > password > renders masked value 1`] = `
804+
[
805+
"[?25l",
806+
"│
807+
◆ foo
808+
│ _
809+
└
810+
",
811+
"",
812+
"",
813+
"",
814+
"│ ▪_",
815+
"",
816+
"",
817+
"",
818+
"",
819+
"│ ▪▪_",
820+
"",
821+
"",
822+
"",
823+
"",
824+
"◇ foo
825+
│ ▪▪",
826+
"
827+
",
828+
"[?25h",
829+
]
830+
`;
831+
832+
exports[`prompts (isCI = false) > password > renders message 1`] = `
833+
[
834+
"[?25l",
835+
"│
836+
◆ foo
837+
│ _
838+
└
839+
",
840+
"",
841+
"",
842+
"",
843+
"◇ foo
844+
│",
845+
"
846+
",
847+
"[?25h",
848+
]
849+
`;
850+
711851
exports[`prompts (isCI = false) > select > can cancel 1`] = `
712852
[
713853
"[?25l",
@@ -1873,6 +2013,146 @@ exports[`prompts (isCI = true) > multiselect > sliding window loops upwards 1`]
18732013
]
18742014
`;
18752015

2016+
exports[`prompts (isCI = true) > password > renders and clears validation errors 1`] = `
2017+
[
2018+
"[?25l",
2019+
"│
2020+
◆ foo
2021+
│ _
2022+
└
2023+
",
2024+
"",
2025+
"",
2026+
"",
2027+
"│ ▪_",
2028+
"",
2029+
"",
2030+
"",
2031+
"",
2032+
"▲ foo
2033+
│ ▪
2034+
└ Password must be at least 2 characters
2035+
",
2036+
"",
2037+
"",
2038+
"",
2039+
"◆ foo
2040+
│ ▪▪_
2041+
└
2042+
",
2043+
"",
2044+
"",
2045+
"",
2046+
"◇ foo
2047+
│ ▪▪",
2048+
"
2049+
",
2050+
"[?25h",
2051+
]
2052+
`;
2053+
2054+
exports[`prompts (isCI = true) > password > renders cancelled value 1`] = `
2055+
[
2056+
"[?25l",
2057+
"│
2058+
◆ foo
2059+
│ _
2060+
└
2061+
",
2062+
"",
2063+
"",
2064+
"",
2065+
"│ ▪_",
2066+
"",
2067+
"",
2068+
"",
2069+
"",
2070+
"■ foo
2071+
│ ▪
2072+
│",
2073+
"
2074+
",
2075+
"[?25h",
2076+
]
2077+
`;
2078+
2079+
exports[`prompts (isCI = true) > password > renders custom mask 1`] = `
2080+
[
2081+
"[?25l",
2082+
"│
2083+
◆ foo
2084+
│ _
2085+
└
2086+
",
2087+
"",
2088+
"",
2089+
"",
2090+
"│ *_",
2091+
"",
2092+
"",
2093+
"",
2094+
"",
2095+
"│ **_",
2096+
"",
2097+
"",
2098+
"",
2099+
"",
2100+
"◇ foo
2101+
│ **",
2102+
"
2103+
",
2104+
"[?25h",
2105+
]
2106+
`;
2107+
2108+
exports[`prompts (isCI = true) > password > renders masked value 1`] = `
2109+
[
2110+
"[?25l",
2111+
"│
2112+
◆ foo
2113+
│ _
2114+
└
2115+
",
2116+
"",
2117+
"",
2118+
"",
2119+
"│ ▪_",
2120+
"",
2121+
"",
2122+
"",
2123+
"",
2124+
"│ ▪▪_",
2125+
"",
2126+
"",
2127+
"",
2128+
"",
2129+
"◇ foo
2130+
│ ▪▪",
2131+
"
2132+
",
2133+
"[?25h",
2134+
]
2135+
`;
2136+
2137+
exports[`prompts (isCI = true) > password > renders message 1`] = `
2138+
[
2139+
"[?25l",
2140+
"│
2141+
◆ foo
2142+
│ _
2143+
└
2144+
",
2145+
"",
2146+
"",
2147+
"",
2148+
"◇ foo
2149+
│",
2150+
"
2151+
",
2152+
"[?25h",
2153+
]
2154+
`;
2155+
18762156
exports[`prompts (isCI = true) > select > can cancel 1`] = `
18772157
[
18782158
"[?25l",

packages/prompts/src/index.test.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,4 +809,94 @@ describe.each(['true', 'false'])('prompts (isCI = %s)', (isCI) => {
809809
expect(output.buffer).toMatchSnapshot();
810810
});
811811
});
812+
813+
describe('password', () => {
814+
test('renders message', async () => {
815+
const result = prompts.password({
816+
message: 'foo',
817+
input,
818+
output,
819+
});
820+
821+
input.emit('keypress', '', { name: 'return' });
822+
823+
await result;
824+
825+
expect(output.buffer).toMatchSnapshot();
826+
});
827+
828+
test('renders masked value', async () => {
829+
const result = prompts.password({
830+
message: 'foo',
831+
input,
832+
output,
833+
});
834+
835+
input.emit('keypress', 'x', { name: 'x' });
836+
input.emit('keypress', 'y', { name: 'y' });
837+
input.emit('keypress', '', { name: 'return' });
838+
839+
const value = await result;
840+
841+
expect(value).toBe('xy');
842+
expect(output.buffer).toMatchSnapshot();
843+
});
844+
845+
test('renders custom mask', async () => {
846+
const result = prompts.password({
847+
message: 'foo',
848+
mask: '*',
849+
input,
850+
output,
851+
});
852+
853+
input.emit('keypress', 'x', { name: 'x' });
854+
input.emit('keypress', 'y', { name: 'y' });
855+
input.emit('keypress', '', { name: 'return' });
856+
857+
await result;
858+
859+
expect(output.buffer).toMatchSnapshot();
860+
});
861+
862+
test('renders and clears validation errors', async () => {
863+
const result = prompts.password({
864+
message: 'foo',
865+
validate: (value) => {
866+
if (value.length < 2) {
867+
return 'Password must be at least 2 characters';
868+
}
869+
870+
return undefined;
871+
},
872+
input,
873+
output,
874+
});
875+
876+
input.emit('keypress', 'x', { name: 'x' });
877+
input.emit('keypress', '', { name: 'return' });
878+
input.emit('keypress', 'y', { name: 'y' });
879+
input.emit('keypress', '', { name: 'return' });
880+
881+
await result;
882+
883+
expect(output.buffer).toMatchSnapshot();
884+
});
885+
886+
test('renders cancelled value', async () => {
887+
const result = prompts.password({
888+
message: 'foo',
889+
input,
890+
output,
891+
});
892+
893+
input.emit('keypress', 'x', { name: 'x' });
894+
input.emit('keypress', '', { name: 'escape' });
895+
896+
const value = await result;
897+
898+
expect(prompts.isCancel(value)).toBe(true);
899+
expect(output.buffer).toMatchSnapshot();
900+
});
901+
});
812902
});

0 commit comments

Comments
 (0)