1
+ import registry from "../../core/registry" ;
1
2
import pattern from "./ajax" ;
2
3
import $ from "jquery" ;
3
4
import { jest } from "@jest/globals" ;
4
5
5
6
var $lab ;
6
7
7
8
describe ( "pat-ajax" , function ( ) {
8
- beforeEach ( function ( ) {
9
- $lab = $ ( "<div/>" , { id : "lab" } ) . appendTo ( document . body ) ;
10
- } ) ;
9
+ describe ( "anchor" , function ( ) {
10
+ beforeEach ( function ( ) {
11
+ $lab = $ ( "<div/>" , { id : "lab" } ) . appendTo ( document . body ) ;
12
+ } ) ;
11
13
12
- afterEach ( function ( ) {
13
- $ ( "#lab" ) . remove ( ) ;
14
- } ) ;
14
+ afterEach ( function ( ) {
15
+ document . body . innerHTML = "" ;
16
+ jest . restoreAllMocks ( ) ;
17
+ } ) ;
15
18
16
- describe ( "anchor" , function ( ) {
17
19
it ( "triggers ajax request on click" , function ( ) {
18
20
var $a = $ ( "<a href='href.html' />" ) . appendTo ( $lab ) ;
19
21
pattern . init ( $a ) ;
@@ -33,6 +35,7 @@ describe("pat-ajax", function () {
33
35
var $form , $button , spy_ajax ;
34
36
35
37
beforeEach ( function ( ) {
38
+ $lab = $ ( "<div/>" , { id : "lab" } ) . appendTo ( document . body ) ;
36
39
$form = $ ( "<form action='action.html'></form>" ) . appendTo ( $lab ) ;
37
40
$button = $ (
38
41
"<button type='submit' name='submit' value='submit' />"
@@ -42,6 +45,11 @@ describe("pat-ajax", function () {
42
45
spy_ajax = jest . spyOn ( $ , "ajax" ) ;
43
46
} ) ;
44
47
48
+ afterEach ( function ( ) {
49
+ document . body . innerHTML = "" ;
50
+ jest . restoreAllMocks ( ) ;
51
+ } ) ;
52
+
45
53
it ( "triggers ajax request on submit" , function ( ) {
46
54
$form . submit ( ) ;
47
55
expect ( spy_ajax ) . toHaveBeenCalled ( ) ;
@@ -75,4 +83,57 @@ describe("pat-ajax", function () {
75
83
expect ( ajaxargs . data ) . toEqual ( "input1=value1" ) ;
76
84
} ) ;
77
85
} ) ;
86
+
87
+ describe ( "Arguments can be set" , function ( ) {
88
+ afterEach ( function ( ) {
89
+ document . body . innerHTML = "" ;
90
+ jest . restoreAllMocks ( ) ;
91
+ } ) ;
92
+
93
+ // URL
94
+ it ( "Gets URL from anchor-href" , function ( ) {
95
+ document . body . innerHTML = `<a class="pat-ajax" href="somewhere.html"/>` ;
96
+ registry . scan ( document . body ) ;
97
+ jest . spyOn ( $ , "ajax" ) ;
98
+ document . body . querySelector ( ".pat-ajax" ) . click ( ) ;
99
+ const ajaxargs = $ . ajax . mock . calls [ $ . ajax . mock . calls . length - 1 ] [ 0 ] ;
100
+ expect ( ajaxargs . url ) . toEqual ( "somewhere.html" ) ;
101
+ } ) ;
102
+ it ( "Gets URL from form-action" , function ( ) {
103
+ document . body . innerHTML = `<form class="pat-ajax" action="somewhere.html"/>` ;
104
+ registry . scan ( document . body ) ;
105
+ jest . spyOn ( $ , "ajax" ) ;
106
+ $ ( ".pat-ajax" ) . submit ( ) ;
107
+ const ajaxargs = $ . ajax . mock . calls [ $ . ajax . mock . calls . length - 1 ] [ 0 ] ;
108
+ expect ( ajaxargs . url ) . toEqual ( "somewhere.html" ) ;
109
+ } ) ;
110
+ it ( "Can explicitly set URL on anchor" , function ( ) {
111
+ document . body . innerHTML = `
112
+ <a
113
+ class="pat-ajax"
114
+ href="somewhere.html"
115
+ data-pat-ajax="url: else.html"
116
+ />
117
+ ` ;
118
+ registry . scan ( document . body ) ;
119
+ jest . spyOn ( $ , "ajax" ) ;
120
+ document . body . querySelector ( ".pat-ajax" ) . click ( ) ;
121
+ const ajaxargs = $ . ajax . mock . calls [ $ . ajax . mock . calls . length - 1 ] [ 0 ] ;
122
+ expect ( ajaxargs . url ) . toEqual ( "else.html" ) ;
123
+ } ) ;
124
+ it ( "Can explicitly set URL on form" , function ( ) {
125
+ document . body . innerHTML = `
126
+ <form
127
+ class="pat-ajax"
128
+ action="somewhere.html"
129
+ data-pat-ajax="url: else.html"
130
+ />
131
+ ` ;
132
+ registry . scan ( document . body ) ;
133
+ jest . spyOn ( $ , "ajax" ) ;
134
+ $ ( ".pat-ajax" ) . submit ( ) ;
135
+ const ajaxargs = $ . ajax . mock . calls [ $ . ajax . mock . calls . length - 1 ] [ 0 ] ;
136
+ expect ( ajaxargs . url ) . toEqual ( "else.html" ) ;
137
+ } ) ;
138
+ } ) ;
78
139
} ) ;
0 commit comments