Skip to content

Commit

Permalink
Port spec for Button (buefy#52)
Browse files Browse the repository at this point in the history
* test(lib): update Button spec

- Updates the unit tests (spec) of `Button` so that they can work with
  `@vue/test-utils` V2.
    - Replaces `wrapper.name()` with `wrapper.vm.$options.name` because
      `wrapper.name` no longer exists in V2.
    - Replaces the test of `wrapper.isVueInstance()` with that of
      presence of `wrapper.vm` because `wrapper.isVueInstance` no longer
      exists in V2.
    - Replaces `listeners.click` in the options for `shallowMount` with
      `props.onClick` because the interface of the options has changed
      in V2.
    - Replaces `wrapper.contains` with a combination of `wrapper.find`
      and `exists` because `wrapper.contains` no longer exists in V2.
    - Awaits `wrapper.setProps` to make sure new properties are applied
      before testing subsequent assertions.
    - Updates the snapshot. I did not see any problem with the new
      snapshot.

issue ntohq#1

* chore(lib): remove comment on Button.spec
  • Loading branch information
kikuomax committed Sep 2, 2023
1 parent b61c9a2 commit df4a9aa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
40 changes: 20 additions & 20 deletions src/components/button/Button.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { shallowMount, mount } from '@vue/test-utils'
import BButton from '@components/button/Button'
import config, {setOptions} from '@utils/config'
import config, { setOptions } from '@utils/config'

let wrapper

Expand All @@ -10,8 +10,8 @@ describe('BButton', () => {
})

it('is called', () => {
expect(wrapper.name()).toBe('BButton')
expect(wrapper.isVueInstance()).toBeTruthy()
expect(wrapper.vm).toBeTruthy()
expect(wrapper.vm.$options.name).toBe('BButton')
})

it('render correctly', () => {
Expand All @@ -21,8 +21,8 @@ describe('BButton', () => {
it('emit a click event', () => {
const click = jest.fn()
wrapper = shallowMount(BButton, {
listeners: {
'click': click
props: {
onClick: click
}
})
wrapper.find('.button').trigger('click')
Expand All @@ -31,15 +31,15 @@ describe('BButton', () => {

it('should show icon', () => {
wrapper = mount(BButton, {
propsData: {
props: {
iconLeft: 'plus'
}
})
expect(wrapper.contains('.icon')).toBe(true)
expect(wrapper.find('.icon').exists()).toBe(true)
})

it('should be medium', () => {
wrapper.setProps({
it('should be medium', async () => {
await wrapper.setProps({
size: 'is-medium'
})
expect(wrapper.classes()).toContain('is-medium')
Expand All @@ -53,7 +53,7 @@ describe('BButton', () => {
}
})
expect(wrapper.classes()).toContain('is-small')
expect(wrapper.contains('.icon')).toBe(true)
expect(wrapper.find('.icon').exists()).toBe(true)
})

it('should be large + icon', () => {
Expand All @@ -64,42 +64,42 @@ describe('BButton', () => {
}
})
expect(wrapper.classes()).toContain('is-large')
expect(wrapper.contains('.icon')).toBe(true)
expect(wrapper.find('.icon').exists()).toBe(true)
})

it('should be rounded when default config set to true', () => {
it('should be rounded when default config set to true', async () => {
setOptions(Object.assign(config, {
defaultButtonRounded: true
}))
wrapper.setProps({
await wrapper.setProps({
rounded: config.defaultButtonRounded
})
expect(wrapper.classes()).toContain('is-rounded')
})

it('should set tag to "button" if disabled', () => {
wrapper.setProps({
it('should set tag to "button" if disabled', async () => {
await wrapper.setProps({
tag: 'a'
})
expect(wrapper.vm.computedTag).toBe('a')

wrapper = shallowMount(BButton, {
attrs: {
'disabled': true
disabled: true
}
})
expect(wrapper.vm.computedTag).toBe('button')
})

it('should set type attribute', () => {
wrapper.setProps({
it('should set type attribute', async () => {
await wrapper.setProps({
nativeType: 'submit'
})
expect(wrapper.element.type).toBe('submit')
})

it("shouldn't set type attribute unless if the tag is button", () => {
wrapper.setProps({
it("shouldn't set type attribute unless if the tag is button", async () => {
await wrapper.setProps({
tag: 'a'
})
expect(wrapper.element.type).toBeFalsy()
Expand Down
9 changes: 5 additions & 4 deletions src/components/button/__snapshots__/Button.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`BButton render correctly 1`] = `
<button type="button" class="button">
<!---->
<!---->
<!----></button>
<button class="button" type="button">
<!--v-if-->
<!--v-if-->
<!--v-if-->
</button>
`;

0 comments on commit df4a9aa

Please sign in to comment.