import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
import { resizeObserver } from './resize-observer.js';
import { mountHost, connectTrait, resetDOM } from '../test-helpers.js';

describe('resize-observer', () => {
  let originalRO;

  beforeEach(() => {
    resetDOM();
    originalRO = globalThis.ResizeObserver;
  });

  afterEach(() => {
    if (originalRO) globalThis.ResizeObserver = originalRO;
    else delete globalThis.ResizeObserver;
  });

  it('without ResizeObserver: gracefully no-ops on connect', () => {
    delete globalThis.ResizeObserver;
    const host = mountHost();
    expect(() => connectTrait(resizeObserver, host)).not.toThrow();
  });

  it('with mocked ResizeObserver: observe() called + disconnect cleans up', () => {
    const observe = vi.fn();
    const roDisconnect = vi.fn();
    globalThis.ResizeObserver = function MockRO() {
      this.observe = observe;
      this.disconnect = roDisconnect;
      this.unobserve = vi.fn();
    };
    const host = mountHost();
    const inst = connectTrait(resizeObserver, host);
    expect(observe).toHaveBeenCalledWith(host);
    inst.disconnect(host);
    expect(roDisconnect).toHaveBeenCalled();
  });
});
