Skip to content
Open
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
111 changes: 46 additions & 65 deletions packages/wxt/src/utils/content-script-ui/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ describe('Content Script UIs', () => {
});
ui.mount();

expect(
document.querySelector('div[data-wxt-integrated]'),
).not.toBeNull();
expect(document.body.children).toContain(ui.wrapper);
expect(document.querySelector('app')).not.toBeNull();
});

Expand All @@ -77,9 +75,7 @@ describe('Content Script UIs', () => {
});
ui.mount();

expect(
document.querySelector('pre[data-wxt-integrated]'),
).not.toBeNull();
expect(document.querySelector('pre')).toBe(ui.wrapper);
expect(document.querySelector('app')).not.toBeNull();
});
});
Expand All @@ -92,7 +88,7 @@ describe('Content Script UIs', () => {
});
ui.mount();

expect(document.querySelector('div[data-wxt-iframe]')).toBeDefined();
expect(document.body.children).toContain(ui.wrapper);
expect(document.querySelector('iframe')).toBeDefined();
});
});
Expand All @@ -108,9 +104,7 @@ describe('Content Script UIs', () => {
});
ui.mount();

expect(
document.querySelector('test-component[data-wxt-shadow-root]'),
).not.toBeNull();
expect(document.querySelector('test-component')).toBe(ui.shadowHost);
expect(ui.shadow.querySelector('app')).not.toBeNull();
});

Expand Down Expand Up @@ -143,7 +137,7 @@ describe('Content Script UIs', () => {
});

expect(ui.wrapper.outerHTML).toMatchInlineSnapshot(
`"<div data-wxt-iframe=""><iframe src="chrome-extension://test-extension-id/page.html"></iframe></div>"`,
`"<div><iframe src="chrome-extension://test-extension-id/page.html"></iframe></div>"`,
);
});
});
Expand All @@ -157,7 +151,7 @@ describe('Content Script UIs', () => {
ui.mount();

expect(ui.wrapper.outerHTML).toMatchInlineSnapshot(
`"<div data-wxt-iframe="" style="overflow: visible; position: relative; width: 0px; height: 0px; display: block;"><iframe src="chrome-extension://test-extension-id/page.html" style="position: absolute; top: 0px; left: 0px;"></iframe></div>"`,
`"<div style="overflow: visible; position: relative; width: 0px; height: 0px; display: block;"><iframe src="chrome-extension://test-extension-id/page.html" style="position: absolute; top: 0px; left: 0px;"></iframe></div>"`,
);
});

Expand All @@ -170,7 +164,7 @@ describe('Content Script UIs', () => {
ui.mount();

expect(ui.wrapper.outerHTML).toMatchInlineSnapshot(
`"<div data-wxt-iframe="" style="overflow: visible; position: relative; width: 0px; height: 0px; display: block;"><iframe src="chrome-extension://test-extension-id/page.html" style="position: absolute; top: 0px; left: 0px;"></iframe></div>"`,
`"<div style="overflow: visible; position: relative; width: 0px; height: 0px; display: block;"><iframe src="chrome-extension://test-extension-id/page.html" style="position: absolute; top: 0px; left: 0px;"></iframe></div>"`,
);
});

Expand All @@ -183,7 +177,7 @@ describe('Content Script UIs', () => {
ui.mount();

expect(ui.wrapper.outerHTML).toMatchInlineSnapshot(
`"<div data-wxt-iframe="" style="overflow: visible; position: relative; width: 0px; height: 0px; display: block;"><iframe src="chrome-extension://test-extension-id/page.html" style="position: absolute; top: 0px; right: 0px;"></iframe></div>"`,
`"<div style="overflow: visible; position: relative; width: 0px; height: 0px; display: block;"><iframe src="chrome-extension://test-extension-id/page.html" style="position: absolute; top: 0px; right: 0px;"></iframe></div>"`,
);
});

Expand All @@ -196,7 +190,7 @@ describe('Content Script UIs', () => {
ui.mount();

expect(ui.wrapper.outerHTML).toMatchInlineSnapshot(
`"<div data-wxt-iframe="" style="overflow: visible; position: relative; width: 0px; height: 0px; display: block;"><iframe src="chrome-extension://test-extension-id/page.html" style="position: absolute; bottom: 0px; right: 0px;"></iframe></div>"`,
`"<div style="overflow: visible; position: relative; width: 0px; height: 0px; display: block;"><iframe src="chrome-extension://test-extension-id/page.html" style="position: absolute; bottom: 0px; right: 0px;"></iframe></div>"`,
);
});

