import BmsRelativeTime from '@/components/utils/BmsRelativeTime.vue';
import { shallowMount } from '@vue/test-utils';
import { vi } from 'vitest';

const factory = (props = {}) => {
  return shallowMount(BmsRelativeTime, {
    propsData: { ...props },
  });
};

describe('BmsRelativeTime', () => {
  beforeEach(() => {
    vi.useFakeTimers();
  });

  it('shoudl correctly call the timeout', () => {
    // under 60 seconds, calling every 10seconds
    // over : every 30 seconds

    const delay = 60000;
    const delta = 40;
    const time = Date.now() - delta * 1000;
    const wrapper = factory({ relativeTo: time });

    vi.advanceTimersByTime(delay);
    // MARTY, GO TO THE FUTURE : ${delay}s !

    expect(wrapper.emitted()).toHaveProperty('change');
    expect(wrapper.emitted().change).toEqual([
      [delta],
      [delta + 10],
      [delta + 20],
      [delta + 50],
    ]);
    // 4 because the first call on mounted, the next two at every 10s because it's under 60s, and the last one at 50s (0+10+10+30);
  });

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