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

Unexpected behaviour when watching #502

Closed
tbuteler opened this issue Mar 30, 2018 · 5 comments
Closed

Unexpected behaviour when watching #502

tbuteler opened this issue Mar 30, 2018 · 5 comments
Labels

Comments

@tbuteler
Copy link

tbuteler commented Mar 30, 2018

Version

1.0.0-beta.13

Reproduction link

https://github.com/tbuteler/vue-test-utils-issue

Steps to reproduce

Clone the repository, then run:

yarn install
yarn run tests

What is expected?

Component:

<template>
  <div id="app">
    <div>
      <pre>data.text: <em>{{ text }}</em></pre>
    </div>
    <!-- Tests fail in 1.0.0-beta.13 with .fails portion of the code -->
    <div class="fails">
      <pre>computed.text: <em>{{ computedText }}</em></pre>
    </div>
  </div>
</template>

<script>
export default {
  name: 'app',
  data () {
    return {
      text: '',
      basket: []
    }
  },
  computed: {
    computedText () {
      return this.text
    }
  },
  watch: {
    text () {
      this.basket.push(this.computedText)
    }
  }
}
</script>

Test:

import { shallow } from '@vue/test-utils'
import App from '../src/App.vue'

describe('App.vue', () => {
  it('updates computed properties', () => {
    const wrapper = shallow(App)
    expect(wrapper.vm.text).toBe('')
    expect(wrapper.vm.basket).toEqual([])
    wrapper.setData({ text: 'foo' })
    expect(wrapper.vm.text).toBe('foo')
    expect(wrapper.vm.computedText).toBe('foo')
    expect(wrapper.vm.basket).toEqual(['foo']) // Fails
  })
})

In the text watch handler, I expected the computed property to have been updated to the latest value of text.

What is actually happening?

The computed property is somehow lagging behind. Curiously, this only happens when the computed property is actually being rendered inside the template, otherwise it behaves as expected.


This issue was not present in 1.0.0-beta.12, regardless of what the template contains. From the release information, I would suspect it has something to do with the new sync functionality.

@eddyerburgh
Copy link
Member

This is a bug caused by dependencies running in the incorrect order. I've created a fix that will be released in the next version.

For the moment, you will need to set sync to false and use Vue.nextTick:

it('updates computed properties', (done) => {
  const wrapper = shallow(App, {
    sync: false
  })
  Vue.nextTick(() => {
    expect(wrapper.vm.basket).toEqual(['foo']) // Fails
    done()
  })
})

Alternatively, you can use beta.12.

@tbuteler
Copy link
Author

Thanks for the quick response, Edd.

I ran your solution locally and though it fixes the original reproduction repository's test, my own tests were still failing. I've pushed an update to the reproduction repository with a bit more code which causes it to fail again. Namely, showing the content conditionally on a user click, then attaching the test component to the document with attachToDocument: true.

(For context, I'm testing a searchable dropdown component, so the test above mimics this behaviour).

What this seems to do is trigger the watcher once on click, before the setData will trigger it again. That earlier trigger seems to cause the computed property to fail to update (as before the fix).

@tbuteler
Copy link
Author

Should I open a new issue? Or is the second scenario something that falls outside the use-case and won't be fixed?

@eddyerburgh
Copy link
Member

eddyerburgh commented Apr 2, 2018

This should be fixed in #510

@tbuteler
Copy link
Author

Tested this today with my actual suite and its components, which are far more complex than the examples above and everything worked as expected. Thanks for your effort!

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

No branches or pull requests

2 participants