-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor liveview handlers to support nested paths; experiement with …
…live_view_route; test; bump version to 0.0.11
- Loading branch information
Showing
8 changed files
with
405 additions
and
296 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { SessionData } from "express-session"; | ||
import { nanoid } from "nanoid"; | ||
import request from "superwstest"; | ||
import { configLiveViewHandler } from "."; | ||
import { BaseLiveViewComponent } from "./component/base_component"; | ||
import { LiveViewMountParams, LiveViewSocket } from "./component/types"; | ||
import { LiveViewServer } from "./live_view_server"; | ||
import { html } from "./templates"; | ||
|
||
|
||
describe("test live view route", () => { | ||
|
||
it("starting / stopping twice has no effect", async () => { | ||
const lvServer = new LiveViewServer({ | ||
signingSecret: "MY_VERY_SECRET_KEY", | ||
port: 7878, | ||
pageTitleDefaults: { | ||
prefix: "TitlePrefix - ", | ||
suffix: " - TitleSuffix", | ||
title: "Title", | ||
}, | ||
}); | ||
lvServer.start(); | ||
expect(lvServer.isStarted).toBe(true); | ||
lvServer.start(); | ||
expect(lvServer.isStarted).toBe(true); | ||
lvServer.shutdown(); | ||
expect(lvServer.isStarted).toBe(false); | ||
lvServer.shutdown(); | ||
expect(lvServer.isStarted).toBe(false); | ||
}) | ||
|
||
it("use configLiveViewHandler", (done) => { | ||
const lvServer = new LiveViewServer({ | ||
signingSecret: "MY_VERY_SECRET_KEY", | ||
port: 7878, | ||
pageTitleDefaults: { | ||
prefix: "TitlePrefix - ", | ||
suffix: " - TitleSuffix", | ||
title: "Title", | ||
}, | ||
}); | ||
|
||
lvServer.start(); | ||
expect(lvServer.isStarted).toBe(true); | ||
const lvComponent = new LiveViewComponent() | ||
lvServer.expressApp.get(...configLiveViewHandler( | ||
"/test/foo", | ||
lvComponent, | ||
"root.html.ejs", | ||
"my signing secret", | ||
(req) => { | ||
return { | ||
...req.session, // copy session data | ||
csrfToken: req.session.csrfToken || nanoid(), | ||
} | ||
}, | ||
{ | ||
title: "Default Title" | ||
} | ||
)) | ||
lvServer.start() | ||
setTimeout((() => { | ||
request(lvServer.httpServer).get('/test/foo').expect(200).then(res => { | ||
expect(res.text).toContain(lvComponent.render({ message: "test" }).toString()) | ||
done(); | ||
lvServer.shutdown() | ||
}) | ||
}), 100) | ||
}) | ||
|
||
|
||
|
||
}) | ||
|
||
declare module 'express-session' { | ||
interface SessionData { | ||
message: string; | ||
} | ||
} | ||
|
||
class LiveViewComponent extends BaseLiveViewComponent<{ message?: string }, {}> { | ||
|
||
mount(params: LiveViewMountParams, session: Partial<SessionData>, socket: LiveViewSocket<{}>): {} { | ||
return { message: session.message || "test" } | ||
} | ||
|
||
render(ctx: { message: string }) { | ||
const { message } = ctx | ||
return html`<div>${message}</div>`; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters