Skip to content

Commit a05cb8f

Browse files
committed
fix query-binding tests
1 parent 0e502a2 commit a05cb8f

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

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

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ describe('LiveController query string binding', () => {
5050
// String
5151

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

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

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

5959
// Remove value
60-
test.expectsAjaxCall().expectUpdatedData({ prop1: '' });
60+
test.expectsAjaxCall().expectUpdatedData({ prop1: '' }).willReturnLiveUrl('?prop1=&prop2=');
6161

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

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

6868
// Set value
69-
test.expectsAjaxCall().expectUpdatedData({ prop2: 42 });
69+
test.expectsAjaxCall().expectUpdatedData({ prop2: 42 }).willReturnLiveUrl('?prop1=&prop2=42');
7070

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

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

7575
// Remove value
76-
test.expectsAjaxCall().expectUpdatedData({ prop2: null });
76+
test.expectsAjaxCall().expectUpdatedData({ prop2: null }).willReturnLiveUrl('?prop1=&prop2=');
7777

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

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

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

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

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

98100
// Remove one value
99-
test.expectsAjaxCall().expectUpdatedData({ prop: ['foo'] });
101+
test.expectsAjaxCall()
102+
.expectUpdatedData({ prop: ['foo'] })
103+
.willReturnLiveUrl('?prop[0]=foo');
100104

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

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

105109
// Remove all remaining values
106-
test.expectsAjaxCall().expectUpdatedData({ prop: [] });
110+
test.expectsAjaxCall().expectUpdatedData({ prop: [] }).willReturnLiveUrl('?prop=');
107111

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

@@ -119,28 +123,34 @@ describe('LiveController query string binding', () => {
119123
);
120124

121125
// Set single nested prop
122-
test.expectsAjaxCall().expectUpdatedData({ 'prop.foo': 'dummy' });
126+
test.expectsAjaxCall().expectUpdatedData({ 'prop.foo': 'dummy' }).willReturnLiveUrl('?prop[foo]=dummy');
123127

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

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

128132
// Set multiple values
129-
test.expectsAjaxCall().expectUpdatedData({ prop: { foo: 'other', bar: 42 } });
133+
test.expectsAjaxCall()
134+
.expectUpdatedData({ prop: { foo: 'other', bar: 42 } })
135+
.willReturnLiveUrl('?prop[foo]=other&prop[bar]=42');
130136

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

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

135141
// Remove one value
136-
test.expectsAjaxCall().expectUpdatedData({ prop: { foo: 'other', bar: null } });
142+
test.expectsAjaxCall()
143+
.expectUpdatedData({ prop: { foo: 'other', bar: null } })
144+
.willReturnLiveUrl('?prop[foo]=other');
137145

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

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

142150
// Remove all values
143-
test.expectsAjaxCall().expectUpdatedData({ prop: { foo: null, bar: null } });
151+
test.expectsAjaxCall()
152+
.expectUpdatedData({ prop: { foo: null, bar: null } })
153+
.willReturnLiveUrl('?prop=');
144154

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

@@ -162,13 +172,15 @@ describe('LiveController query string binding', () => {
162172
.expectActionCalled('changeProp')
163173
.serverWillChangeProps((data: any) => {
164174
data.prop = 'foo';
165-
});
175+
})
176+
.willReturnLiveUrl('?prop=foo');
166177

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

169-
await waitFor(() => expect(test.element).toHaveTextContent('Prop: foo'));
170-
171-
expectCurrentSearch().toEqual('?prop=foo');
180+
await waitFor(() => {
181+
expect(test.element).toHaveTextContent('Prop: foo');
182+
expectCurrentSearch().toEqual('?prop=foo');
183+
});
172184
});
173185

174186
it('uses custom name instead of prop name in the URL', async () => {
@@ -180,14 +192,14 @@ describe('LiveController query string binding', () => {
180192
);
181193

182194
// Set value
183-
test.expectsAjaxCall().expectUpdatedData({ prop1: 'foo' });
195+
test.expectsAjaxCall().expectUpdatedData({ prop1: 'foo' }).willReturnLiveUrl('?alias1=foo');
184196

185197
await test.component.set('prop1', 'foo', true);
186198

187199
expectCurrentSearch().toEqual('?alias1=foo');
188200

189201
// Remove value
190-
test.expectsAjaxCall().expectUpdatedData({ prop1: '' });
202+
test.expectsAjaxCall().expectUpdatedData({ prop1: '' }).willReturnLiveUrl('?alias1=');
191203

192204
await test.component.set('prop1', '', true);
193205

src/LiveComponent/assets/test/tools.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ class MockedAjaxCall {
174174
/* Response properties */
175175
private changePropsCallback?: (props: any) => void;
176176
private template?: (props: any) => string;
177+
private liveUrl?: string;
177178
private delayResponseTime?: number = 0;
178179
private customResponseStatusCode?: number;
179180
private customResponseHTML?: string;
@@ -270,10 +271,16 @@ class MockedAjaxCall {
270271
const html = this.customResponseHTML ? this.customResponseHTML : template(newProps);
271272

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

278285
const response = new Response(html, {
279286
status: this.customResponseStatusCode || 200,
@@ -343,6 +350,12 @@ class MockedAjaxCall {
343350
return this;
344351
}
345352

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

0 commit comments

Comments
 (0)