@@ -103,7 +103,7 @@ describe("InsertAutofillContentService", () => {
103103 delay_between_operations : 20 ,
104104 } ,
105105 metadata : { } ,
106- autosubmit : null ,
106+ autosubmit : [ ] ,
107107 savedUrls : [ "https://bitwarden.com" ] ,
108108 untrustedIframe : false ,
109109 itemType : "login" ,
@@ -218,28 +218,21 @@ describe("InsertAutofillContentService", () => {
218218
219219 await insertAutofillContentService . fillForm ( fillScript ) ;
220220
221- expect ( insertAutofillContentService [ "userCancelledInsecureUrlAutofill" ] ) . toHaveBeenCalled ( ) ;
222- expect (
223- insertAutofillContentService [ "userCancelledUntrustedIframeAutofill" ] ,
224- ) . toHaveBeenCalled ( ) ;
225221 expect ( insertAutofillContentService [ "runFillScriptAction" ] ) . toHaveBeenCalledTimes ( 3 ) ;
226222 expect ( insertAutofillContentService [ "runFillScriptAction" ] ) . toHaveBeenNthCalledWith (
227223 1 ,
228224 fillScript . script [ 0 ] ,
229225 0 ,
230- fillScript . script ,
231226 ) ;
232227 expect ( insertAutofillContentService [ "runFillScriptAction" ] ) . toHaveBeenNthCalledWith (
233228 2 ,
234229 fillScript . script [ 1 ] ,
235230 1 ,
236- fillScript . script ,
237231 ) ;
238232 expect ( insertAutofillContentService [ "runFillScriptAction" ] ) . toHaveBeenNthCalledWith (
239233 3 ,
240234 fillScript . script [ 2 ] ,
241235 2 ,
242- fillScript . script ,
243236 ) ;
244237 } ) ;
245238 } ) ;
@@ -623,7 +616,30 @@ describe("InsertAutofillContentService", () => {
623616 } ) ;
624617 } ) ;
625618
626- it ( "will set the `value` attribute of any passed input or textarea elements" , ( ) => {
619+ it ( "will set the `value` attribute of any passed input or textarea elements if the value differs" , ( ) => {
620+ document . body . innerHTML = `<input type="text" id="username" value="old" /><textarea id="bio">old</textarea>` ;
621+ const value1 = "test" ;
622+ const value2 = "test2" ;
623+ const textInputElement = document . getElementById ( "username" ) as HTMLInputElement ;
624+ const textareaElement = document . getElementById ( "bio" ) as HTMLTextAreaElement ;
625+ jest . spyOn ( insertAutofillContentService as any , "handleInsertValueAndTriggerSimulatedEvents" ) ;
626+
627+ insertAutofillContentService [ "insertValueIntoField" ] ( textInputElement , value1 ) ;
628+
629+ expect ( textInputElement . value ) . toBe ( value1 ) ;
630+ expect (
631+ insertAutofillContentService [ "handleInsertValueAndTriggerSimulatedEvents" ] ,
632+ ) . toHaveBeenCalledWith ( textInputElement , expect . any ( Function ) ) ;
633+
634+ insertAutofillContentService [ "insertValueIntoField" ] ( textareaElement , value2 ) ;
635+
636+ expect ( textareaElement . value ) . toBe ( value2 ) ;
637+ expect (
638+ insertAutofillContentService [ "handleInsertValueAndTriggerSimulatedEvents" ] ,
639+ ) . toHaveBeenCalledWith ( textareaElement , expect . any ( Function ) ) ;
640+ } ) ;
641+
642+ it ( "will NOT set the `value` attribute of any passed input or textarea elements if they already have values matching the passed value" , ( ) => {
627643 document . body . innerHTML = `<input type="text" id="username" /><textarea id="bio"></textarea>` ;
628644 const value1 = "test" ;
629645 const value2 = "test2" ;
@@ -638,14 +654,28 @@ describe("InsertAutofillContentService", () => {
638654 expect ( textInputElement . value ) . toBe ( value1 ) ;
639655 expect (
640656 insertAutofillContentService [ "handleInsertValueAndTriggerSimulatedEvents" ] ,
641- ) . toHaveBeenCalledWith ( textInputElement , expect . any ( Function ) ) ;
657+ ) . not . toHaveBeenCalled ( ) ;
642658
643659 insertAutofillContentService [ "insertValueIntoField" ] ( textareaElement , value2 ) ;
644660
645661 expect ( textareaElement . value ) . toBe ( value2 ) ;
646662 expect (
647663 insertAutofillContentService [ "handleInsertValueAndTriggerSimulatedEvents" ] ,
648- ) . toHaveBeenCalledWith ( textareaElement , expect . any ( Function ) ) ;
664+ ) . not . toHaveBeenCalled ( ) ;
665+ } ) ;
666+
667+ it ( "skips filling when the field already has the target value" , ( ) => {
668+ const value = "test" ;
669+ document . body . innerHTML = `<input type="text" id="username" value="${ value } "/>` ;
670+ const element = document . getElementById ( "username" ) as FillableFormFieldElement ;
671+ jest . spyOn ( insertAutofillContentService as any , "handleInsertValueAndTriggerSimulatedEvents" ) ;
672+
673+ insertAutofillContentService [ "insertValueIntoField" ] ( element , value ) ;
674+
675+ expect (
676+ insertAutofillContentService [ "handleInsertValueAndTriggerSimulatedEvents" ] ,
677+ ) . not . toHaveBeenCalled ( ) ;
678+ expect ( element . value ) . toBe ( value ) ;
649679 } ) ;
650680 } ) ;
651681
0 commit comments