From 9d0cd2d84e7718710cb18203d15e0cbb89a4ca0f Mon Sep 17 00:00:00 2001 From: Jeremiah Date: Wed, 27 Jan 2021 21:56:23 -0600 Subject: [PATCH] Preserve input values in cache Re-submission of https://github.com/hotwired/turbo/pull/70 This change will save the values of ``s, ` + + + + + + +
Rendering
diff --git a/src/tests/functional/rendering_tests.ts b/src/tests/functional/rendering_tests.ts index d29b82b88..24a6fc2b6 100644 --- a/src/tests/functional/rendering_tests.ts +++ b/src/tests/functional/rendering_tests.ts @@ -8,6 +8,8 @@ import { nextBody, nextEventNamed, pathname, + propertyForSelector, + readEventLogs, scrollToSelector, selectorHasFocus, sleep, @@ -19,6 +21,7 @@ import { test.beforeEach(async ({ page }) => { await page.goto("/src/tests/fixtures/rendering.html") await clearLocalStorage(page) + await readEventLogs(page) }) test("test triggers before-render and render events", async ({ page }) => { @@ -392,6 +395,61 @@ test("test preserves permanent element video playback", async ({ page }) => { assert.equal(timeAfterRender, timeBeforeRender, "element state is preserved") }) +test("test preserves input values", async ({ page }) => { + await page.fill("#text-input", "test") + await page.click("#checkbox-input") + await page.click("#radio-input") + await page.fill("#textarea", "test") + await page.selectOption("#select", "2") + await page.selectOption("#select-multiple", "2") + + await page.click("#same-origin-link") + await nextEventNamed(page, "turbo:load") + await page.goBack() + await nextEventNamed(page, "turbo:load") + + assert.equal(await propertyForSelector(page, "#text-input", "value"), "test") + assert.equal(await propertyForSelector(page, "#checkbox-input", "checked"), true) + assert.equal(await propertyForSelector(page, "#radio-input", "checked"), true) + assert.equal(await propertyForSelector(page, "#textarea", "value"), "test") + assert.equal(await propertyForSelector(page, "#select", "value"), "2") + assert.equal(await propertyForSelector(page, "#select-multiple", "value"), "2") +}) + +test("test does not preserve password values", async ({ page }) => { + await page.fill("#password-input", "test") + + await page.click("#same-origin-link") + await nextEventNamed(page, "turbo:load") + await page.goBack() + await nextEventNamed(page, "turbo:load") + + assert.equal(await propertyForSelector(page, "#password-input", "value"), "") +}) + +test("test clears values when restored from cache", async ({ page }) => { + await page.fill("#text-input", "test") + await page.click("#checkbox-input") + await page.click("#radio-input") + await page.fill("#textarea", "test") + await page.selectOption("#select", "2") + await page.selectOption("#select-multiple", "2") + + await page.click("#same-origin-link") + await nextEventNamed(page, "turbo:load") + await page.goBack() + await nextEventNamed(page, "turbo:load") + + await page.click("#reset-input") + + assert.equal(await propertyForSelector(page, "#text-input", "value"), "") + assert.equal(await propertyForSelector(page, "#checkbox-input", "checked"), false) + assert.equal(await propertyForSelector(page, "#radio-input", "checked"), false) + assert.equal(await propertyForSelector(page, "#textarea", "value"), "") + assert.equal(await propertyForSelector(page, "#select", "value"), "1") + assert.equal(await propertyForSelector(page, "#select-multiple", "value"), "") +}) + test("test before-cache event", async ({ page }) => { await page.evaluate(() => { addEventListener("turbo:before-cache", () => (document.body.innerHTML = "Modified"), { once: true })