import { describe, it, expect, beforeEach } from 'vitest';
import { portal } from './portal.js';
import { mountHost, connectTrait, resetDOM } from '../test-helpers.js';

describe('portal', () => {
  beforeEach(resetDOM);

  it('connect sets data-portal-active', () => {
    const host = mountHost();
    connectTrait(portal, host);
    expect(host.hasAttribute('data-portal-active')).toBe(true);
  });

  it('disconnect clears the active attribute', () => {
    const host = mountHost();
    const inst = connectTrait(portal, host);
    inst.disconnect(host);
    expect(host.hasAttribute('data-portal-active')).toBe(false);
  });

  it('with data-portal-target, host is moved to the target element', () => {
    const target = document.createElement('div');
    target.id = 'portal-out';
    document.body.appendChild(target);
    const host = mountHost('div', { 'data-portal-target': '#portal-out' });
    connectTrait(portal, host);
    // The trait moves the host into the target on connect.
    expect(target.contains(host)).toBe(true);
  });
});
