CoolFace
Modelpublic

AXERA-TECH/lite_webui

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes3downloads
settings-modal.test.js78 linesDownload Raw Back to tests
1import { beforeEach, describe, expect, it, vi } from 'vitest';2import { SettingsModal } from '../src/components/settings-modal.js';3import { store } from '../src/store.js';4 5function mountModal() {6  const modal = new SettingsModal();7  modal.render();8  modal.show();9  return modal;10}11 12beforeEach(() => {13  document.body.innerHTML = '';14  vi.restoreAllMocks();15});16 17describe('SettingsModal', () => {18  it('shows only fetched models for the current baseUrl', () => {19    store.saveSettings({ baseUrl: 'http://a.local' });20    store.saveAvailableModels('http://a.local', ['model-a']);21    store.saveAvailableModels('http://b.local', ['model-b']);22 23    const modal = mountModal();24    const text = modal.el.querySelector('#model-caps-list').textContent;25 26    expect(text).toContain('model-a');27    expect(text).not.toContain('model-b');28  });29 30  it('saves fetched models under the current baseUrl', async () => {31    store.saveSettings({ baseUrl: 'http://a.local', apiKey: '' });32    globalThis.fetch = vi.fn().mockResolvedValue({33      ok: true,34      json: async () => ({ data: [{ id: 'model-a' }] }),35    });36 37    const modal = mountModal();38    await modal._fetchModels();39 40    expect(store.getAvailableModels('http://a.local')).toEqual(['model-a']);41  });42 43  it('does not close when clicking inside the modal panel while editing inputs', () => {44    const modal = mountModal();45    const panel = modal.el.querySelector('.modal-panel');46    const input = modal.el.querySelector('#settings-base-url');47 48    panel.dispatchEvent(new MouseEvent('mousedown', { bubbles: true }));49    input.dispatchEvent(new MouseEvent('click', { bubbles: true }));50 51    expect(document.body.contains(modal.el)).toBe(true);52    expect(modal.visible).toBe(true);53  });54 55  it('closes only when the backdrop itself is clicked', () => {56    const modal = mountModal();57 58    modal.el.dispatchEvent(new MouseEvent('mousedown', { bubbles: true }));59    modal.el.dispatchEvent(new MouseEvent('click', { bubbles: true }));60 61    expect(document.body.contains(modal.el)).toBe(false);62    expect(modal.visible).toBe(false);63  });64 65  it('clears selected model when switching to a URL without that model', () => {66    store.saveAvailableModels('http://a.local', ['model-a']);67    store.setCurrentModel('http://a.local', 'model-a');68    store.saveSettings({ baseUrl: 'http://a.local' });69 70    const modal = mountModal();71    modal.el.querySelector('#settings-base-url').value = 'http://b.local';72    modal.el.querySelector('#settings-save').click();73 74    expect(store.getSettings().baseUrl).toBe('http://b.local');75    expect(store.getCurrentModel('http://b.local')).toBe('');76  });77});78