@@ -36,10 +36,13 @@ const startStimulus = () => {
36
36
} ;
37
37
38
38
describe ( 'SwupController' , ( ) => {
39
- let container ;
39
+ afterEach ( ( ) => {
40
+ clearDOM ( ) ;
41
+ actualSwupOptions = null ;
42
+ } ) ;
40
43
41
- beforeEach ( ( ) => {
42
- container = mountDOM ( `
44
+ it ( 'connect' , async ( ) => {
45
+ const container = mountDOM ( `
43
46
<html>
44
47
<head>
45
48
<title>Symfony UX</title>
@@ -62,14 +65,6 @@ describe('SwupController', () => {
62
65
</body>
63
66
</html>
64
67
` ) ;
65
- } ) ;
66
-
67
- afterEach ( ( ) => {
68
- clearDOM ( ) ;
69
- actualSwupOptions = null ;
70
- } ) ;
71
-
72
- it ( 'connect' , async ( ) => {
73
68
const bodyElement = getByTestId ( container , 'body' ) ;
74
69
expect ( bodyElement ) . not . toHaveClass ( 'connected' ) ;
75
70
@@ -81,4 +76,138 @@ describe('SwupController', () => {
81
76
expect ( actualSwupOptions . cache ) . toBe ( true ) ;
82
77
expect ( actualSwupOptions . animateHistoryBrowsing ) . toBe ( true ) ;
83
78
} ) ;
79
+
80
+ it ( 'neither main element nor containers provided' , async ( ) => {
81
+ const container = mountDOM ( `
82
+ <html>
83
+ <head>
84
+ <title>Symfony UX</title>
85
+ </head>
86
+ <body>
87
+ <div
88
+ data-testid="body"
89
+ data-controller="check swup"
90
+ data-swup-link-selector-value="a"
91
+ data-swup-animation-selector-value="[transition-*]"
92
+ data-swup-debug-value="data-debug"
93
+ data-swup-cache-value="data-cache"
94
+ data-swup-animate-history-browsing-value="data-animate-history-browsing">
95
+ <div id="nav"></div>
96
+ <div id="swup">
97
+ <a href="/">Link</a>
98
+ </div>
99
+ </div>
100
+ </body>
101
+ </html>
102
+ ` ) ;
103
+ const bodyElement = getByTestId ( container , 'body' ) ;
104
+ expect ( bodyElement ) . not . toHaveClass ( 'connected' ) ;
105
+
106
+ startStimulus ( ) ;
107
+ await waitFor ( ( ) => expect ( bodyElement ) . toHaveClass ( 'connected' ) ) ;
108
+ expect ( actualSwupOptions . mainElement ) . toEqual ( undefined ) ;
109
+ expect ( actualSwupOptions . containers ) . toEqual ( [ '#swup' ] ) ;
110
+ } ) ;
111
+
112
+ it ( 'only data-main-element is provided,' , async ( ) => {
113
+ const container = mountDOM ( `
114
+ <html>
115
+ <head>
116
+ <title>Symfony UX</title>
117
+ </head>
118
+ <body>
119
+ <div
120
+ data-testid="body"
121
+ data-controller="check swup"
122
+ data-swup-main-element-value="#main"
123
+ data-swup-link-selector-value="a"
124
+ data-swup-animation-selector-value="[transition-*]"
125
+ data-swup-debug-value="data-debug"
126
+ data-swup-cache-value="data-cache"
127
+ data-swup-animate-history-browsing-value="data-animate-history-browsing">
128
+ <div id="nav"></div>
129
+ <div id="main"></div>
130
+ <div id="swup">
131
+ <a href="/">Link</a>
132
+ </div>
133
+ </div>
134
+ </body>
135
+ </html>
136
+ ` ) ;
137
+ const bodyElement = getByTestId ( container , 'body' ) ;
138
+ expect ( bodyElement ) . not . toHaveClass ( 'connected' ) ;
139
+
140
+ startStimulus ( ) ;
141
+ await waitFor ( ( ) => expect ( bodyElement ) . toHaveClass ( 'connected' ) ) ;
142
+ expect ( actualSwupOptions . mainElement ) . toEqual ( '#main' ) ;
143
+ expect ( actualSwupOptions . containers ) . toEqual ( [ '#main' ] ) ;
144
+ } ) ;
145
+
146
+ it ( 'only data-containers provided' , async ( ) => {
147
+ const container = mountDOM ( `
148
+ <html>
149
+ <head>
150
+ <title>Symfony UX</title>
151
+ </head>
152
+ <body>
153
+ <div
154
+ data-testid="body"
155
+ data-controller="check swup"
156
+ data-swup-containers-value="["#swup", "#nav"]"
157
+ data-swup-link-selector-value="a"
158
+ data-swup-animation-selector-value="[transition-*]"
159
+ data-swup-debug-value="data-debug"
160
+ data-swup-cache-value="data-cache"
161
+ data-swup-animate-history-browsing-value="data-animate-history-browsing">
162
+ <div id="nav"></div>
163
+ <div id="swup">
164
+ <a href="/">Link</a>
165
+ </div>
166
+ </div>
167
+ </body>
168
+ </html>
169
+ ` ) ;
170
+ const bodyElement = getByTestId ( container , 'body' ) ;
171
+ expect ( bodyElement ) . not . toHaveClass ( 'connected' ) ;
172
+
173
+ startStimulus ( ) ;
174
+ await waitFor ( ( ) => expect ( bodyElement ) . toHaveClass ( 'connected' ) ) ;
175
+ expect ( actualSwupOptions . mainElement ) . toEqual ( undefined ) ;
176
+ expect ( actualSwupOptions . containers ) . toEqual ( [ '#swup' , '#nav' ] ) ;
177
+ } ) ;
178
+
179
+ it ( 'data-main-element and data-containers are provided,' , async ( ) => {
180
+ const container = mountDOM ( `
181
+ <html>
182
+ <head>
183
+ <title>Symfony UX</title>
184
+ </head>
185
+ <body>
186
+ <div
187
+ data-testid="body"
188
+ data-controller="check swup"
189
+ data-swup-main-element-value="#main"
190
+ data-swup-containers-value="["#swup", "#nav"]"
191
+ data-swup-link-selector-value="a"
192
+ data-swup-animation-selector-value="[transition-*]"
193
+ data-swup-debug-value="data-debug"
194
+ data-swup-cache-value="data-cache"
195
+ data-swup-animate-history-browsing-value="data-animate-history-browsing">
196
+ <div id="nav"></div>
197
+ <div id="main"></div>
198
+ <div id="swup">
199
+ <a href="/">Link</a>
200
+ </div>
201
+ </div>
202
+ </body>
203
+ </html>
204
+ ` ) ;
205
+ const bodyElement = getByTestId ( container , 'body' ) ;
206
+ expect ( bodyElement ) . not . toHaveClass ( 'connected' ) ;
207
+
208
+ startStimulus ( ) ;
209
+ await waitFor ( ( ) => expect ( bodyElement ) . toHaveClass ( 'connected' ) ) ;
210
+ expect ( actualSwupOptions . mainElement ) . toEqual ( '#main' ) ;
211
+ expect ( actualSwupOptions . containers ) . toEqual ( [ '#main' , '#swup' , '#nav' ] ) ;
212
+ } ) ;
84
213
} ) ;
0 commit comments