10
10
'use strict' ;
11
11
12
12
import { clearDOM } from '@symfony/stimulus-testing' ;
13
- import { startStimulus } from '../tools' ;
13
+ import { initLiveComponent , startStimulus } from '../tools' ;
14
14
import { getByLabelText , waitFor } from '@testing-library/dom' ;
15
15
import userEvent from '@testing-library/user-event' ;
16
16
import fetchMock from 'fetch-mock-jest' ;
17
17
18
18
describe ( 'LiveController data-model Tests' , ( ) => {
19
19
const template = ( data ) => `
20
20
<div
21
- data-controller="live"
22
- data-live-url-value="http://localhost"
21
+ ${ initLiveComponent ( '/_components/my_component' , data ) }
23
22
>
24
23
<label>
25
24
Name:
@@ -39,12 +38,9 @@ describe('LiveController data-model Tests', () => {
39
38
40
39
it ( 'renders correctly with data-model and live#update' , async ( ) => {
41
40
const data = { name : 'Ryan' } ;
42
- const { element, controller } = await startStimulus (
43
- template ( data ) ,
44
- data
45
- ) ;
41
+ const { element, controller } = await startStimulus ( template ( data ) ) ;
46
42
47
- fetchMock . getOnce ( 'http://localhost/ ?name=Ryan+WEAVER' , {
43
+ fetchMock . getOnce ( 'end: ?name=Ryan+WEAVER' , {
48
44
html : template ( { name : 'Ryan Weaver' } ) ,
49
45
data : { name : 'Ryan Weaver' }
50
46
} ) ;
@@ -67,10 +63,7 @@ describe('LiveController data-model Tests', () => {
67
63
68
64
it ( 'correctly only uses the most recent render call results' , async ( ) => {
69
65
const data = { name : 'Ryan' } ;
70
- const { element, controller } = await startStimulus (
71
- template ( data ) ,
72
- data
73
- ) ;
66
+ const { element, controller } = await startStimulus ( template ( data ) ) ;
74
67
75
68
let renderCount = 0 ;
76
69
element . addEventListener ( 'live:render' , ( ) => {
@@ -83,7 +76,7 @@ describe('LiveController data-model Tests', () => {
83
76
[ 'guy' , 150 ]
84
77
] ;
85
78
requests . forEach ( ( [ string , delay ] ) => {
86
- fetchMock . getOnce ( `http://localhost/ ?name=Ryan${ string } ` , {
79
+ fetchMock . getOnce ( `end:my_component ?name=Ryan${ string } ` , {
87
80
// the _ at the end helps us look that the input has changed
88
81
// as a result of a re-render (not just from typing in the input)
89
82
html : template ( { name : `Ryan${ string } _` } ) ,
@@ -114,17 +107,14 @@ describe('LiveController data-model Tests', () => {
114
107
115
108
it ( 'falls back to using the name attribute when no data-model is present' , async ( ) => {
116
109
const data = { name : 'Ryan' } ;
117
- const { element, controller } = await startStimulus (
118
- template ( data ) ,
119
- data
120
- ) ;
110
+ const { element, controller } = await startStimulus ( template ( data ) ) ;
121
111
122
112
// replace data-model with name attribute
123
113
const inputElement = getByLabelText ( element , 'Name:' ) ;
124
114
delete inputElement . dataset . model ;
125
115
inputElement . setAttribute ( 'name' , 'name' ) ;
126
116
127
- fetchMock . getOnce ( 'http://localhost/ ?name=Ryan+WEAVER' , {
117
+ fetchMock . getOnce ( 'end: ?name=Ryan+WEAVER' , {
128
118
html : template ( { name : 'Ryan Weaver' } ) ,
129
119
data : { name : 'Ryan Weaver' }
130
120
} ) ;
@@ -140,17 +130,14 @@ describe('LiveController data-model Tests', () => {
140
130
141
131
it ( 'uses data-model when both name and data-model is present' , async ( ) => {
142
132
const data = { name : 'Ryan' } ;
143
- const { element, controller } = await startStimulus (
144
- template ( data ) ,
145
- data
146
- ) ;
133
+ const { element, controller } = await startStimulus ( template ( data ) ) ;
147
134
148
135
// give element data-model="name" and name="first_name"
149
136
const inputElement = getByLabelText ( element , 'Name:' ) ;
150
137
inputElement . setAttribute ( 'name' , 'first_name' ) ;
151
138
152
139
// ?name should be what's sent to the server
153
- fetchMock . getOnce ( 'http://localhost/ ?name=Ryan+WEAVER' , {
140
+ fetchMock . getOnce ( 'end: ?name=Ryan+WEAVER' , {
154
141
html : template ( { name : 'Ryan Weaver' } ) ,
155
142
data : { name : 'Ryan Weaver' }
156
143
} ) ;
@@ -166,8 +153,7 @@ describe('LiveController data-model Tests', () => {
166
153
it ( 'standardizes user[firstName] style models into post.name' , async ( ) => {
167
154
const deeperModelTemplate = ( data ) => `
168
155
<div
169
- data-controller="live"
170
- data-live-url-value="http://localhost"
156
+ ${ initLiveComponent ( '/_components/my_component' , data ) }
171
157
>
172
158
<label>
173
159
First Name:
@@ -180,16 +166,13 @@ describe('LiveController data-model Tests', () => {
180
166
</div>
181
167
` ;
182
168
const data = { user : { firstName : 'Ryan' } } ;
183
- const { element, controller } = await startStimulus (
184
- deeperModelTemplate ( data ) ,
185
- data
186
- ) ;
169
+ const { element, controller } = await startStimulus ( deeperModelTemplate ( data ) ) ;
187
170
188
171
// replace data-model with name attribute
189
172
const inputElement = getByLabelText ( element , 'First Name:' ) ;
190
173
191
174
const newData = { user : { firstName : 'Ryan Weaver' } } ;
192
- fetchMock . getOnce ( 'http://localhost/ ?user%5BfirstName%5D=Ryan+WEAVER' , {
175
+ fetchMock . getOnce ( 'end: ?user%5BfirstName%5D=Ryan+WEAVER' , {
193
176
html : deeperModelTemplate ( newData ) ,
194
177
data : newData
195
178
} ) ;
@@ -206,8 +189,7 @@ describe('LiveController data-model Tests', () => {
206
189
it ( 'updates correctly when live#update is on a parent element' , async ( ) => {
207
190
const parentUpdateTemplate = ( data ) => `
208
191
<div
209
- data-controller="live"
210
- data-live-url-value="http://localhost"
192
+ ${ initLiveComponent ( '/_components/my_component' , data ) }
211
193
>
212
194
<div data-action="input->live#update">
213
195
<label>
@@ -222,12 +204,9 @@ describe('LiveController data-model Tests', () => {
222
204
` ;
223
205
224
206
const data = { firstName : 'Ryan' } ;
225
- const { element, controller } = await startStimulus (
226
- parentUpdateTemplate ( data ) ,
227
- data
228
- ) ;
207
+ const { element } = await startStimulus ( parentUpdateTemplate ( data ) ) ;
229
208
230
- fetchMock . getOnce ( 'http://localhost/ ?firstName=Ryan+WEAVER' , {
209
+ fetchMock . getOnce ( 'end: ?firstName=Ryan+WEAVER' , {
231
210
html : parentUpdateTemplate ( { firstName : 'Ryan Weaver' } ) ,
232
211
data : { firstName : 'Ryan Weaver' }
233
212
} ) ;
@@ -246,12 +225,9 @@ describe('LiveController data-model Tests', () => {
246
225
it ( 'tracks which fields should be modified, sends, without forgetting previous fields' , async ( ) => {
247
226
// start with one other field in validatedFields
248
227
const data = { name : 'Ryan' , validatedFields : [ 'otherField' ] } ;
249
- const { element, controller } = await startStimulus (
250
- template ( data ) ,
251
- data
252
- ) ;
228
+ const { element } = await startStimulus ( template ( data ) ) ;
253
229
254
- fetchMock . getOnce ( 'http://localhost/ ?name=Ryan+WEAVER&validatedFields%5B0%5D=otherField&validatedFields%5B1%5D=name' , {
230
+ fetchMock . getOnce ( 'end: ?name=Ryan+WEAVER&validatedFields%5B0%5D=otherField&validatedFields%5B1%5D=name' , {
255
231
html : template ( { name : 'Ryan Weaver' } ) ,
256
232
data : { name : 'Ryan Weaver' }
257
233
} ) ;
0 commit comments