From 29250da726635d881d10a063c4aec14c98343716 Mon Sep 17 00:00:00 2001 From: mugi-uno Date: Wed, 18 Mar 2020 11:26:28 +0900 Subject: [PATCH] Fix Emit Decorator with multiple arguments --- src/vue-property-decorator.ts | 10 ++++- tests/Emit.spec.ts | 71 +++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/src/vue-property-decorator.ts b/src/vue-property-decorator.ts index 0199967..e7f0ea8 100644 --- a/src/vue-property-decorator.ts +++ b/src/vue-property-decorator.ts @@ -274,10 +274,16 @@ export function Emit(event?: string) { } else if (args.length === 1) { this.$emit(emitName, args[0]) } else { - this.$emit(emitName, args) + this.$emit(emitName, ...args) } } else { - this.$emit(emitName, returnValue) + if (args.length === 0) { + this.$emit(emitName, returnValue) + } else if (args.length === 1) { + this.$emit(emitName, returnValue, args[0]) + } else { + this.$emit(emitName, returnValue, ...args) + } } } diff --git a/tests/Emit.spec.ts b/tests/Emit.spec.ts index af6c84c..bcb4a0d 100644 --- a/tests/Emit.spec.ts +++ b/tests/Emit.spec.ts @@ -61,6 +61,77 @@ describe(Emit, () => { expect(mockFn.mock.calls[0][1]).toBe(value) }) }) + + describe('when multiple arguments is given', () => { + @Component + class ChildComponent extends Vue { + count = 0 + + @Emit() increment(n1: number, n2: number) { + this.count += n1 + n2 + } + } + + const child = new ChildComponent() + const mockFn = jest.fn() + child.$emit = mockFn + + const value1 = 30 + const value2 = 40 + + beforeAll(() => { + child.increment(value1, value2) + }) + + test('call $emit method', () => { + expect(mockFn).toHaveBeenCalled() + }) + + test('emit event with method name', () => { + expect(mockFn.mock.calls[0][0]).toBe('increment') + }) + + test('emit event with multiple arguments', () => { + expect(mockFn.mock.calls[0][1]).toBe(value1) + expect(mockFn.mock.calls[0][2]).toBe(value2) + }) + }) + + describe('when the value is returned and multiple arguments is given', () => { + @Component + class ChildComponent extends Vue { + count = 0 + + @Emit() increment(n1: number, n2: number) { + return n1 + n2; + } + } + + const child = new ChildComponent() + const mockFn = jest.fn() + child.$emit = mockFn + + const value1 = 30 + const value2 = 40 + + beforeAll(() => { + child.increment(value1, value2) + }) + + test('call $emit method', () => { + expect(mockFn).toHaveBeenCalled() + }) + + test('emit event with method name', () => { + expect(mockFn.mock.calls[0][0]).toBe('increment') + }) + + test('emit event with multiple arguments', () => { + expect(mockFn.mock.calls[0][1]).toBe(value1 + value2) + expect(mockFn.mock.calls[0][2]).toBe(value1) + expect(mockFn.mock.calls[0][3]).toBe(value2) + }) + }) describe('when promise has been returned', () => { const value = 10