Expand All @@ -209,7 +203,7 @@ describe('Content Script UIs', () => {
ui.mount();

expect(ui.wrapper.outerHTML).toMatchInlineSnapshot(
`"<div data-wxt-iframe="" style="overflow: visible; position: relative; width: 0px; height: 0px; display: block;"><iframe src="chrome-extension://test-extension-id/page.html" style="position: absolute; bottom: 0px; left: 0px;"></iframe></div>"`,
`"<div style="overflow: visible; position: relative; width: 0px; height: 0px; display: block;"><iframe src="chrome-extension://test-extension-id/page.html" style="position: absolute; bottom: 0px; left: 0px;"></iframe></div>"`,
);
});

Expand All @@ -235,7 +229,7 @@ describe('Content Script UIs', () => {
ui.mount();

expect(ui.wrapper.outerHTML).toMatchInlineSnapshot(
`"<div data-wxt-iframe="" style="overflow: visible; position: relative; width: 0px; height: 0px; display: block;"><iframe src="chrome-extension://test-extension-id/page.html" style="position: fixed; top: 0px; bottom: 0px; left: 0px; right: 0px;"></iframe></div>"`,
`"<div style="overflow: visible; position: relative; width: 0px; height: 0px; display: block;"><iframe src="chrome-extension://test-extension-id/page.html" style="position: fixed; top: 0px; bottom: 0px; left: 0px; right: 0px;"></iframe></div>"`,
);
});

Expand All @@ -262,9 +256,7 @@ describe('Content Script UIs', () => {
});
ui.mount();

expect(
document.querySelector('body > div[data-wxt-integrated]'),
).not.toBeNull();
expect(document.body.children).toContain(ui.wrapper);
});
});

Expand All @@ -277,9 +269,9 @@ describe('Content Script UIs', () => {
});
ui.mount();

expect(
document.querySelector('#parent > div[data-wxt-integrated]'),
).not.toBeNull();
expect(document.querySelector('#parent')!.children).toContain(
ui.wrapper,
);
});

it('should append the element using an XPath string', () => {
Expand All @@ -295,9 +287,7 @@ describe('Content Script UIs', () => {
});
ui.mount();

expect(
document.querySelector('#three > div[data-wxt-integrated]'),
).not.toBeNull();
expect(document.querySelector('#three')!.children[0]).toBe(ui.wrapper);
});
});

Expand All @@ -310,9 +300,9 @@ describe('Content Script UIs', () => {
});
ui.mount();

expect(
document.querySelector('#parent > div[data-wxt-integrated]'),
).not.toBeNull();
expect(document.querySelector('#parent')!.children).toContain(
ui.wrapper,
);
});
});

Expand All @@ -325,9 +315,9 @@ describe('Content Script UIs', () => {
});
ui.mount();

expect(
document.querySelector('#parent > div[data-wxt-integrated]'),
).not.toBeNull();
expect(document.querySelector('#parent')!.children).toContain(
ui.wrapper,
);
});
});

Expand All @@ -354,9 +344,7 @@ describe('Content Script UIs', () => {
ui.mount();

expect(
document.querySelector(
'#parent > div[data-wxt-integrated]:last-child',
),
document.querySelector('#parent')!.lastElementChild,
).not.toBeNull();
});
});
Expand All @@ -372,9 +360,7 @@ describe('Content Script UIs', () => {
ui.mount();

expect(
document.querySelector(
'#parent > div[data-wxt-integrated]:first-child',
),
document.querySelector('#parent')!.children[0].firstElementChild,
).not.toBeNull();
});
});
Expand All @@ -389,9 +375,7 @@ describe('Content Script UIs', () => {
});
ui.mount();

expect(
document.querySelector('body > div[data-wxt-integrated]'),
).not.toBeNull();
expect(document.body.children).toContain(ui.wrapper);
expect(document.querySelector('#parent')).toBeNull();
});
});
Expand All @@ -406,11 +390,9 @@ describe('Content Script UIs', () => {
});
ui.mount();

expect(
document.querySelector(
'#parent > div[data-wxt-integrated]:first-child',
),
).not.toBeNull();
expect(document.querySelector('#parent')!.firstElementChild).toBe(
ui.wrapper,
);
});
});

Expand All @@ -424,11 +406,9 @@ describe('Content Script UIs', () => {
});
ui.mount();

expect(
document.querySelector(
'#parent > div[data-wxt-integrated]:last-child',
),
).not.toBeNull();
expect(document.querySelector('#parent')!.lastElementChild).toBe(
ui.wrapper,
);
});
});

Expand All @@ -444,9 +424,8 @@ describe('Content Script UIs', () => {
});
ui.mount();

expect(
document.querySelector('body > div[data-wxt-integrated]'),
).not.toBeNull();
expect(document.body.children).toContain(ui.wrapper);

