Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 120 additions & 1 deletion types/hotwired__turbo/hotwired__turbo-tests.ts
Original file line number Diff line number Diff line change
@@ -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")!;

Expand Down Expand Up @@ -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<string>
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<boolean>) | 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("<turbo-stream></turbo-stream>");
renderStreamMessage(streamMessage);
Turbo.renderStreamMessage("<turbo-stream></turbo-stream>");
Turbo.renderStreamMessage(streamMessage);

// $ExpectType "text/vnd.turbo-stream.html"
StreamMessage.contentType;

// $ExpectType StreamMessage
StreamMessage.wrap("<turbo-stream></turbo-stream>");
Loading