Skip to content

Commit 6903e06

Browse files
committed
fix query-binding tests
1 parent a1d8067 commit 6903e06

File tree

2 files changed

+56
-15
lines changed

2 files changed

+56
-15
lines changed

src/LiveComponent/assets/test/controller/query-binding.test.ts

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,18 @@ describe('LiveController query string binding', () => {
4949
// String
5050

5151
// Set value
52-
test.expectsAjaxCall().expectUpdatedData({ prop1: 'foo' });
52+
test.expectsAjaxCall()
53+
.expectUpdatedData({ prop1: 'foo' })
54+
.willReturnLiveUrl('?prop1=foo&prop2=');
5355

5456
await test.component.set('prop1', 'foo', true);
5557

5658
expectCurrentSearch().toEqual('?prop1=foo&prop2=');
5759

5860
// Remove value
59-
test.expectsAjaxCall().expectUpdatedData({ prop1: '' });
61+
test.expectsAjaxCall()
62+
.expectUpdatedData({ prop1: '' })
63+
.willReturnLiveUrl('?prop1=&prop2=');
6064

6165
await test.component.set('prop1', '', true);
6266

@@ -65,14 +69,18 @@ describe('LiveController query string binding', () => {
6569
// Number
6670

6771
// Set value
68-
test.expectsAjaxCall().expectUpdatedData({ prop2: 42 });
72+
test.expectsAjaxCall()
73+
.expectUpdatedData({ prop2: 42 })
74+
.willReturnLiveUrl('?prop1=&prop2=42');
6975

7076
await test.component.set('prop2', 42, true);
7177

7278
expectCurrentSearch().toEqual('?prop1=&prop2=42');
7379

7480
// Remove value
75-
test.expectsAjaxCall().expectUpdatedData({ prop2: null });
81+
test.expectsAjaxCall()
82+
.expectUpdatedData({ prop2: null })
83+
.willReturnLiveUrl('?prop1=&prop2=');
7684

7785
await test.component.set('prop2', null, true);
7886

@@ -88,21 +96,27 @@ describe('LiveController query string binding', () => {
8896
);
8997

9098
// Set value
91-
test.expectsAjaxCall().expectUpdatedData({ prop: ['foo', 'bar'] });
99+
test.expectsAjaxCall()
100+
.expectUpdatedData({ prop: ['foo', 'bar'] })
101+
.willReturnLiveUrl('?prop[0]=foo&prop[1]=bar');
92102

93103
await test.component.set('prop', ['foo', 'bar'], true);
94104

95105
expectCurrentSearch().toEqual('?prop[0]=foo&prop[1]=bar');
96106

97107
// Remove one value
98-
test.expectsAjaxCall().expectUpdatedData({ prop: ['foo'] });
108+
test.expectsAjaxCall()
109+
.expectUpdatedData({ prop: ['foo'] })
110+
.willReturnLiveUrl('?prop[0]=foo');
99111

100112
await test.component.set('prop', ['foo'], true);
101113

102114
expectCurrentSearch().toEqual('?prop[0]=foo');
103115

104116
// Remove all remaining values
105-
test.expectsAjaxCall().expectUpdatedData({ prop: [] });
117+
test.expectsAjaxCall()
118+
.expectUpdatedData({ prop: [] })
119+
.willReturnLiveUrl('?prop=');
106120

107121
await test.component.set('prop', [], true);
108122

@@ -118,28 +132,36 @@ describe('LiveController query string binding', () => {
118132
);
119133

120134
// Set single nested prop
121-
test.expectsAjaxCall().expectUpdatedData({ 'prop.foo': 'dummy' });
135+
test.expectsAjaxCall()
136+
.expectUpdatedData({ 'prop.foo': 'dummy' })
137+
.willReturnLiveUrl('?prop[foo]=dummy');
122138

123139
await test.component.set('prop.foo', 'dummy', true);
124140

125141
expectCurrentSearch().toEqual('?prop[foo]=dummy');
126142

127143
// Set multiple values
128-
test.expectsAjaxCall().expectUpdatedData({ prop: { foo: 'other', bar: 42 } });
144+
test.expectsAjaxCall()
145+
.expectUpdatedData({ prop: { foo: 'other', bar: 42 } })
146+
.willReturnLiveUrl('?prop[foo]=other&prop[bar]=42');
129147

130148
await test.component.set('prop', { foo: 'other', bar: 42 }, true);
131149

132150
expectCurrentSearch().toEqual('?prop[foo]=other&prop[bar]=42');
133151

134152
// Remove one value
135-
test.expectsAjaxCall().expectUpdatedData({ prop: { foo: 'other', bar: null } });
153+
test.expectsAjaxCall()
154+
.expectUpdatedData({ prop: { foo: 'other', bar: null } })
155+
.willReturnLiveUrl('?prop[foo]=other');
136156

137157
await test.component.set('prop', { foo: 'other', bar: null }, true);
138158

139159
expectCurrentSearch().toEqual('?prop[foo]=other');
140160

141161
// Remove all values
142-
test.expectsAjaxCall().expectUpdatedData({ prop: { foo: null, bar: null } });
162+
test.expectsAjaxCall()
163+
.expectUpdatedData({ prop: { foo: null, bar: null } })
164+
.willReturnLiveUrl('?prop=');
143165

144166
await test.component.set('prop', { foo: null, bar: null }, true);
145167

@@ -161,7 +183,8 @@ describe('LiveController query string binding', () => {
161183
.expectActionCalled('changeProp')
162184
.serverWillChangeProps((data: any) => {
163185
data.prop = 'foo';
164-
});
186+
})
187+
.willReturnLiveUrl('?prop=foo');
165188

166189
getByText(test.element, 'Change prop').click();
167190

@@ -179,14 +202,18 @@ describe('LiveController query string binding', () => {
179202
);
180203

181204
// Set value
182-
test.expectsAjaxCall().expectUpdatedData({ prop1: 'foo' });
205+
test.expectsAjaxCall()
206+
.expectUpdatedData({ prop1: 'foo' })
207+
.willReturnLiveUrl('?alias1=foo');
183208

184209
await test.component.set('prop1', 'foo', true);
185210

186211
expectCurrentSearch().toEqual('?alias1=foo');
187212

188213
// Remove value
189-
test.expectsAjaxCall().expectUpdatedData({ prop1: '' });
214+
test.expectsAjaxCall()
215+
.expectUpdatedData({ prop1: '' })
216+
.willReturnLiveUrl('?alias1=');
190217

191218
await test.component.set('prop1', '', true);
192219

src/LiveComponent/assets/test/tools.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ class MockedAjaxCall {
173173
/* Response properties */
174174
private changePropsCallback?: (props: any) => void;
175175
private template?: (props: any) => string;
176+
private liveUrl?: string;
176177
private delayResponseTime?: number = 0;
177178
private customResponseStatusCode?: number;
178179
private customResponseHTML?: string;
@@ -269,10 +270,17 @@ class MockedAjaxCall {
269270
const html = this.customResponseHTML ? this.customResponseHTML : template(newProps);
270271

271272
// assume a normal, live-component response unless it's totally custom
272-
const headers = { 'Content-Type': 'application/vnd.live-component+html' };
273+
const headers = {
274+
'Content-Type': 'application/vnd.live-component+html',
275+
'X-Live-Url': '',
276+
};
273277
if (this.customResponseHTML) {
274278
headers['Content-Type'] = 'text/html';
275279
}
280+
if (this.liveUrl) {
281+
headers['X-Live-Url'] = this.liveUrl;
282+
}
283+
console.log('headers', headers);
276284

277285
const response = new Response(html, {
278286
status: this.customResponseStatusCode || 200,
@@ -342,6 +350,12 @@ class MockedAjaxCall {
342350
return this;
343351
}
344352

353+
willReturnLiveUrl(liveUrl: string): MockedAjaxCall {
354+
this.liveUrl = liveUrl;
355+
356+
return this;
357+
}
358+
345359
serverWillReturnCustomResponse(statusCode: number, responseHTML: string): MockedAjaxCall {
346360
this.customResponseStatusCode = statusCode;
347361
this.customResponseHTML = responseHTML;

0 commit comments

Comments
 (0)