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

Commit

Permalink
Fix Emit Decorator with multiple arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
mugi-uno committed Apr 11, 2020
1 parent a5fd1aa commit 29250da
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
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

0 comments on commit 29250da

Please sign in to comment.