Skip to content

Commit 6a846b3

Browse files
committed
Fixing bug in test: we now need to return the Content-Type header
1 parent baaef75 commit 6a846b3

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const wildcardExternalsPlugin = (peerDependencies) => ({
3838
}
3939
});
4040

41-
const files = glob.sync("src/**/assets/src/*controller.ts");
41+
const files = glob.sync("src/**/assets/src/live_controller.ts");
4242
const packages = files.map((file) => {
4343
const absolutePath = path.join(__dirname, file);
4444
const packageData = require(pkgUp.sync({ cwd: absolutePath }));

src/LiveComponent/assets/test/controller/error.test.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ describe('LiveController Error Handling', () => {
2828
// ONLY a post is sent, not a re-render GET
2929
test.expectsAjaxCall('post')
3030
.expectSentData(test.initialData)
31-
.serverWillReturnAnError(500, `
32-
<html><head><title>Error!</title></head><body><h1>An error ocurred</h1></body></html>
31+
.serverWillReturnCustomResponse(500, `
32+
<html><head><title>Error!</title></head><body><h1>An error occurred</h1></body></html>
3333
`)
3434
.expectActionCalled('save')
3535
.init();
@@ -45,4 +45,28 @@ describe('LiveController Error Handling', () => {
4545
}
4646
expect(errorContainer.querySelector('iframe')).not.toBeNull();
4747
});
48+
49+
it('displays a modal on any non-component response', async () => {
50+
const test = await createTest({ }, (data: any) => `
51+
<div ${initComponent(data)}>
52+
Original component text
53+
<button data-action="live#action" data-action-name="save">Save</button>
54+
</div>
55+
`);
56+
57+
// ONLY a post is sent, not a re-render GET
58+
test.expectsAjaxCall('post')
59+
.expectSentData(test.initialData)
60+
.serverWillReturnCustomResponse(200, `
61+
<html><head><title>Hi!</title></head><body><h1>I'm a whole page, not a component!</h1></body></html>
62+
`)
63+
.expectActionCalled('save')
64+
.init();
65+
66+
getByText(test.element, 'Save').click();
67+
68+
await waitFor(() => expect(document.getElementById('live-component-error')).not.toBeNull());
69+
// the component did not change or re-render
70+
expect(test.element).toHaveTextContent('Original component text');
71+
});
4872
});

src/LiveComponent/assets/test/tools.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ class MockedAjaxCall {
120120
options: any = {};
121121
fetchMock?: typeof fetchMock;
122122
routeName?: string;
123-
errorStatusCode?: number;
124-
errorHTML?: string;
123+
customResponseStatusCode?: number;
124+
customResponseHTML?: string;
125125

126126
constructor(method: string, test: FunctionalTest) {
127127
this.method = method.toUpperCase();
@@ -183,13 +183,18 @@ class MockedAjaxCall {
183183
const template = this.template ? this.template : this.test.template;
184184

185185
let response;
186-
if (this.errorStatusCode) {
186+
if (this.customResponseStatusCode) {
187187
response = {
188-
body: this.errorHTML,
189-
status: this.errorStatusCode
188+
body: this.customResponseHTML,
189+
status: this.customResponseStatusCode
190190
}
191191
} else {
192-
response = template(finalServerData);
192+
response = {
193+
body: template(finalServerData),
194+
headers: {
195+
'Content-Type': 'application/vnd.live-component+html'
196+
}
197+
}
193198
}
194199

195200
this.fetchMock = fetchMock.mock(
@@ -213,10 +218,10 @@ class MockedAjaxCall {
213218
return this;
214219
}
215220

216-
serverWillReturnAnError(statusCode: number, responseHTML: string): MockedAjaxCall {
221+
serverWillReturnCustomResponse(statusCode: number, responseHTML: string): MockedAjaxCall {
217222
this.checkInitialization('serverWillReturnAnError');
218-
this.errorStatusCode = statusCode;
219-
this.errorHTML = responseHTML;
223+
this.customResponseStatusCode = statusCode;
224+
this.customResponseHTML = responseHTML;
220225

221226
return this;
222227
}

0 commit comments

Comments
 (0)