Skip to content

Commit ceabe03

Browse files
committed
fix query-binding tests
1 parent 80945b5 commit ceabe03

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

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

+25-14
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ describe('LiveController query string binding', () => {
4949
// String
5050

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

5454
await test.component.set('prop1', 'foo', true);
5555

5656
expectCurrentSearch().toEqual('?prop1=foo&prop2=');
5757

5858
// Remove value
59-
test.expectsAjaxCall().expectUpdatedData({ prop1: '' });
59+
test.expectsAjaxCall().expectUpdatedData({ prop1: '' }).willReturnLiveUrl('?prop1=&prop2=');
6060

6161
await test.component.set('prop1', '', true);
6262

@@ -65,14 +65,14 @@ describe('LiveController query string binding', () => {
6565
// Number
6666

6767
// Set value
68-
test.expectsAjaxCall().expectUpdatedData({ prop2: 42 });
68+
test.expectsAjaxCall().expectUpdatedData({ prop2: 42 }).willReturnLiveUrl('?prop1=&prop2=42');
6969

7070
await test.component.set('prop2', 42, true);
7171

7272
expectCurrentSearch().toEqual('?prop1=&prop2=42');
7373

7474
// Remove value
75-
test.expectsAjaxCall().expectUpdatedData({ prop2: null });
75+
test.expectsAjaxCall().expectUpdatedData({ prop2: null }).willReturnLiveUrl('?prop1=&prop2=');
7676

7777
await test.component.set('prop2', null, true);
7878

@@ -88,21 +88,25 @@ describe('LiveController query string binding', () => {
8888
);
8989

9090
// Set value
91-
test.expectsAjaxCall().expectUpdatedData({ prop: ['foo', 'bar'] });
91+
test.expectsAjaxCall()
92+
.expectUpdatedData({ prop: ['foo', 'bar'] })
93+
.willReturnLiveUrl('?prop[0]=foo&prop[1]=bar');
9294

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

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

9799
// Remove one value
98-
test.expectsAjaxCall().expectUpdatedData({ prop: ['foo'] });
100+
test.expectsAjaxCall()
101+
.expectUpdatedData({ prop: ['foo'] })
102+
.willReturnLiveUrl('?prop[0]=foo');
99103

100104
await test.component.set('prop', ['foo'], true);
101105

102106
expectCurrentSearch().toEqual('?prop[0]=foo');
103107

104108
// Remove all remaining values
105-
test.expectsAjaxCall().expectUpdatedData({ prop: [] });
109+
test.expectsAjaxCall().expectUpdatedData({ prop: [] }).willReturnLiveUrl('?prop=');
106110

107111
await test.component.set('prop', [], true);
108112

@@ -118,28 +122,34 @@ describe('LiveController query string binding', () => {
118122
);
119123

120124
// Set single nested prop
121-
test.expectsAjaxCall().expectUpdatedData({ 'prop.foo': 'dummy' });
125+
test.expectsAjaxCall().expectUpdatedData({ 'prop.foo': 'dummy' }).willReturnLiveUrl('?prop[foo]=dummy');
122126

123127
await test.component.set('prop.foo', 'dummy', true);
124128

125129
expectCurrentSearch().toEqual('?prop[foo]=dummy');
126130

127131
// Set multiple values
128-
test.expectsAjaxCall().expectUpdatedData({ prop: { foo: 'other', bar: 42 } });
132+
test.expectsAjaxCall()
133+
.expectUpdatedData({ prop: { foo: 'other', bar: 42 } })
134+
.willReturnLiveUrl('?prop[foo]=other&prop[bar]=42');
129135

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

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

134140
// Remove one value
135-
test.expectsAjaxCall().expectUpdatedData({ prop: { foo: 'other', bar: null } });
141+
test.expectsAjaxCall()
142+
.expectUpdatedData({ prop: { foo: 'other', bar: null } })
143+
.willReturnLiveUrl('?prop[foo]=other');
136144

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

139147
expectCurrentSearch().toEqual('?prop[foo]=other');
140148

141149
// Remove all values
142-
test.expectsAjaxCall().expectUpdatedData({ prop: { foo: null, bar: null } });
150+
test.expectsAjaxCall()
151+
.expectUpdatedData({ prop: { foo: null, bar: null } })
152+
.willReturnLiveUrl('?prop=');
143153

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

@@ -161,7 +171,8 @@ describe('LiveController query string binding', () => {
161171
.expectActionCalled('changeProp')
162172
.serverWillChangeProps((data: any) => {
163173
data.prop = 'foo';
164-
});
174+
})
175+
.willReturnLiveUrl('?prop=foo');
165176

166177
getByText(test.element, 'Change prop').click();
167178

@@ -179,14 +190,14 @@ describe('LiveController query string binding', () => {
179190
);
180191

181192
// Set value
182-
test.expectsAjaxCall().expectUpdatedData({ prop1: 'foo' });
193+
test.expectsAjaxCall().expectUpdatedData({ prop1: 'foo' }).willReturnLiveUrl('?alias1=foo');
183194

184195
await test.component.set('prop1', 'foo', true);
185196

186197
expectCurrentSearch().toEqual('?alias1=foo');
187198

188199
// Remove value
189-
test.expectsAjaxCall().expectUpdatedData({ prop1: '' });
200+
test.expectsAjaxCall().expectUpdatedData({ prop1: '' }).willReturnLiveUrl('?alias1=');
190201

191202
await test.component.set('prop1', '', true);
192203

src/LiveComponent/assets/test/tools.ts

+15-1
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)