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

Dynamic classes not added correctly to wrapper classes when using shallowMount #1474

Closed
kierans opened this issue Mar 18, 2020 · 7 comments · Fixed by #1476
Closed

Dynamic classes not added correctly to wrapper classes when using shallowMount #1474

kierans opened this issue Mar 18, 2020 · 7 comments · Fixed by #1476
Labels

Comments

@kierans
Copy link

kierans commented Mar 18, 2020

Version

1.0.0-beta.32

Reproduction link

https://github.com/kierans/vue-test-utils-bug

Steps to reproduce

  1. Create a SFC called Card.vue
<template>
  <v-card
    outlined
    flat
  >
    <v-list-item>
      <v-list-item-avatar
          color="grey"
      />
      <v-list-item-content>
        <v-list-item-title>{{ contents.title }}</v-list-item-title>
        <v-list-item-subtitle>
          {{ contents.subtitle }}
        </v-list-item-subtitle>
      </v-list-item-content>
    </v-list-item>

    <v-card-text
        class="subtitle-1"
        v-bind:class="{
        'negative-amount': contents.amount < 0
      }"
    >
      {{ contents.amount }}
    </v-card-text>
  </v-card>
</template>

<script>
export default {
  name: "Card",
  data() {
    return {
      contents: {
        title: "Title",
        subtitle: "Subtitle",
        amount: -10
      }
    };
  }
}
</script>
  1. Write the following test code
wrapper = shallowMount(Card);

const text: Wrapper<Vue> = wrapper.find(VCardText as ComponentOptions<Vue>);
expect(text.classes("negative-amount"), "Amount not marked as negative").to.be.true;

What is expected?

That the classes array/string will contain the value "negative-amount"

What is actually happening?

The classes array/string contains "[object Object]"


When running the app, the Card is correctly rendered with the "negative-amount" class being added.

@lmiller1990
Copy link
Member

lmiller1990 commented Mar 18, 2020

Not sure why this isn't working. Will need to look into it.

For now you could use mount. That way the entire tree will be evaluated. Probably the best option here, so you can test the entire thing works together, as expected.

@lmiller1990
Copy link
Member

Edit: This is working for me:

<template>
  <div>
    <h1>Hello world</h1>
    <Bar id="my-bar" class="bar-class" :class="{ 'is-bar': isBar }" />
    <Bar id="my-bar-with-slot">
      <Qux :class="{ 'is-qux': isQux }" />
    </Bar>
  </div>
</template>

<script>
import Bar from './Bar.vue'
import Qux from './Qux.vue'

export default {
  name: 'Foo',

  data() {
    return {
        isBar: true,
        isQux: true
    }
  },

  components: {
    Bar,
    Qux
  } 
}
</script>

Gives me

<div>
      <h1>Hello world</h1>
      <bar-stub id="my-bar" class="bar-class is-bar"></bar-stub>
      <bar-stub id="my-bar-with-slot">
        <qux-stub class="is-qux"></qux-stub>
      </bar-stub>
    </div>

I wonder what's different here? Hm.

@lmiller1990
Copy link
Member

Edit: ok, this happens when the component is a functional component. v-card-text is a functional component.

@lmiller1990
Copy link
Member

lmiller1990 commented Mar 18, 2020

Problem is it is stringifed here

function createClassString(staticClass, dynamicClass) {

@lmiller1990
Copy link
Member

lmiller1990 commented Mar 18, 2020

Fixed! Turn around time of 6h, not to shabby. @kierans, Quick review? #1476

@kierans
Copy link
Author

kierans commented Mar 18, 2020

Ship it! 👏

@palpich
Copy link
Contributor

palpich commented Dec 2, 2020

Unfortunately, #1476 don't fix dynamic class as Array.

I did fix (also fix events binding), can you review, @lmiller1990 ?

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

Successfully merging a pull request may close this issue.

3 participants