Skip to content

Commit

Permalink
fix: prevent widget to be removed when onWidgetLoad exists
Browse files Browse the repository at this point in the history
fix #56
  • Loading branch information
marsidev committed Apr 27, 2024
1 parent 3ece995 commit c3bc85d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions demos/nextjs/src/components/demo-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export default function DemoWidget({
setToken(token)
setStatus('solved')
}}
onWidgetLoad={widgetId => console.log('Widget loaded', widgetId)}
/>

<h3>Configuration</h3>
Expand Down
4 changes: 0 additions & 4 deletions packages/lib/src/lib.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,6 @@ export const Turnstile = forwardRef<TurnstileInstance | undefined, TurnstileProp
if (!checkElementExistence(widgetId.current)) return

onWidgetLoad?.(widgetId.current)

return () => {
if (widgetId.current && window.turnstile) window.turnstile!.remove(widgetId.current)
}
}, [widgetId, onWidgetLoad])

useEffect(() => {
Expand Down
16 changes: 16 additions & 0 deletions test/e2e/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const isCI = process.env.CI

let browser: Browser
let page: Page
let logsCount = 0

const route = 'basic'

Expand Down Expand Up @@ -50,6 +51,21 @@ test('widget iframe is visible', async () => {
}))
})

test('`onWidgetLoad` callback is called', async () => {
if (logsCount > 0) return
const iframe = page.getByTitle('Widget containing a Cloudflare security challenge')
const widgetId = await iframe.getAttribute('id')
expect(widgetId).toBeDefined()

page.on('console', msg => {
const text = msg.text()
if (text.includes('Widget loaded') && logsCount === 0) {
logsCount++
expect(text).toBe(`Widget loaded ${widgetId}`)
}
})
})

test('challenge has been solved', async () => {
await ensureChallengeSolved(page)
!isCI &&
Expand Down

0 comments on commit c3bc85d

Please sign in to comment.