@@ -4834,6 +4834,7 @@ describe("a router", () => {
4834
4834
await t . navigate ( "/tasks" , {
4835
4835
// @ts -expect-error
4836
4836
formMethod : "head" ,
4837
+ // @ts -expect-error
4837
4838
formData : formData ,
4838
4839
} ) ;
4839
4840
expect ( t . router . state . navigation . state ) . toBe ( "idle" ) ;
@@ -4874,6 +4875,7 @@ describe("a router", () => {
4874
4875
await t . navigate ( "/tasks" , {
4875
4876
// @ts -expect-error
4876
4877
formMethod : "options" ,
4878
+ // @ts -expect-error
4877
4879
formData : formData ,
4878
4880
} ) ;
4879
4881
expect ( t . router . state . navigation . state ) . toBe ( "idle" ) ;
@@ -6006,6 +6008,161 @@ describe("a router", () => {
6006
6008
expect ( ( await req . formData ( ) ) . get ( "file" ) ) . toEqual ( "file.txt" ) ;
6007
6009
} ) ;
6008
6010
6011
+ it ( "serializes payload as application/x-www-form-urlencoded" , async ( ) => {
6012
+ let t = setup ( {
6013
+ routes : [ { id : "root" , path : "/" , action : true } ] ,
6014
+ } ) ;
6015
+
6016
+ let payload = { a : "1" } ;
6017
+ let nav = await t . navigate ( "/" , {
6018
+ formMethod : "post" ,
6019
+ formEncType : "application/x-www-form-urlencoded" ,
6020
+ payload,
6021
+ } ) ;
6022
+ await nav . actions . root . resolve ( "ACTION" ) ;
6023
+
6024
+ expect ( nav . actions . root . stub ) . toHaveBeenCalledWith ( {
6025
+ params : { } ,
6026
+ request : expect . any ( Request ) ,
6027
+ payload,
6028
+ } ) ;
6029
+
6030
+ let request = nav . actions . root . stub . mock . calls [ 0 ] [ 0 ] . request ;
6031
+ expect ( request . method ) . toBe ( "POST" ) ;
6032
+ expect ( request . url ) . toBe ( "http://localhost/" ) ;
6033
+ expect ( request . headers . get ( "Content-Type" ) ) . toBe (
6034
+ "application/x-www-form-urlencoded;charset=UTF-8"
6035
+ ) ;
6036
+ expect ( ( await request . formData ( ) ) . get ( "a" ) ) . toBe ( "1" ) ;
6037
+ } ) ;
6038
+
6039
+ it ( "serializes payload as application/json if specified (object)" , async ( ) => {
6040
+ let t = setup ( {
6041
+ routes : [ { id : "root" , path : "/" , action : true } ] ,
6042
+ } ) ;
6043
+
6044
+ let payload = { a : "1" } ;
6045
+ let nav = await t . navigate ( "/" , {
6046
+ formMethod : "post" ,
6047
+ formEncType : "application/json" ,
6048
+ payload,
6049
+ } ) ;
6050
+ await nav . actions . root . resolve ( "ACTION" ) ;
6051
+
6052
+ expect ( nav . actions . root . stub ) . toHaveBeenCalledWith ( {
6053
+ params : { } ,
6054
+ request : expect . any ( Request ) ,
6055
+ payload,
6056
+ } ) ;
6057
+
6058
+ let request = nav . actions . root . stub . mock . calls [ 0 ] [ 0 ] . request ;
6059
+ expect ( request . method ) . toBe ( "POST" ) ;
6060
+ expect ( request . url ) . toBe ( "http://localhost/" ) ;
6061
+ expect ( request . headers . get ( "Content-Type" ) ) . toBe ( "application/json" ) ;
6062
+ expect ( JSON . parse ( await request . text ( ) ) ) . toEqual ( payload ) ;
6063
+ } ) ;
6064
+
6065
+ it ( "serializes payload as application/json if specified (array)" , async ( ) => {
6066
+ let t = setup ( {
6067
+ routes : [ { id : "root" , path : "/" , action : true } ] ,
6068
+ } ) ;
6069
+
6070
+ let payload = [ 1 , 2 , 3 ] ;
6071
+ let nav = await t . navigate ( "/" , {
6072
+ formMethod : "post" ,
6073
+ formEncType : "application/json" ,
6074
+ payload,
6075
+ } ) ;
6076
+ await nav . actions . root . resolve ( "ACTION" ) ;
6077
+
6078
+ expect ( nav . actions . root . stub ) . toHaveBeenCalledWith ( {
6079
+ params : { } ,
6080
+ request : expect . any ( Request ) ,
6081
+ payload,
6082
+ } ) ;
6083
+
6084
+ let request = nav . actions . root . stub . mock . calls [ 0 ] [ 0 ] . request ;
6085
+ expect ( request . method ) . toBe ( "POST" ) ;
6086
+ expect ( request . url ) . toBe ( "http://localhost/" ) ;
6087
+ expect ( request . headers . get ( "Content-Type" ) ) . toBe ( "application/json" ) ;
6088
+ expect ( JSON . parse ( await request . text ( ) ) ) . toEqual ( payload ) ;
6089
+ } ) ;
6090
+
6091
+ it ( "serializes payload as text/plain if specified" , async ( ) => {
6092
+ let t = setup ( {
6093
+ routes : [ { id : "root" , path : "/" , action : true } ] ,
6094
+ } ) ;
6095
+
6096
+ let payload = "plain text" ;
6097
+ let nav = await t . navigate ( "/" , {
6098
+ formMethod : "post" ,
6099
+ formEncType : "text/plain" ,
6100
+ payload,
6101
+ } ) ;
6102
+ await nav . actions . root . resolve ( "ACTION" ) ;
6103
+
6104
+ expect ( nav . actions . root . stub ) . toHaveBeenCalledWith ( {
6105
+ params : { } ,
6106
+ request : expect . any ( Request ) ,
6107
+ payload,
6108
+ } ) ;
6109
+
6110
+ let request = nav . actions . root . stub . mock . calls [ 0 ] [ 0 ] . request ;
6111
+ expect ( request . method ) . toBe ( "POST" ) ;
6112
+ expect ( request . url ) . toBe ( "http://localhost/" ) ;
6113
+ expect ( request . headers . get ( "Content-Type" ) ) . toBe ( "text/plain" ) ;
6114
+ expect ( await request . text ( ) ) . toEqual ( payload ) ;
6115
+ } ) ;
6116
+
6117
+ it ( "does not serialize payload when encType=undefined" , async ( ) => {
6118
+ let t = setup ( {
6119
+ routes : [ { id : "root" , path : "/" , action : true } ] ,
6120
+ } ) ;
6121
+
6122
+ let payload = { a : "1" } ;
6123
+ let nav = await t . navigate ( "/" , {
6124
+ formMethod : "post" ,
6125
+ payload,
6126
+ } ) ;
6127
+ await nav . actions . root . resolve ( "ACTION" ) ;
6128
+
6129
+ expect ( nav . actions . root . stub ) . toHaveBeenCalledWith ( {
6130
+ params : { } ,
6131
+ request : expect . any ( Request ) ,
6132
+ payload,
6133
+ } ) ;
6134
+
6135
+ let request = nav . actions . root . stub . mock . calls [ 0 ] [ 0 ] . request ;
6136
+ expect ( request . method ) . toBe ( "POST" ) ;
6137
+ expect ( request . body ) . toBe ( null ) ;
6138
+ expect ( request . headers . get ( "Content-Type" ) ) . toBe ( null ) ;
6139
+ } ) ;
6140
+
6141
+ it ( "does not serialize payload when encType=null" , async ( ) => {
6142
+ let t = setup ( {
6143
+ routes : [ { id : "root" , path : "/" , action : true } ] ,
6144
+ } ) ;
6145
+
6146
+ let payload = { a : "1" } ;
6147
+ let nav = await t . navigate ( "/" , {
6148
+ formMethod : "post" ,
6149
+ formEncType : null ,
6150
+ payload,
6151
+ } ) ;
6152
+ await nav . actions . root . resolve ( "ACTION" ) ;
6153
+
6154
+ expect ( nav . actions . root . stub ) . toHaveBeenCalledWith ( {
6155
+ params : { } ,
6156
+ request : expect . any ( Request ) ,
6157
+ payload,
6158
+ } ) ;
6159
+
6160
+ let request = nav . actions . root . stub . mock . calls [ 0 ] [ 0 ] . request ;
6161
+ expect ( request . method ) . toBe ( "POST" ) ;
6162
+ expect ( request . body ) . toBe ( null ) ;
6163
+ expect ( request . headers . get ( "Content-Type" ) ) . toBe ( null ) ;
6164
+ } ) ;
6165
+
6009
6166
it ( "races actions and loaders against abort signals" , async ( ) => {
6010
6167
let loaderDfd = createDeferred ( ) ;
6011
6168
let actionDfd = createDeferred ( ) ;
0 commit comments