|
1 | | -import { StreamActions, visit } from "@hotwired/turbo"; |
| 1 | +import { |
| 2 | + cache, |
| 3 | + config, |
| 4 | + connectStreamSource, |
| 5 | + disconnectStreamSource, |
| 6 | + navigator, |
| 7 | + renderStreamMessage, |
| 8 | + session, |
| 9 | + start, |
| 10 | + StreamActions, |
| 11 | + StreamMessage, |
| 12 | + StreamSource, |
| 13 | + visit, |
| 14 | +} from "@hotwired/turbo"; |
2 | 15 |
|
3 | 16 | const turboFrame = document.querySelector("turbo-frame")!; |
4 | 17 |
|
@@ -114,3 +127,109 @@ document.addEventListener("turbo:submit-end", function(event) { |
114 | 127 | event.detail.fetchResponse; |
115 | 128 | } |
116 | 129 | }); |
| 130 | + |
| 131 | +// Test start() function |
| 132 | +start(); |
| 133 | +Turbo.start(); |
| 134 | + |
| 135 | +// Test session.adapter |
| 136 | +// $ExpectType BrowserAdapter |
| 137 | +session.adapter; |
| 138 | +session.adapter.formSubmissionStarted(); |
| 139 | +session.adapter.formSubmissionFinished(); |
| 140 | +Turbo.session.adapter.formSubmissionStarted(); |
| 141 | +Turbo.session.adapter.formSubmissionFinished(); |
| 142 | + |
| 143 | +// Test navigator.submitForm |
| 144 | +const form = document.querySelector("form")!; |
| 145 | +navigator.submitForm(form); |
| 146 | +navigator.submitForm(form, document.querySelector("button")!); |
| 147 | +Turbo.navigator.submitForm(form); |
| 148 | +Turbo.navigator.submitForm(form, document.querySelector("button")!); |
| 149 | + |
| 150 | +// Test cache methods |
| 151 | +cache.clear(); |
| 152 | +cache.resetCacheControl(); |
| 153 | +cache.exemptPageFromCache(); |
| 154 | +cache.exemptPageFromPreview(); |
| 155 | +Turbo.cache.clear(); |
| 156 | +Turbo.cache.resetCacheControl(); |
| 157 | +Turbo.cache.exemptPageFromCache(); |
| 158 | +Turbo.cache.exemptPageFromPreview(); |
| 159 | + |
| 160 | +// Test config.drive |
| 161 | +// $ExpectType boolean |
| 162 | +config.drive.enabled; |
| 163 | +// $ExpectType number |
| 164 | +config.drive.progressBarDelay; |
| 165 | +config.drive.progressBarDelay = 1000; |
| 166 | +// $ExpectType Set<string> |
| 167 | +config.drive.unvisitableExtensions; |
| 168 | + |
| 169 | +// Test config.drive.enabled assignment |
| 170 | +config.drive.enabled = false; |
| 171 | +config.drive.enabled = true; |
| 172 | + |
| 173 | +// Test config.drive.unvisitableExtensions Set modification |
| 174 | +config.drive.unvisitableExtensions.add(".custom"); |
| 175 | +config.drive.unvisitableExtensions.delete(".pdf"); |
| 176 | + |
| 177 | +// Test config.forms |
| 178 | +// $ExpectType "on" | "off" | "optin" |
| 179 | +config.forms.mode; |
| 180 | +config.forms.mode = "optin"; |
| 181 | +config.forms.mode = "on"; |
| 182 | +config.forms.mode = "off"; |
| 183 | +// @ts-expect-error |
| 184 | +config.forms.mode = "invalid"; |
| 185 | + |
| 186 | +// Test Turbo.config |
| 187 | +Turbo.config.drive.progressBarDelay = 300; |
| 188 | +Turbo.config.forms.mode = "optin"; |
| 189 | + |
| 190 | +// Test config.forms.confirm is optional (undefined by default, can be set to undefined) |
| 191 | +// $ExpectType ((message: string, element: HTMLFormElement, submitter: HTMLElement | null) => Promise<boolean>) | undefined |
| 192 | +config.forms.confirm; |
| 193 | +config.forms.confirm = undefined; |
| 194 | + |
| 195 | +// Test config.forms.confirm assignment |
| 196 | +config.forms.confirm = async (message, element, submitter) => { |
| 197 | + return window.confirm(message); |
| 198 | +}; |
| 199 | + |
| 200 | +// Test StreamElement.templateElement and templateContent |
| 201 | +// $ExpectType HTMLTemplateElement |
| 202 | +turboStream.templateElement; |
| 203 | +// $ExpectType DocumentFragment |
| 204 | +turboStream.templateContent; |
| 205 | + |
| 206 | +// @ts-expect-error - templateElement is readonly |
| 207 | +turboStream.templateElement = document.createElement("template"); |
| 208 | +// @ts-expect-error - templateContent is readonly |
| 209 | +turboStream.templateContent = document.createDocumentFragment(); |
| 210 | + |
| 211 | +const eventSource = new EventSource("https://example.com/stream"); |
| 212 | +const webSocket = new WebSocket("wss://example.com/stream"); |
| 213 | + |
| 214 | +const streamSource: StreamSource = eventSource; |
| 215 | +connectStreamSource(streamSource); |
| 216 | +disconnectStreamSource(streamSource); |
| 217 | +connectStreamSource(eventSource); |
| 218 | +disconnectStreamSource(eventSource); |
| 219 | +connectStreamSource(webSocket); |
| 220 | +disconnectStreamSource(webSocket); |
| 221 | + |
| 222 | +// @ts-expect-error |
| 223 | +connectStreamSource({}); |
| 224 | + |
| 225 | +const streamMessage = new StreamMessage(document.createDocumentFragment()); |
| 226 | +renderStreamMessage("<turbo-stream></turbo-stream>"); |
| 227 | +renderStreamMessage(streamMessage); |
| 228 | +Turbo.renderStreamMessage("<turbo-stream></turbo-stream>"); |
| 229 | +Turbo.renderStreamMessage(streamMessage); |
| 230 | + |
| 231 | +// $ExpectType "text/vnd.turbo-stream.html" |
| 232 | +StreamMessage.contentType; |
| 233 | + |
| 234 | +// $ExpectType StreamMessage |
| 235 | +StreamMessage.wrap("<turbo-stream></turbo-stream>"); |
0 commit comments