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

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

  return { wrapper, inputElement };
};

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

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

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

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

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