|
| 1 | +import ComponentWithInput from '~resources/components/component-with-input.vue' |
| 2 | +import { describeWithShallowAndMount } from '~resources/utils' |
| 3 | + |
| 4 | +describeWithShallowAndMount('setChecked', (mountingMethod) => { |
| 5 | + it('sets element checked true with no option passed', () => { |
| 6 | + const wrapper = mountingMethod(ComponentWithInput) |
| 7 | + const input = wrapper.find('input[type="checkbox"]') |
| 8 | + input.setChecked() |
| 9 | + |
| 10 | + expect(input.element.checked).to.equal(true) |
| 11 | + }) |
| 12 | + |
| 13 | + it('sets element checked equal to param passed', () => { |
| 14 | + const wrapper = mountingMethod(ComponentWithInput) |
| 15 | + const input = wrapper.find('input[type="checkbox"]') |
| 16 | + |
| 17 | + input.setChecked(true) |
| 18 | + expect(input.element.checked).to.equal(true) |
| 19 | + |
| 20 | + input.setChecked(false) |
| 21 | + expect(input.element.checked).to.equal(false) |
| 22 | + }) |
| 23 | + |
| 24 | + it('updates dom with checkbox v-model', () => { |
| 25 | + const wrapper = mountingMethod(ComponentWithInput) |
| 26 | + const input = wrapper.find('input[type="checkbox"]') |
| 27 | + |
| 28 | + input.setChecked() |
| 29 | + expect(wrapper.text()).to.contain('checkbox checked') |
| 30 | + |
| 31 | + input.setChecked(false) |
| 32 | + expect(wrapper.text()).to.not.contain('checkbox checked') |
| 33 | + }) |
| 34 | + |
| 35 | + it('changes state the right amount of times with checkbox v-model', () => { |
| 36 | + const wrapper = mountingMethod(ComponentWithInput) |
| 37 | + const input = wrapper.find('input[type="checkbox"]') |
| 38 | + |
| 39 | + input.setChecked() |
| 40 | + input.setChecked(false) |
| 41 | + input.setChecked(false) |
| 42 | + input.setChecked(true) |
| 43 | + input.setChecked(false) |
| 44 | + input.setChecked(false) |
| 45 | + |
| 46 | + expect(wrapper.find('.counter').text()).to.equal('4') |
| 47 | + }) |
| 48 | + |
| 49 | + it('updates dom with radio v-model', () => { |
| 50 | + const wrapper = mountingMethod(ComponentWithInput) |
| 51 | + |
| 52 | + wrapper.find('#radioBar').setChecked() |
| 53 | + expect(wrapper.text()).to.contain('radioBarResult') |
| 54 | + |
| 55 | + wrapper.find('#radioFoo').setChecked() |
| 56 | + expect(wrapper.text()).to.contain('radioFooResult') |
| 57 | + }) |
| 58 | + |
| 59 | + it('changes state the right amount of times with checkbox v-model', () => { |
| 60 | + const wrapper = mountingMethod(ComponentWithInput) |
| 61 | + const radioBar = wrapper.find('#radioBar') |
| 62 | + const radioFoo = wrapper.find('#radioFoo') |
| 63 | + |
| 64 | + radioBar.setChecked() |
| 65 | + radioBar.setChecked() |
| 66 | + radioFoo.setChecked() |
| 67 | + radioBar.setChecked() |
| 68 | + radioBar.setChecked() |
| 69 | + radioFoo.setChecked() |
| 70 | + radioFoo.setChecked() |
| 71 | + |
| 72 | + expect(wrapper.find('.counter').text()).to.equal('4') |
| 73 | + }) |
| 74 | + |
| 75 | + it('throws error if checked param is not boolean', () => { |
| 76 | + const message = 'wrapper.setChecked() must be passed a boolean' |
| 77 | + shouldThrowErrorOnElement('input[type="checkbox"]', message, 'asd') |
| 78 | + }) |
| 79 | + |
| 80 | + it('throws error if checked param is false on radio element', () => { |
| 81 | + const message = 'wrapper.setChecked() cannot be called with parameter false on a <input type="radio" /> element.' |
| 82 | + shouldThrowErrorOnElement('#radioFoo', message, false) |
| 83 | + }) |
| 84 | + |
| 85 | + it('throws error if wrapper does not contain element', () => { |
| 86 | + const wrapper = mountingMethod({ render: (h) => h('div') }) |
| 87 | + const div = wrapper.find('div') |
| 88 | + div.element = null |
| 89 | + |
| 90 | + const fn = () => div.setChecked() |
| 91 | + const message = '[vue-test-utils]: cannot call wrapper.setChecked() on a wrapper without an element' |
| 92 | + expect(fn).to.throw().with.property('message', message) |
| 93 | + }) |
| 94 | + |
| 95 | + it('throws error if element is select', () => { |
| 96 | + const message = 'wrapper.setChecked() cannot be called on a <select> element. Use wrapper.setSelected() instead' |
| 97 | + shouldThrowErrorOnElement('select', message) |
| 98 | + }) |
| 99 | + |
| 100 | + it('throws error if element is text like', () => { |
| 101 | + const message = 'wrapper.setChecked() cannot be called on "text" inputs. Use wrapper.setValue() instead' |
| 102 | + shouldThrowErrorOnElement('input[type="text"]', message) |
| 103 | + }) |
| 104 | + |
| 105 | + it('throws error if element is not valid', () => { |
| 106 | + const message = 'wrapper.setChecked() cannot be called on this element' |
| 107 | + shouldThrowErrorOnElement('#label-el', message) |
| 108 | + }) |
| 109 | + |
| 110 | + function shouldThrowErrorOnElement (selector, message, value) { |
| 111 | + const wrapper = mountingMethod(ComponentWithInput) |
| 112 | + const input = wrapper.find(selector) |
| 113 | + |
| 114 | + const fn = () => input.setChecked(value) |
| 115 | + expect(fn).to.throw().with.property('message', '[vue-test-utils]: ' + message) |
| 116 | + } |
| 117 | +}) |
0 commit comments