@@ -64,7 +64,7 @@ describe("Validatinator Utils", function() {
6464 expect ( utils . isValueAnArray ( "not an array" ) ) . toBeFalsy ( ) ;
6565 } ) ;
6666
67- describe ( 'Set Test Form For Value Retrieval ' , function ( ) {
67+ describe ( 'getFieldsValue ' , function ( ) {
6868 beforeEach ( function ( ) {
6969 // Creating our testing form.
7070 var myForm = document . createElement ( 'form' ) ,
@@ -76,16 +76,19 @@ describe("Validatinator Utils", function() {
7676 myForm . name = "my-form" ;
7777 firstName . name = "first-name" ;
7878 lastName . name = "last-name" ;
79+
7980 gender . name = "gender" ;
8081 gender . type = "radio" ;
8182 gender . value = "male" ;
83+
8284 languages . name = "languages" ;
8385 languages . type = "checkbox" ;
8486 languages . value = "english" ;
8587
8688 lastName . value = "test" ;
8789
8890 document . body . appendChild ( myForm ) ;
91+
8992 // Now that our element is in the dom let's select it again.
9093 myForm = document . getElementsByName ( "my-form" ) [ 0 ] ;
9194
@@ -96,21 +99,43 @@ describe("Validatinator Utils", function() {
9699 myForm . appendChild ( languages ) ;
97100 } ) ;
98101
99- it ( 'getFieldsValue should throw an error if there is no field to grab a value from, else return the field\'s value.' , function ( ) {
102+ // Do proper cleanup after each of these tests.
103+ afterEach ( function ( ) {
104+ document . body . removeChild ( document . querySelector ( '[name="my-form"]' ) ) ;
105+ } ) ;
106+
107+ it ( 'should throw an error if the field doesn\'t exist.' , function ( ) {
100108 expect ( utils . getFieldsValue ( "my-form" , "first-name" ) ) . toEqual ( "" ) ;
101109 expect ( utils . getFieldsValue ( "my-form" , "last-name" ) ) . toEqual ( "test" ) ;
102110
103- expect ( function ( ) { utils . getFieldsValue ( "my-form" , "some-fake-field" ) ; } ) . toThrow ( "Couldn't find the field element some-fake-field for the form my-form." ) ;
111+ expect ( function ( ) {
112+ utils . getFieldsValue ( "my-form" , "some-fake-field" ) ;
113+ } ) . toThrow ( ) ;
104114 } ) ;
105115
106- it ( 'getFieldsValue should only return the value from radio or checkbox if its actually selected/checked ' , function ( ) {
116+ it ( 'should only return a value for radio or checkbox if its actually selected. ' , function ( ) {
107117 expect ( utils . getFieldsValue ( "my-form" , "gender" ) ) . toEqual ( "" ) ;
108118 expect ( utils . getFieldsValue ( "my-form" , "languages" ) ) . toEqual ( "" ) ;
109119
110120 document . getElementsByName ( "gender" ) [ 0 ] . checked = true ;
111121 document . getElementsByName ( "languages" ) [ 0 ] . checked = true ;
122+
112123 expect ( utils . getFieldsValue ( "my-form" , "gender" ) ) . toEqual ( "male" ) ;
113124 expect ( utils . getFieldsValue ( "my-form" , "languages" ) ) . toEqual ( "english" ) ;
114125 } ) ;
126+
127+ it ( 'should only evaluate field DOM objects that are within our form' , function ( ) {
128+ // Test specific setup
129+ var fieldName = "first-name" ;
130+
131+ var meta = document . createElement ( 'meta' ) ;
132+ meta . name = fieldName ;
133+ meta . value = "David" ;
134+
135+ var head = document . querySelector ( "head" ) ;
136+ head . appendChild ( meta ) ;
137+
138+ expect ( utils . getFieldsValue ( "my-form" , fieldName ) ) . toEqual ( "" ) ;
139+ } ) ;
115140 } ) ;
116- } ) ;
141+ } ) ;
0 commit comments