import { field } from '@/plugins/field';
import BmsInputNumber, { Props } from './BmsInputNumber.vue';
import { mount } from '@vue/test-utils';
import { nextTick } from 'vue';

const factory = (props: Props) => {
  const wrapper = mount(BmsInputNumber, {
    props,
    global: { plugins: [field] },
  });
  const inputElement = wrapper.get('input');

  return { wrapper, inputElement };
};

describe('BmsInputNumber', () => {
  it('should display correct value on init', () => {
    const { inputElement } = factory({ modelValue: 10 });

    expect(inputElement.element.value).toStrictEqual('10');
  });
  it('should react correctly on input', async () => {
    const { wrapper, inputElement } = factory({ modelValue: 10 });

    inputElement.setValue(20);
    await nextTick();
    expect(wrapper.emitted()['update:modelValue'][0]).toStrictEqual([20]);
  });

  it('should not update if disabled', async () => {
    const { wrapper, inputElement } = factory({
      modelValue: 10,
      disabled: true,
    });

    inputElement.setValue(20);
    await nextTick();
    expect(wrapper.emitted()['update:modelValue']).toBeUndefined();
  });

  it('should try limit of the input when value change', async () => {
    const { wrapper, inputElement } = factory({
      modelValue: 8,
      disabled: true,
      max: 10,
      min: 5,
    });

    expect((wrapper.vm as any).internalErrors.length).toBe(0);
    await wrapper.setProps({ modelValue: 2 });
    await nextTick();

    expect((wrapper.vm as any).internalErrors.length).toBe(1);
    expect((wrapper.vm as any).internalErrors[0]).toBe(
      'Valeur inférieure au minimum autorisé (min : 5)',
    );

    await wrapper.setProps({ modelValue: 15 });
    await nextTick();

    expect((wrapper.vm as any).internalErrors.length).toBe(1);
    expect((wrapper.vm as any).internalErrors[0]).toBe(
      'Valeur supérieure au maximum autorisé (max : 10)',
    );
  });
});