expect(document.querySelector('#parent')).toBeNull();
});
});
Expand Down Expand Up @@ -529,23 +508,29 @@ describe('Content Script UIs', () => {
{
name: 'integrated',
createUiFunction: createIntegratedUi,
uiSelector: 'div[data-wxt-integrated]',
uiSelector: '[data-wrapper-name="integrated"]',
},
{
name: 'iframe',
createUiFunction: createIframeUi,
uiSelector: 'div[data-wxt-iframe]',
uiSelector: `[data-wrapper-name="iframe"]`,
},
{
name: 'shadow-root',
createUiFunction: createShadowRootUi,
uiSelector: 'test-component[data-wxt-shadow-root]',
uiSelector: 'test-component',
},
] as const)(
'built-in UI type: $name',
({ name, createUiFunction, uiSelector }) => {
const onMount = vi.fn((wrapper: HTMLElement) => {
if (name === 'shadow-root') return;

wrapper.setAttribute('data-wrapper-name', name);
appendTestApp(wrapper);
});

it('should mount if an anchor already exists at the initialization', async () => {
const onMount = vi.fn(appendTestApp);
ui = await createUiFunction(ctx, {
position: 'inline',
onMount,
Expand All @@ -555,6 +540,7 @@ describe('Content Script UIs', () => {
});

appendTestElement({ id: DYNAMIC_CHILD_ID });

ui.autoMount();
await runMicrotasks();

Expand All @@ -564,7 +550,6 @@ describe('Content Script UIs', () => {
});

it('should mount when an anchor is dynamically added and unmount when an anchor is removed', async () => {
const onMount = vi.fn(appendTestApp);
const onRemove = vi.fn();
ui = await createUiFunction(ctx, {
position: 'inline',
Expand Down Expand Up @@ -599,7 +584,6 @@ describe('Content Script UIs', () => {

describe('options', () => {
it('should auto-mount only once mount and remove when the `once` option is true', async () => {
const onMount = vi.fn(appendTestApp);
const onRemove = vi.fn();
ui = await createUiFunction(ctx, {
position: 'inline',
Expand Down Expand Up @@ -644,7 +628,7 @@ describe('Content Script UIs', () => {
it('should throw when anchor is set as type Element', async () => {
ui = await createUiFunction(ctx, {
position: 'inline',
onMount: appendTestApp,
onMount,
anchor: document.documentElement,
page: name === 'iframe' ? '/page.html' : undefined,
name: 'test-component',
Expand All @@ -657,7 +641,7 @@ describe('Content Script UIs', () => {
it('should throw when anchor is set as type `() => Element`', async () => {
ui = await createUiFunction(ctx, {
position: 'inline',
onMount: appendTestApp,
onMount,
anchor: () => document.documentElement,
page: name === 'iframe' ? '/page.html' : undefined,
name: 'test-component',
Expand All @@ -670,7 +654,6 @@ describe('Content Script UIs', () => {

describe('StopAutoMount', () => {
it('should stop auto-mounting and remove ui when `ui.remove` is called', async () => {
const onMount = vi.fn(appendTestApp);
const onRemove = vi.fn();
ui = await createUiFunction(ctx, {
position: 'inline',
Expand Down Expand Up @@ -705,7 +688,6 @@ describe('Content Script UIs', () => {
});

it('should call internal StopAutoMount when `ui.remove` is called', async () => {
const onMount = vi.fn(appendTestApp);
const onRemove = vi.fn();
const onStop = vi.fn();
ui = await createUiFunction(ctx, {
Expand All @@ -723,7 +705,6 @@ describe('Content Script UIs', () => {
});

it('should allow calling automount again after internal StopAutoMount is called', async () => {
const onMount = vi.fn(appendTestApp);
ui = await createUiFunction(ctx, {
position: 'inline',
onMount,
Expand Down
1 change: 0 additions & 1 deletion packages/wxt/src/utils/content-script-ui/iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export function createIframeUi<TMounted>(
options: IframeContentScriptUiOptions<TMounted>,
): IframeContentScriptUi<TMounted> {
const wrapper = document.createElement('div');
wrapper.setAttribute('data-wxt-iframe', '');
const iframe = document.createElement('iframe');
// @ts-expect-error: getURL is defined per-project, but not inside the package
iframe.src = browser.runtime.getURL(options.page);
Expand Down
1 change: 0 additions & 1 deletion packages/wxt/src/utils/content-script-ui/integrated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export function createIntegratedUi<TMounted>(
options: IntegratedContentScriptUiOptions<TMounted>,
): IntegratedContentScriptUi<TMounted> {
const wrapper = document.createElement(options.tag || 'div');
wrapper.setAttribute('data-wxt-integrated', '');

let mounted: TMounted | undefined = undefined;
const mount = () => {
Expand Down
1 change: 0 additions & 1 deletion packages/wxt/src/utils/content-script-ui/shadow-root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export async function createShadowRootUi<TMounted>(
mode: options.mode ?? 'open',
isolateEvents: options.isolateEvents,
});
shadowHost.setAttribute('data-wxt-shadow-root', '');

let mounted: TMounted | undefined;

Expand Down