import { TestBed } from '@angular/core/testing';
import { ThemeService } from './theme.service';
import { LocalStoreConstants } from '../constants/LocalStoreConstants';

describe('ThemeService', () => {
  beforeEach(() => {
    localStorage.clear();
  });

  afterEach(() => {
    localStorage.clear();
  });

  function createService(): ThemeService {
    TestBed.configureTestingModule({});
    return TestBed.inject(ThemeService);
  }

  it('uses the stored theme as the initial value', () => {
    localStorage.setItem(LocalStoreConstants.THEME_KEY, 'dark');
    const service = createService();

    expect(service.theme()).toBe('dark');
  });

  it('toggle switches between light and dark', () => {
    localStorage.setItem(LocalStoreConstants.THEME_KEY, 'light');
    const service = createService();

    service.toggle();
    expect(service.theme()).toBe('dark');

    service.toggle();
    expect(service.theme()).toBe('light');
  });

  it('setTheme persists the selection to localStorage', () => {
    localStorage.setItem(LocalStoreConstants.THEME_KEY, 'light');
    const service = createService();

    service.setTheme('dark');
    TestBed.tick();

    expect(localStorage.getItem(LocalStoreConstants.THEME_KEY)).toBe('dark');
  });
});
