From b600bbc8467a3bef3605ef9150a195341b5c4061 Mon Sep 17 00:00:00 2001 From: Martin Beyer Date: Tue, 24 Apr 2018 20:21:03 -0300 Subject: [PATCH 1/7] added setValue method and few tests --- packages/test-utils/src/wrapper.js | 8 +++++++ .../components/component-with-input.vue | 16 +++++++++++++ test/specs/wrapper/setValue.spec.js | 23 +++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 test/resources/components/component-with-input.vue create mode 100644 test/specs/wrapper/setValue.spec.js diff --git a/packages/test-utils/src/wrapper.js b/packages/test-utils/src/wrapper.js index 790904ce3..828119392 100644 --- a/packages/test-utils/src/wrapper.js +++ b/packages/test-utils/src/wrapper.js @@ -531,6 +531,14 @@ export default class Wrapper implements BaseWrapper { this.vnode = this.vm._vnode } + /** + * Sets input value + */ + setValue (value: string) { + this.element.value = value // input element value is changed, v-model is not + this.trigger('input') + } + /** * Return text of wrapper element */ diff --git a/test/resources/components/component-with-input.vue b/test/resources/components/component-with-input.vue new file mode 100644 index 000000000..8bf4c2f87 --- /dev/null +++ b/test/resources/components/component-with-input.vue @@ -0,0 +1,16 @@ + + + diff --git a/test/specs/wrapper/setValue.spec.js b/test/specs/wrapper/setValue.spec.js new file mode 100644 index 000000000..9fade47ca --- /dev/null +++ b/test/specs/wrapper/setValue.spec.js @@ -0,0 +1,23 @@ +import ComponentWithInput from '~resources/components/component-with-input.vue' +import { describeWithShallowAndMount } from '~resources/utils' + +describeWithShallowAndMount('setValue', (mountingMethod) => { + it('sets element value', () => { + const wrapper = mountingMethod(ComponentWithInput) + const input = wrapper.find('input') + input.setValue('foo') + + expect(input.element.value).to.equal('foo') + }) + + it('calls trigger with input', () => { + const wrapper = mountingMethod(ComponentWithInput) + const input = wrapper.find('input') + sinon.spy(input, 'trigger') + + input.setValue('foo') + + expect(input.trigger.called).to.equal(true) + expect(input.trigger.calledWith('input')).to.equal(true) + }) +}) From a955decd8fa3b20869abb0d9426f7c852be53221 Mon Sep 17 00:00:00 2001 From: Martin Beyer Date: Sun, 29 Apr 2018 14:15:49 -0300 Subject: [PATCH 2/7] Added setChecked --- flow/wrapper.flow.js | 2 + packages/test-utils/src/wrapper.js | 77 ++++++++++++++- packages/test-utils/types/index.d.ts | 3 + .../components/component-with-input.vue | 25 ++++- test/specs/wrapper/setChecked.spec.js | 98 +++++++++++++++++++ test/specs/wrapper/setValue.spec.js | 54 +++++++++- 6 files changed, 247 insertions(+), 12 deletions(-) create mode 100644 test/specs/wrapper/setChecked.spec.js diff --git a/flow/wrapper.flow.js b/flow/wrapper.flow.js index 3ceb4f6f8..fb56b79c2 100644 --- a/flow/wrapper.flow.js +++ b/flow/wrapper.flow.js @@ -32,6 +32,8 @@ declare interface BaseWrapper { // eslint-disable-line no-undef setData(data: Object): void, setComputed(computed: Object): void, setMethods(methods: Object): void, + setValue(value: any): void, + setChecked(checked: boolean): void, setProps(data: Object): void, trigger(type: string, options: Object): void, destroy(): void diff --git a/packages/test-utils/src/wrapper.js b/packages/test-utils/src/wrapper.js index 828119392..9231c62da 100644 --- a/packages/test-utils/src/wrapper.js +++ b/packages/test-utils/src/wrapper.js @@ -532,11 +532,80 @@ export default class Wrapper implements BaseWrapper { } /** - * Sets input value + * Sets element value and triggers input event */ - setValue (value: string) { - this.element.value = value // input element value is changed, v-model is not - this.trigger('input') + setValue (value: any) { + const el = this.element + + if (!el) { + throwError('cannot call wrapper.setValue() on a wrapper without an element') + } + + const tag = el.tagName + const type = this.attributes().type + const event = 'input' + + if (this.isVueComponent) { + } else if (tag === 'SELECT') { + // event = 'change' + // el.value = value + throwError('wrapper.setValue() cannot be called on select') + } else if (tag === 'INPUT' && type === 'checkbox') { + // event = 'change' + // el.checked = value + throwError('wrapper.setValue() cannot be called on checkbox. Use wrapper.setChecked() instead') + } else if (tag === 'INPUT' && type === 'radio') { + throwError('wrapper.setValue() cannot be called on radio. Use wrapper.setChecked() instead') + } else if (tag === 'INPUT' || tag === 'textarea') { + } else { + throwError('wrapper.setValue() cannot be called on this element') + } + + el.value = value + this.trigger(event) + } + + /** + * Sets element value and triggers input event + */ + setChecked (checked: boolean) { + console.log(typeof checked) + if (typeof checked !== 'undefined') { + if (typeof checked !== 'boolean') { + throwError('wrapper.setChecked() must be passed a boolean') + } + } else { + checked = true + } + + const el = this.element + + if (!el) { + throwError('cannot call wrapper.setChecked() on a wrapper without an element') + } + + const tag = el.tagName + const type = this.attributes().type + const event = 'change' + + if (this.isVueComponent) { + throwError('wrapper.setChecked() cannot be called on component. Use wrapper.setValue() instead') + } else if (tag === 'SELECT') { + throwError('wrapper.setChecked() cannot be called on select') + } else if (tag === 'INPUT' && type === 'checkbox') { + el.checked = checked + this.trigger(event) + } else if (tag === 'INPUT' && type === 'radio') { + if (!checked) { + throwError('wrapper.setChecked() cannot be called with parameter false on radio') + } + el.checked = true + this.trigger(event) + } else if (tag === 'INPUT' || tag === 'textarea') { + throwError('wrapper.setChecked() cannot be called on "text" inputs. Use wrapper.setValue() instead') + } else { + throwError('wrapper.setChecked() cannot be called on this element') + } } /** diff --git a/packages/test-utils/types/index.d.ts b/packages/test-utils/types/index.d.ts index 148029561..e1fb420fd 100644 --- a/packages/test-utils/types/index.d.ts +++ b/packages/test-utils/types/index.d.ts @@ -99,6 +99,9 @@ export interface Wrapper extends BaseWrapper { text (): string name (): string + setValue (value: any): void + setChecked (checked: boolean): void + emitted (event?: string): { [name: string]: Array> } emittedByOrder (): Array<{ name: string, args: Array }> } diff --git a/test/resources/components/component-with-input.vue b/test/resources/components/component-with-input.vue index 8bf4c2f87..635998a7e 100644 --- a/test/resources/components/component-with-input.vue +++ b/test/resources/components/component-with-input.vue @@ -1,15 +1,32 @@