Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Fix Emit Decorator with multiple arguments #306

Merged
merged 1 commit into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/vue-property-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down
71 changes: 71 additions & 0 deletions tests/Emit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down