/// <reference types="xrm" />

import { describe, it, expect, beforeEach, vi } from 'vitest';
import ExampleWebResource from '../src/example';

describe('ExampleWebResource', () => {
  let formContextMock: { ui: { setFormNotification: ReturnType<typeof vi.fn> } };
  let executionContextMock: { getFormContext: ReturnType<typeof vi.fn> };

  beforeEach(() => {
    formContextMock = {
      ui: {
        setFormNotification: vi.fn(),
      },
    };

    executionContextMock = {
      getFormContext: vi.fn(() => formContextMock),
    };
  });

  it('calls setFormNotification with the correct args on onLoad', () => {
    const resource = new ExampleWebResource();
    resource.onLoad(executionContextMock as unknown as Xrm.Events.EventContext);

    expect(executionContextMock.getFormContext).toHaveBeenCalled();
    expect(formContextMock.ui.setFormNotification).toHaveBeenCalledWith(
      'This is a notification from the Example Web Resource.',
      'INFO',
      'exampleNotification'
    );
  });

  it('attaches itself to window.ExampleWebResource', () => {
    // Vitest runs in a jsdom-like environment by default; window should exist
    expect((window as any).ExampleWebResource).toBe(ExampleWebResource);
  });
});
