File tree 2 files changed +59
-0
lines changed
2 files changed +59
-0
lines changed Original file line number Diff line number Diff line change @@ -48,6 +48,40 @@ async function validateText(
48
48
options : ValidateTextOptionsWithWarnings
49
49
) : Promise < ValidateTextResultWithWarnings > ;
50
50
async function validateText ( textToBeValidated : string , options ?: ValidateTextOptions ) : Promise < ValidateTextResult > {
51
+ // Validations
52
+ if ( ! textToBeValidated ) {
53
+ throw new Error ( 'You must pass in text to be validated' ) ;
54
+ }
55
+
56
+ if ( typeof textToBeValidated !== 'string' ) {
57
+ throw new Error ( 'The text to be validated must be a string' ) ;
58
+ }
59
+
60
+ if ( options ) {
61
+ const allowedMediums : typeof options [ 'medium' ] [ ] = [
62
+ 'all' ,
63
+ 'braille' ,
64
+ 'embossed' ,
65
+ 'handheld' ,
66
+ 'print' ,
67
+ 'projection' ,
68
+ 'screen' ,
69
+ 'speech' ,
70
+ 'tty' ,
71
+ 'tv' ,
72
+ ] ;
73
+
74
+ if ( options . medium && ! allowedMediums . includes ( options . medium ) ) {
75
+ throw new Error ( `The medium must be one of the following: ${ allowedMediums . join ( ', ' ) } ` ) ;
76
+ }
77
+
78
+ const allowedWarningLevels : typeof options [ 'warningLevel' ] [ ] = [ 0 , 1 , 2 , 3 ] ;
79
+
80
+ if ( options . warningLevel && ! allowedWarningLevels . includes ( options . warningLevel ) ) {
81
+ throw new Error ( `The warning level must be one of the following: ${ allowedWarningLevels . join ( ', ' ) } ` ) ;
82
+ }
83
+ }
84
+
51
85
// Build URL for fetching
52
86
const params = {
53
87
text : encodeURIComponent ( textToBeValidated ) ,
Original file line number Diff line number Diff line change @@ -61,4 +61,29 @@ describe('#validateText()', () => {
61
61
errors : [ ] ,
62
62
} ) ;
63
63
} ) ;
64
+
65
+ it ( 'Complains about missing text' , async ( ) => {
66
+ // @ts -expect-error: We're trying to force an error here
67
+ await expect ( cssValidator . validateText ( ) ) . rejects . toThrow ( 'You must pass in text to be validated' ) ;
68
+ await expect ( cssValidator . validateText ( '' ) ) . rejects . toThrow ( 'You must pass in text to be validated' ) ;
69
+ } ) ;
70
+
71
+ it ( 'Complains about text not being a string' , async ( ) => {
72
+ // @ts -expect-error: We're trying to force an error here
73
+ await expect ( cssValidator . validateText ( true ) ) . rejects . toThrow ( 'The text to be validated must be a string' ) ;
74
+ } ) ;
75
+
76
+ it ( 'Complains about invalid medium' , async ( ) => {
77
+ // @ts -expect-error: We're trying to force an error here
78
+ await expect ( cssValidator . validateText ( 'abc' , { medium : 'fake' } ) ) . rejects . toThrow (
79
+ 'The medium must be one of the following:'
80
+ ) ;
81
+ } ) ;
82
+
83
+ it ( 'Complains about invalid warning level' , async ( ) => {
84
+ // @ts -expect-error: We're trying to force an error here
85
+ await expect ( cssValidator . validateText ( 'abc' , { warningLevel : 'fake' } ) ) . rejects . toThrow (
86
+ 'The warning level must be one of the following:'
87
+ ) ;
88
+ } ) ;
64
89
} ) ;
You can’t perform that action at this time.
0 commit comments