Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn about v-model components not handled with fireEvent.change #83

Closed
afontcu opened this issue Aug 13, 2019 · 17 comments
Closed

Warn about v-model components not handled with fireEvent.change #83

afontcu opened this issue Aug 13, 2019 · 17 comments
Labels
enhancement New feature or request

Comments

@afontcu
Copy link
Member

afontcu commented Aug 13, 2019

More often than not people relies on fireEvent.input or fireEvent.change to handle v-model powered inputs, thus running into issues with input value not being properly updated.

I'm wondering if we should provide some kind of warning when that happens.

Does it make sense?

@afontcu afontcu changed the title Warn about v-mode components not handled with fireEvent.change Warn about v-model components not handled with fireEvent.change Aug 14, 2019
@afontcu afontcu added the enhancement New feature or request label Aug 14, 2019
@afontcu afontcu added the good first issue Good for newcomers label Oct 16, 2019
@chiptus
Copy link
Contributor

chiptus commented Nov 4, 2019

Do we have docs about it? I'm currently struggling with updating a custom component in a form using fireEvent.update

@chiptus
Copy link
Contributor

chiptus commented Nov 4, 2019

is fireEvent.update working with custom elements? like if I have a custom select box? in your examples you find the select component, but in the end, it's just a regular select. I'm trying to understand if all fireEvent.update expect is a component which can be used with v-model (because in my examples, it doesn't work)

@afontcu
Copy link
Member Author

afontcu commented Nov 4, 2019

If that's the case, would you mind opening up a new issue with a reproduction link? All fireEvent.update does is guessing the input type it was called upon, and update its value accordingly.

Thanks!! 🙌

@chiptus
Copy link
Contributor

chiptus commented Nov 4, 2019

If I understand the code for fireEvent, this function just fires native events, right? if I want to update a component, I should update it as the user does, for example in my selector, I would click open, and choose the right value. right?

@afontcu
Copy link
Member Author

afontcu commented Nov 4, 2019

this function just fires native events
Yes, it is aligned with the guiding principle of Testing Library: keeping things as close as how your software is used.

if I want to update a component, I should update it as the user does, for example in my selector, I would click open, and choose the right value. right?
Exactly! Interact with your component as any user would (or, well, as similar as possible).


fireEvent.update goes the extra mile and helps you with common use cases for v-model, but I'm not 100% positive it would handle complex scenarios with custom events and compound components. If you find yourself stuck because of that, please share an example so we can work on it :)

@chiptus
Copy link
Contributor

chiptus commented Nov 5, 2019

I actually found a very nice solution, I'll try to post an example

@ChamNouki
Copy link

ChamNouki commented Mar 2, 2020

I've got an issue that i think is related to this one.

I've got a custom input file component that use v-file-input from vuetify. My component emit input event to give selected file to the parent component through v-model. It works perfectly in browser but emitted() does not see any event.

Here is my code :

<template>
  <v-file-input
    accept=".xlsx"
    label="Matrice certifié nego"
    name="matrix"
    prepend-icon="mdi-file-table"
    :rules="[required]"
    @change="change"
  ></v-file-input>
</template>

<script lang="ts">
import Vue from 'vue';
export default Vue.extend({
  name: 'MatriceInputFile',
  props: {
    value: File
  },
  methods: {
    required(value: File): boolean | string {
      return (
        value !== undefined ||
        'Vous devez fournir une matrice de négociation tarrifaire.'
      );
    },
    change(value: File) {
      this.$emit('input', value);
    }
  }
});
</script>
export const renderWithVuetify = <V extends Vue>(
  component: VueClass<V>,
  options?: RenderOptions<V>,
  callback?: ConfigurationCallback<V>
) => {
  return render(
    // anonymous component
    Vue.extend({
      // Vue's render function
      render(createElement) {
        // add data-app="true" attribut and render the test component
        return createElement(component, { attrs: { 'data-app': true } });
      }
    }),
    // for Vuetify components that use the $vuetify instance property
    { vuetify: new Vuetify(), ...options },
    callback
  );
};

async function selectFile(inputFile: HTMLInputElement): Promise<void> {
    const file = new File(['(⌐□_□)'], 'chucknorris.png', {
      type: 'image/png'
    });

    await fireEvent.focus(inputFile);
    await fireEvent.change(inputFile, { target: { files: [file] } });
    await fireEvent.blur(inputFile);
}

it('should get selected file to parent component', async () => {
    const { getByLabelText, emitted } = renderWithVuetify(MatriceInputFile);

    const inputFile = getByLabelText(
      'Matrice certifié nego'
    ) as HTMLInputElement;
    await selectFile(inputFile);

    expect(emitted()['input']).toBeDefined();
  });

@jameesjohn
Copy link

I noticed this while testing a component too.
I think having a warning of some sort will be great.

@jhack32
Copy link
Contributor

jhack32 commented Oct 27, 2020

@afontcu - Is this something that needs to be done? I can give it a try

@afontcu
Copy link
Member Author

afontcu commented Oct 27, 2020

Hi @jhack32! Yeah sure! 🚀 I think it'd be great to add a warning message when using typing events on elements that we know may cause unexpected issues, and suggest people to use update instead :)

afontcu pushed a commit that referenced this issue Nov 17, 2020
….update() (#166)

* Add warning message to fireEvent input and change when called directly #83

* Add user event test case

* Reword warning message to include used event key

* Fix template literal for warning messages
@JeromeDeLeon
Copy link
Contributor

Should it trigger a warning for the input file?

@ChamNouki
Copy link

since issue occurred with input file too, it seems logic to me to trigger warning on input file too ?

@JeromeDeLeon
Copy link
Contributor

Then we wouldn't be able to do this fireEvent.change(inputFile, { target: { files: [file] } }); because the code uses element.value = value

@afontcu
Copy link
Member Author

afontcu commented Nov 20, 2020

Hi! I'm not entirely sure what's the suggestion with input file – can't you simulate a file uploading using fireEvent.update()?

@afontcu afontcu removed the good first issue Good for newcomers label Nov 20, 2020
@afontcu
Copy link
Member Author

afontcu commented Nov 20, 2020

Since this was released in #166, I'll go ahead and close this issue, feel free to open up a new one if we're missing some warnings!

thank you all!

@afontcu afontcu closed this as completed Nov 20, 2020
@adnan-razzaq
Copy link

I am trying to use this for vuetify text field and nothing seems to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

7 participants