@@ -22,14 +22,49 @@ beforeEach(() => {
2222
2323test ( "function returns random chars" , async ( ) => {
2424 // Mock the input values
25- mockedGetInput . mockReturnValueOnce ( "50" ) ;
25+ const numOfChars = "100" ;
26+ mockedGetInput . mockReturnValueOnce ( numOfChars ) ;
27+ mockedGetInput . mockReturnValueOnce ( "0x0020" ) ;
28+ mockedGetInput . mockReturnValueOnce ( "0x007e" ) ;
2629
2730 // Run the `run` function
2831 await generateRandomCharacters ( ) ;
2932
3033 // Assertions
31- expect ( mockedGetInput ) . toHaveBeenCalledTimes ( 1 ) ;
32- expect ( mockedGetInput ) . toHaveBeenCalledWith ( "input1" ) ;
34+ expect ( mockedGetInput ) . toHaveBeenCalledTimes ( 3 ) ;
35+ expect ( mockedGetInput ) . toHaveBeenCalledWith ( "numOfChars" ) ;
36+ expect ( mockedGetInput ) . toHaveBeenCalledWith ( "startRange" ) ;
37+ expect ( mockedGetInput ) . toHaveBeenCalledWith ( "endRange" ) ;
3338 expect ( mockedSetOutput ) . toHaveBeenCalledTimes ( 1 ) ;
34- expect ( mockedSetOutput ) . toHaveBeenCalledWith ( "output1" , expect . any ( String ) ) ;
39+ expect ( mockedSetOutput ) . toHaveBeenCalledWith ( "output" , expect . any ( String ) ) ;
40+ const lengthRegex = new RegExp ( `^.{${ numOfChars } }$` ) ;
41+ expect ( mockedSetOutput ) . toHaveBeenCalledWith ( "output" , expect . stringMatching ( lengthRegex ) ) ;
42+ } ) ;
43+
44+ test ( "should handle numOfChars = 0" , async ( ) => {
45+ mockedGetInput . mockReturnValueOnce ( "0" ) ;
46+ await generateRandomCharacters ( ) ;
47+ expect ( mockedSetOutput ) . toHaveBeenCalledWith ( "output" , "" ) ;
48+ } ) ;
49+
50+ it ( "should handle a negative numOfChars" , async ( ) => {
51+ mockedGetInput . mockReturnValueOnce ( "-10" ) ;
52+ // Expect an error to be thrown, or handle it appropriately in your code.
53+ await expect ( generateRandomCharacters ( ) ) . rejects . toThrowError ( "numOfChars must be a positive integer" ) ;
54+ } ) ;
55+
56+ it ( "should handle invalid hexadecimal values" , async ( ) => {
57+ mockedGetInput . mockReturnValueOnce ( "100" ) ;
58+ mockedGetInput . mockReturnValueOnce ( "invalidStart" ) ;
59+ mockedGetInput . mockReturnValueOnce ( "invalidEnd" ) ;
60+ // Expect an error to be thrown
61+ await expect ( generateRandomCharacters ( ) ) . rejects . toThrowError ( "Invalid code point NaN" ) ;
62+ } ) ;
63+
64+ it ( "should handle startRange > endRange" , async ( ) => {
65+ mockedGetInput . mockReturnValueOnce ( "100" ) ;
66+ mockedGetInput . mockReturnValueOnce ( "0x007e" ) ; // End range is smaller
67+ mockedGetInput . mockReturnValueOnce ( "0x0020" ) ; // Start range is greater
68+ // Expect an error to be thrown
69+ await expect ( generateRandomCharacters ( ) ) . rejects . toThrowError ( "startRange must be less than or equal to endRange" ) ;
3570} ) ;
0 commit comments