Skip to content

Commit

Permalink
Fix preview widget insertion trigger (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospassos authored Dec 12, 2022
1 parent e0025b3 commit 669d75f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ export class PreviewPlugin implements Plugin {
}
});

document.body.prepend(widget);
const insert = (): void => document.body?.prepend(widget);

if (document.readyState === 'complete') {
insert();
} else {
window.addEventListener('DOMContentLoaded', insert);
}
}

private updateUrl(): void {
Expand Down
24 changes: 24 additions & 0 deletions test/preview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,30 @@ describe('A Preview plugin', () => {
expect(src.searchParams.get('variant')).toBe(metadata.variantName);
});

it('should insert the widget when the document is ready', () => {
Object.defineProperty(document, 'readyState', {
writable: true,
value: 'loading',
});

const plugin = new PreviewPlugin(configuration);

configuration.tokenStore.setToken(token);

plugin.enable();

expect(document.body.querySelector('iframe')).toBe(null);

Object.defineProperty(document, 'readyState', {
writable: true,
value: 'complete',
});

window.dispatchEvent(new Event('DOMContentLoaded'));

expect(document.body.querySelector('iframe')).not.toBe(null);
});

it('should ignore messages from unknown origins', () => {
const plugin = new PreviewPlugin(configuration);

Expand Down

0 comments on commit 669d75f

Please sign in to comment.