File tree Expand file tree Collapse file tree 2 files changed +26
-5
lines changed Expand file tree Collapse file tree 2 files changed +26
-5
lines changed Original file line number Diff line number Diff line change @@ -12,12 +12,10 @@ import registry from "../../core/registry";
12
12
const log = logging . getLogger ( "pat.ajax" ) ;
13
13
14
14
export const parser = new Parser ( "ajax" ) ;
15
+ parser . addArgument ( "accept" , "text/html" ) ;
15
16
parser . addArgument ( "url" , function ( $el ) {
16
- return ( $el . is ( "a" )
17
- ? $el . attr ( "href" )
18
- : $el . is ( "form" )
19
- ? $el . attr ( "action" )
20
- : ""
17
+ return (
18
+ $el . is ( "a" ) ? $el . attr ( "href" ) : $el . is ( "form" ) ? $el . attr ( "action" ) : ""
21
19
) . split ( "#" ) [ 0 ] ;
22
20
} ) ;
23
21
@@ -104,10 +102,15 @@ var _ = {
104
102
args = {
105
103
context : $el ,
106
104
data : [ $el . serialize ( ) , clickedData ] . filter ( Boolean ) . join ( "&" ) ,
105
+ headers : { } ,
107
106
url : cfg . url ,
108
107
method : $el . attr ( "method" ) ? $el . attr ( "method" ) : "GET" ,
109
108
} ;
110
109
110
+ if ( cfg . accept ) {
111
+ args . headers . Accept = cfg . accept ;
112
+ }
113
+
111
114
if (
112
115
$el . is ( "form" ) &&
113
116
$el . attr ( "method" ) &&
Original file line number Diff line number Diff line change @@ -135,5 +135,23 @@ describe("pat-ajax", function () {
135
135
const ajaxargs = $ . ajax . mock . calls [ $ . ajax . mock . calls . length - 1 ] [ 0 ] ;
136
136
expect ( ajaxargs . url ) . toEqual ( "else.html" ) ;
137
137
} ) ;
138
+
139
+ // Accept
140
+ it ( "Default accept header" , function ( ) {
141
+ document . body . innerHTML = `<a class="pat-ajax" />` ;
142
+ registry . scan ( document . body ) ;
143
+ jest . spyOn ( $ , "ajax" ) ;
144
+ document . body . querySelector ( ".pat-ajax" ) . click ( ) ;
145
+ const ajaxargs = $ . ajax . mock . calls [ $ . ajax . mock . calls . length - 1 ] [ 0 ] ;
146
+ expect ( ajaxargs . headers ) . toEqual ( { Accept : "text/html" } ) ;
147
+ } ) ;
148
+ it ( "Can set accept header" , function ( ) {
149
+ document . body . innerHTML = `<a class="pat-ajax" data-pat-ajax="accept: */*"/>` ;
150
+ registry . scan ( document . body ) ;
151
+ jest . spyOn ( $ , "ajax" ) ;
152
+ document . body . querySelector ( ".pat-ajax" ) . click ( ) ;
153
+ const ajaxargs = $ . ajax . mock . calls [ $ . ajax . mock . calls . length - 1 ] [ 0 ] ;
154
+ expect ( ajaxargs . headers ) . toEqual ( { Accept : "*/*" } ) ;
155
+ } ) ;
138
156
} ) ;
139
157
} ) ;
You can’t perform that action at this time.
0 commit comments