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

Testig window/document events #230

Closed
apertureless opened this issue Dec 5, 2017 · 5 comments
Closed

Testig window/document events #230

apertureless opened this issue Dec 5, 2017 · 5 comments

Comments

@apertureless
Copy link

Maybe I missed something, but is there a way to test EventListeners?
I have following component

export default {
  props: { ..... }, 
 created () {
    if (this.closeOnEsc) { // boolean prop.
      window.addEventListener('keydown', this._closeOnEsc)
    }
  },
  beforeDestroy () {
    if (this.closeOnEsc) {
      window.removeEventListener('keydown', this._closeOnEsc)
    }
  },
  methods: {
    close () {
      this.$emit('close')
    },
    _closeOnEsc (e) {
      if (this.isOpen && e.keyCode === 27) {
        this.close()
      }
    }
  }
}

It is working in storybook so far, that if you set the prop to true and press esc that the close event is emitted.

It works also if I test against the button which has @click="close()"

Here are the tests

it('should emit close event on button click', () => {
    const wrapper = shallow(Modal, {
      propsData: {
        isOpen: true
      }
    })
    expect(wrapper.props().isOpen).toBe(true)
    wrapper.find('.modal-close').trigger('click')
    expect(wrapper.emitted().close).toBeTruthy()
  })

  it('should emit close event on esc', () => {
    const wrapper = shallow(Modal, {
      propsData: {
        isOpen: true,
        closeOnEsc: true
      }
    })
    expect(wrapper.props().closeOnEsc).toBe(true)
    wrapper.trigger('keydown.esc')
    expect(wrapper.emitted().close).toBeTruthy()
  })

The first test with the button is working. However not the second one.

@eddyerburgh
Copy link
Member

Hi, thanks for the detailed bug report.

This is related to this bug — #215. It should be fixed in the next beta release 😀

@apertureless
Copy link
Author

Awesome! Thanks! 🙏

@wtho
Copy link
Contributor

wtho commented Dec 9, 2017

I am not sure if it was related to that issue, but the following example works for me:

it('should emit close event on esc', () => {
  const component = {
    created () {
      window.addEventListener('keydown', this._closeOnEsc)
    },
    methods: {
      close () {
        this.$emit('close')
      },
      _closeOnEsc (e) {
        if (e.keyCode === 27) {
          this.close()
        }
      }
    }
  }

  const wrapper = mount(component, {
    attachToDocument: true
  })
  wrapper.trigger('keydown.esc')
  expect(wrapper.emitted().close).to.be.ok
})

@apertureless make sure you use attachToDocument: true when working with window and DOM-Events.

@apertureless
Copy link
Author

Much thanks @wtho ! 🙏
attachToDocument did the trick.

@luckylooke
Copy link
Contributor

attachToDocument: true did the thing, but it is deprecated, now it is:

		attachTo: document.body

but anyway, it points me to it, thanks 🙏🙂

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

No branches or pull requests

4 participants