diff --git a/types/hotwired__turbo/hotwired__turbo-tests.ts b/types/hotwired__turbo/hotwired__turbo-tests.ts index 3ea7a89377bec7..ec30cf42b88978 100644 --- a/types/hotwired__turbo/hotwired__turbo-tests.ts +++ b/types/hotwired__turbo/hotwired__turbo-tests.ts @@ -1,4 +1,17 @@ -import { StreamActions, visit } from "@hotwired/turbo"; +import { + cache, + config, + connectStreamSource, + disconnectStreamSource, + navigator, + renderStreamMessage, + session, + start, + StreamActions, + StreamMessage, + StreamSource, + visit, +} from "@hotwired/turbo"; const turboFrame = document.querySelector("turbo-frame")!; @@ -114,3 +127,109 @@ document.addEventListener("turbo:submit-end", function(event) { event.detail.fetchResponse; } }); + +// Test start() function +start(); +Turbo.start(); + +// Test session.adapter +// $ExpectType BrowserAdapter +session.adapter; +session.adapter.formSubmissionStarted(); +session.adapter.formSubmissionFinished(); +Turbo.session.adapter.formSubmissionStarted(); +Turbo.session.adapter.formSubmissionFinished(); + +// Test navigator.submitForm +const form = document.querySelector("form")!; +navigator.submitForm(form); +navigator.submitForm(form, document.querySelector("button")!); +Turbo.navigator.submitForm(form); +Turbo.navigator.submitForm(form, document.querySelector("button")!); + +// Test cache methods +cache.clear(); +cache.resetCacheControl(); +cache.exemptPageFromCache(); +cache.exemptPageFromPreview(); +Turbo.cache.clear(); +Turbo.cache.resetCacheControl(); +Turbo.cache.exemptPageFromCache(); +Turbo.cache.exemptPageFromPreview(); + +// Test config.drive +// $ExpectType boolean +config.drive.enabled; +// $ExpectType number +config.drive.progressBarDelay; +config.drive.progressBarDelay = 1000; +// $ExpectType Set +config.drive.unvisitableExtensions; + +// Test config.drive.enabled assignment +config.drive.enabled = false; +config.drive.enabled = true; + +// Test config.drive.unvisitableExtensions Set modification +config.drive.unvisitableExtensions.add(".custom"); +config.drive.unvisitableExtensions.delete(".pdf"); + +// Test config.forms +// $ExpectType "on" | "off" | "optin" +config.forms.mode; +config.forms.mode = "optin"; +config.forms.mode = "on"; +config.forms.mode = "off"; +// @ts-expect-error +config.forms.mode = "invalid"; + +// Test Turbo.config +Turbo.config.drive.progressBarDelay = 300; +Turbo.config.forms.mode = "optin"; + +// Test config.forms.confirm is optional (undefined by default, can be set to undefined) +// $ExpectType ((message: string, element: HTMLFormElement, submitter: HTMLElement | null) => Promise) | undefined +config.forms.confirm; +config.forms.confirm = undefined; + +// Test config.forms.confirm assignment +config.forms.confirm = async (message, element, submitter) => { + return window.confirm(message); +}; + +// Test StreamElement.templateElement and templateContent +// $ExpectType HTMLTemplateElement +turboStream.templateElement; +// $ExpectType DocumentFragment +turboStream.templateContent; + +// @ts-expect-error - templateElement is readonly +turboStream.templateElement = document.createElement("template"); +// @ts-expect-error - templateContent is readonly +turboStream.templateContent = document.createDocumentFragment(); + +const eventSource = new EventSource("https://example.com/stream"); +const webSocket = new WebSocket("wss://example.com/stream"); + +const streamSource: StreamSource = eventSource; +connectStreamSource(streamSource); +disconnectStreamSource(streamSource); +connectStreamSource(eventSource); +disconnectStreamSource(eventSource); +connectStreamSource(webSocket); +disconnectStreamSource(webSocket); + +// @ts-expect-error +connectStreamSource({}); + +const streamMessage = new StreamMessage(document.createDocumentFragment()); +renderStreamMessage(""); +renderStreamMessage(streamMessage); +Turbo.renderStreamMessage(""); +Turbo.renderStreamMessage(streamMessage); + +// $ExpectType "text/vnd.turbo-stream.html" +StreamMessage.contentType; + +// $ExpectType StreamMessage +StreamMessage.wrap(""); diff --git a/types/hotwired__turbo/index.d.ts b/types/hotwired__turbo/index.d.ts index d5232a06bce928..8bacdec62669a3 100644 --- a/types/hotwired__turbo/index.d.ts +++ b/types/hotwired__turbo/index.d.ts @@ -13,10 +13,13 @@ export class FrameElement extends HTMLElement { } export class StreamElement extends HTMLElement { + static renderElement(newElement: StreamElement): Promise; + connectedCallback(): Promise; render(): Promise; disconnect(): void; removeDuplicateTargetChildren(): void; + removeDuplicateTargetSiblings(): void; /** * The current action. @@ -38,6 +41,41 @@ export class StreamElement extends HTMLElement { * Reads the request-id attribute */ readonly requestId: string; + + /** + * Gets the main `