Skip to content

Commit

Permalink
fix(vike): remove vike contentType warning
Browse files Browse the repository at this point in the history
  • Loading branch information
camfou authored and Romakita committed Mar 6, 2024
1 parent 94a2419 commit b2719c2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
34 changes: 25 additions & 9 deletions packages/third-parties/vike/src/services/ViteService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ async function getServiceFixture(httpResponse: any) {

(mod.renderPage as jest.Mock).mockResolvedValue(pageContext);

return {renderPage: mod.renderPage, service, $ctx, pageContext, stateSnapshot: PlatformTest.injector.settings.get("stateSnapshot")};
return {
renderPage: mod.renderPage,
service,
$ctx,
pageContext,
stateSnapshot: PlatformTest.injector.settings.get("stateSnapshot")
};
}

describe("ViteService", () => {
Expand All @@ -49,12 +55,12 @@ describe("ViteService", () => {
it("should render the page", async () => {
const {$ctx, service, renderPage} = await getServiceFixture({
statusCode: 200,
contentType: "text/html",
headers: [["content-type", "text/html"]],
body: "html"
});

jest.spyOn($ctx.response, "status").mockReturnThis();
jest.spyOn($ctx.response, "contentType").mockReturnThis();
jest.spyOn($ctx.response, "setHeader").mockReturnThis();

const result = await service.render("*", {$ctx});

Expand All @@ -80,7 +86,7 @@ describe("ViteService", () => {
urlOriginal: "/"
});
expect($ctx.response.status).toHaveBeenCalledWith(200);
expect($ctx.response.contentType).toHaveBeenCalledWith("text/html");
expect($ctx.response.setHeader).toHaveBeenCalledWith("content-type", "text/html");
});
it("should return empty content if the page doesn't contains jsx content", async () => {
const {$ctx, service, renderPage} = await getServiceFixture(null);
Expand Down Expand Up @@ -143,24 +149,34 @@ describe("ViteService", () => {
beforeEach(() => {
jest.resetAllMocks();
return PlatformTest.create({
vite: {enableStream: true, root: "./path/to/client", stateSnapshot: jest.fn().mockReturnValue({state: "state"})}
vite: {
enableStream: true,
root: "./path/to/client",
stateSnapshot: jest.fn().mockReturnValue({state: "state"})
}
});
});
afterEach(() => PlatformTest.reset());

it("should render the page", async () => {
const {$ctx, service, renderPage} = await getServiceFixture({
statusCode: 200,
contentType: "text/html",
headers: [["content-type", "text/html"]],
body: "html"
});

jest.spyOn($ctx.response, "status").mockReturnThis();
jest.spyOn($ctx.response, "contentType").mockReturnThis();
jest.spyOn($ctx.response, "setHeader").mockReturnThis();

const result = await service.render("*", {$ctx});

expect(result).toEqual({body: "html", contentType: "text/html", statusCode: 200});
expect(result).toEqual({
body: "html",
"headers": [
["content-type", "text/html"]
],
statusCode: 200
});
expect(renderPage).toHaveBeenCalledWith({
view: "*",
pageProps: {
Expand All @@ -182,7 +198,7 @@ describe("ViteService", () => {
urlOriginal: "/"
});
expect($ctx.response.status).toHaveBeenCalledWith(200);
expect($ctx.response.contentType).toHaveBeenCalledWith("text/html");
expect($ctx.response.setHeader).toHaveBeenCalledWith("content-type", "text/html");
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions packages/third-parties/vike/src/services/ViteService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export class ViteService {

if (pageContext.httpResponse) {
const {httpResponse} = pageContext;

$ctx.response.contentType(httpResponse.contentType).status(httpResponse.statusCode);
httpResponse.headers?.forEach(([name, value]: [string, string]) => $ctx.response.setHeader(name, value));
$ctx.response.status(httpResponse.statusCode);

if (this.enableStream) {
return httpResponse;
Expand Down

0 comments on commit b2719c2

Please sign in to comment.