| 1 |
import { test, expect } from '@playwright/test'; |
| 2 |
|
| 3 |
import { expectToBeAt, expectToHaveText } from '../support/commands.js'; |
| 4 |
import { navigateWithSwup, waitForSwup } from '../support/swup.js'; |
| 5 |
|
| 6 |
test.describe('persisting', () => { |
| 7 |
test.beforeEach(async ({ page }) => { |
| 8 |
await page.goto('/persist-1.html'); |
| 9 |
await waitForSwup(page); |
| 10 |
}); |
| 11 |
|
| 12 |
test('persists elements across page loads', async ({ page }) => { |
| 13 |
const identifier = String(Math.random()); |
| 14 |
const persistedEl = page.getByTestId('persisted'); |
| 15 |
const unpersistedEl = page.getByTestId('unpersisted'); |
| 16 |
|
| 17 |
const state = await persistedEl.evaluate((el, id) => el.dataset.id = id, identifier); |
| 18 |
|
| 19 |
await navigateWithSwup(page, '/persist-2.html'); |
| 20 |
|
| 21 |
await expectToBeAt(page, '/persist-2.html', 'Persist 2'); |
| 22 |
await expectToHaveText(persistedEl, 'Persist 1'); |
| 23 |
await expectToHaveText(unpersistedEl, 'Persist 2'); |
| 24 |
|
| 25 |
const persistedState = await persistedEl.evaluate((el) => el.dataset.id); |
| 26 |
expect(persistedState).not.toBeFalsy(); |
| 27 |
expect(persistedState).toBe(state); |
| 28 |
expect(persistedState).toBe(identifier); |
| 29 |
}); |
| 30 |
}); |