Skip to content

Commit 8267b32

Browse files
committed
fix query-binding tests
1 parent af803e8 commit 8267b32

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

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

+15-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,21 @@ describe('LiveController query string binding', () => {
8888
);
8989

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

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

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

9797
// Remove one value
98-
test.expectsAjaxCall().expectUpdatedData({ prop: ['foo'] });
98+
test.expectsAjaxCall().expectUpdatedData({ prop: ['foo'] }).willReturnLiveUrl('?prop[0]=foo');
9999

100100
await test.component.set('prop', ['foo'], true);
101101

102102
expectCurrentSearch().toEqual('?prop[0]=foo');
103103

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

107107
await test.component.set('prop', [], true);
108108

@@ -118,28 +118,28 @@ describe('LiveController query string binding', () => {
118118
);
119119

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

123123
await test.component.set('prop.foo', 'dummy', true);
124124

125125
expectCurrentSearch().toEqual('?prop[foo]=dummy');
126126

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

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

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

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

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

139139
expectCurrentSearch().toEqual('?prop[foo]=other');
140140

141141
// Remove all values
142-
test.expectsAjaxCall().expectUpdatedData({ prop: { foo: null, bar: null } });
142+
test.expectsAjaxCall().expectUpdatedData({ prop: { foo: null, bar: null } }).willReturnLiveUrl('?prop=');
143143

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

@@ -161,7 +161,8 @@ describe('LiveController query string binding', () => {
161161
.expectActionCalled('changeProp')
162162
.serverWillChangeProps((data: any) => {
163163
data.prop = 'foo';
164-
});
164+
})
165+
.willReturnLiveUrl('?prop=foo');
165166

166167
getByText(test.element, 'Change prop').click();
167168

@@ -179,14 +180,14 @@ describe('LiveController query string binding', () => {
179180
);
180181

181182
// Set value
182-
test.expectsAjaxCall().expectUpdatedData({ prop1: 'foo' });
183+
test.expectsAjaxCall().expectUpdatedData({ prop1: 'foo' }).willReturnLiveUrl('?alias1=foo');
183184

184185
await test.component.set('prop1', 'foo', true);
185186

186187
expectCurrentSearch().toEqual('?alias1=foo');
187188

188189
// Remove value
189-
test.expectsAjaxCall().expectUpdatedData({ prop1: '' });
190+
test.expectsAjaxCall().expectUpdatedData({ prop1: '' }).willReturnLiveUrl('?alias1=');
190191

191192
await test.component.set('prop1', '', true);
192193

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)