import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { mount } from '@vue/test-utils';
import JivoWidget from '../../resources/js/components/support/JivoWidget.vue';

beforeEach(() => {
    document.querySelectorAll('#jivosite-widget-js').forEach((n) => n.remove());
});

afterEach(() => {
    vi.unstubAllEnvs();
});

describe('JivoWidget', () => {
    it('подгружает скрипт Jivo, когда задан VITE_JIVO_WIDGET_ID', () => {
        vi.stubEnv('VITE_JIVO_WIDGET_ID', 'abc123');
        mount(JivoWidget);
        const script = document.getElementById('jivosite-widget-js') as HTMLScriptElement | null;
        expect(script).not.toBeNull();
        expect(script?.src).toContain('code.jivosite.com/widget/abc123');
    });

    it('ничего не грузит, когда ключ пуст (виджет спит)', () => {
        vi.stubEnv('VITE_JIVO_WIDGET_ID', '');
        mount(JivoWidget);
        expect(document.getElementById('jivosite-widget-js')).toBeNull();
    });
});
