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
49 changes: 33 additions & 16 deletions src/Elastic.Documentation.Site/Assets/api-chrome-sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,71 @@ import {
syncApiVersionSwitcher,
syncIsolatedHeaderTitle,
} from './api-chrome-sync'
import htmx from 'htmx.org'

const picker = (hrefs: string[]) =>
`<div id="api-version-switcher">${hrefs
.map((href) => `<a href="${href}">link</a>`)
.join('')}</div>`

const hrefs = () =>
[...document.querySelectorAll('#api-version-switcher a')].map((a) =>
a.getAttribute('href')
)

// htmx reads a boosted link's href once, when it processes the link. Rewriting the attribute leaves the link
// requesting the page it first pointed at, so the synced picker has to be a new element that htmx processes.
// htmx is mocked in this suite (jsdom cannot run it), so the click itself is covered by the browser journey.
describe('api version switcher sync', () => {
beforeEach(() => {
vi.mocked(htmx.process).mockClear()
document.body.innerHTML = picker(['/api/es', '/api/es/v8'])
})

it('takes the links of the page that was swapped in', () => {
it('replaces the picker with the one of the page that was swapped in and has htmx process it', () => {
const before = document.getElementById('api-version-switcher')
// A detached picker would miss the `hx-boost` it inherits from `<body>`, so htmx must see it in the live page.
let processedInLivePage = false
vi.mocked(htmx.process).mockImplementationOnce((element) => {
processedInLivePage =
element instanceof Element && document.contains(element)
})

const changed = syncApiVersionSwitcher(
`<html><body>${picker([
'/api/es/operation/operation-ping',
'/api/es/v8/operation/operation-ping',
])}</body></html>`
)

const after = document.getElementById('api-version-switcher')
expect(changed).toBe(true)
expect(
[...document.querySelectorAll('a')].map((a) =>
a.getAttribute('href')
)
).toEqual([
expect(hrefs()).toEqual([
'/api/es/operation/operation-ping',
'/api/es/v8/operation/operation-ping',
])
expect(after).not.toBe(before)
expect(htmx.process).toHaveBeenCalledExactlyOnceWith(after)
Comment thread
infra-vault-gh-plugin-prod[bot] marked this conversation as resolved.
Resolved
expect(processedInLivePage).toBe(true)
})

it('leaves the links alone when the response has no picker', () => {
it('leaves the picker alone when the response has no picker', () => {
const before = document.getElementById('api-version-switcher')

const changed = syncApiVersionSwitcher('<html><body></body></html>')

expect(changed).toBe(false)
expect(document.querySelectorAll('a')[1].getAttribute('href')).toBe(
'/api/es/v8'
)
expect(document.getElementById('api-version-switcher')).toBe(before)
expect(htmx.process).not.toHaveBeenCalled()
})

it('leaves the links alone when the picker has a different number of versions', () => {
it('takes the picker of the response when it lists a different number of versions', () => {
const changed = syncApiVersionSwitcher(
`<html><body>${picker(['/api/es'])}</body></html>`
`<html><body>${picker(['/api/es/operation/operation-ping'])}</body></html>`
)

expect(changed).toBe(false)
expect(document.querySelectorAll('a')[1].getAttribute('href')).toBe(
'/api/es/v8'
)
expect(changed).toBe(true)
expect(hrefs()).toEqual(['/api/es/operation/operation-ping'])
})
})

Expand Down
15 changes: 7 additions & 8 deletions src/Elastic.Documentation.Site/Assets/api-chrome-sync.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import htmx from 'htmx.org'

// Boosted API navigation can leave chrome outside the swapped region stale. The response carries the chrome
// for the new page, so the parts that change per page are copied over.

// A swap of #api-content-grid leaves the version picker linking to the first page loaded.
// A swap of #api-content-grid leaves the version picker linking to the first page loaded. htmx reads a boosted
// link's href once, when it processes the link, so the picker is replaced and processed again. Rewriting the
// href would show the new page and still request the old one.
export function syncApiVersionSwitcher(html: string): boolean {
const current = document.getElementById('api-version-switcher')
if (!current) return false
Expand All @@ -10,13 +14,8 @@ export function syncApiVersionSwitcher(html: string): boolean {
.getElementById('api-version-switcher')
if (!next) return false

const currentLinks = current.querySelectorAll('a[href]')
const nextLinks = next.querySelectorAll('a[href]')
if (currentLinks.length !== nextLinks.length) return false

currentLinks.forEach((link, i) =>
link.setAttribute('href', nextLinks[i].getAttribute('href')!)
)
current.replaceWith(next)
htmx.process(next)
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,25 @@ feature navigation-preview
end

# Open an operation of that version: its page shows the method and the route under "Paths". Waiting for a link of the
# new version also waits for the page swap to finish.
expect attached "#elastic-docs-v3 a[href*='/docs/api/elasticsearch/v8/operation/']"
# new version also waits for the page swap to finish. The operation exists in both majors, so the version switch below
# has a page to land on.
expect attached "#elastic-docs-v3 a[href$='/docs/api/elasticsearch/v8/operation/operation-async-search-status']"
wait --load networkidle
wait 1500
click "#elastic-docs-v3 a[href*='/docs/api/elasticsearch/v8/operation/']"
wait --url **/docs/api/elasticsearch/v8/operation/**
click "#elastic-docs-v3 a[href$='/docs/api/elasticsearch/v8/operation/operation-async-search-status']"
wait --url **/docs/api/elasticsearch/v8/operation/operation-async-search-status
expect text ".api-url-listing .api-method-label" "^(GET|PUT|POST|DELETE|HEAD|PATCH)$" --regex
expect text ".api-url-listing .api-url-path" "^/" --regex

# The operation was reached by a link click, which swaps only the page content. Switching version from there keeps
# the operation: the picker must link to this operation's page in the other version, not to that version's landing page.
feature !navigation-preview
click "#api-hub-switcher button"
click "#api-version-switcher a[href$='/docs/api/elasticsearch/operation/operation-async-search-status']"
end
feature navigation-preview
click "#api-version-switcher button"
click "#api-version-switcher a[href$='/docs/api/elasticsearch/operation/operation-async-search-status']"
end
wait --url **/docs/api/elasticsearch/operation/operation-async-search-status
expect url **/docs/api/elasticsearch/operation/operation-async-search-status
Loading