File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
Roadmap/13 - PRUEBAS UNITARIAS/javascript Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * #13 PRUEBAS UNITARIAS
3
+ */
4
+
5
+ const sum = ( num1 , num2 ) => num1 + num2
6
+
7
+ // test
8
+ const result1 = sum ( 2 , 3 )
9
+ if ( result1 === 5 ) {
10
+ console . log ( 'Test passed' )
11
+ } else {
12
+ console . error ( 'Error: The sum of 2 and 3 is 5' )
13
+ }
14
+
15
+ /*
16
+ * DIFICULTAD EXTRA
17
+ */
18
+
19
+ const person = {
20
+ name : 'cesar-ch' ,
21
+ age : 3 ,
22
+ birthDate : '2021-02-03' ,
23
+ programmingLanguages : [ "Javascript" , "Python" , "Java" ]
24
+ }
25
+
26
+ // test for existence of fields
27
+ const expectedKeys = [ "name" , "age" , "birthDate" , "programmingLanguages" ]
28
+ const allKeysExist = expectedKeys . every ( key => key in person )
29
+ if ( allKeysExist ) {
30
+ console . log ( 'Test passed' )
31
+ } else {
32
+ console . error ( 'Error: The person object has the expected keys' )
33
+ }
34
+
35
+ // test to verify field data
36
+ if ( typeof person . name !== "string" ) {
37
+ console . error ( 'Error: The name field is not string' )
38
+ }
39
+
40
+ if ( isNaN ( person . age ) || person . age < 0 ) {
41
+ console . error ( 'Error: Age must be a positive number' )
42
+ }
43
+
44
+ const regexDate = / ^ \d { 4 } - \d { 2 } - \d { 2 } $ /
45
+
46
+ if ( ! regexDate . test ( person . birthDate ) ) {
47
+ console . error ( 'Error: Birth date is not in the expected format' )
48
+ }
49
+
50
+ if ( ! Array . isArray ( person . programmingLanguages ) ) {
51
+ console . error ( 'Error: Programming languages is not an array' )
52
+ }
You can’t perform that action at this time.
0 commit comments