Skip to content

Commit 04dc91a

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

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
@@ -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,13 +171,15 @@ 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

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

173185
it('uses custom name instead of prop name in the URL', async () => {
@@ -179,14 +191,14 @@ describe('LiveController query string binding', () => {
179191
);
180192

181193
// Set value
182-
test.expectsAjaxCall().expectUpdatedData({ prop1: 'foo' });
194+
test.expectsAjaxCall().expectUpdatedData({ prop1: 'foo' }).willReturnLiveUrl('?alias1=foo');
183195

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

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

188200
// Remove value
189-
test.expectsAjaxCall().expectUpdatedData({ prop1: '' });
201+
test.expectsAjaxCall().expectUpdatedData({ prop1: '' }).willReturnLiveUrl('?alias1=');
190202

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

src/LiveComponent/assets/test/tools.ts

Lines changed: 14 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,16 @@ 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+
}
276283

277284
const response = new Response(html, {
278285
status: this.customResponseStatusCode || 200,
@@ -342,6 +349,12 @@ class MockedAjaxCall {
342349
return this;
343350
}
344351

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

0 commit comments

Comments
 (0)