Skip to content

Commit

Permalink
Split up error logging for dispatch_event into two arguments
Browse files Browse the repository at this point in the history
Also replace `sinon.fake()` with `sinon.spy()` in `dispatch_event` tests
  • Loading branch information
marcoroth committed Nov 5, 2023
1 parent 7f60562 commit 4374d1c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/actions/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export function dispatch_event(this: StreamElement) {
}
} catch (error: any) {
console.error(
`[TurboPower] error proccessing provided "detail" in "<template>" ("${template}") for Turbo Streams operation "dispatch_event". Error: "${error.message}"`,
`[TurboPower] error proccessing provided "detail" in "<template>" ("${template}") for Turbo Streams operation "dispatch_event".`,
`Error: "${error.message}"`,
)
}
}
Expand Down
37 changes: 21 additions & 16 deletions test/events/dispatch_event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,53 @@ describe("dispatch_event", () => {
})

it("should do nothing and print warning if attribute is empty", async () => {
sinon.replace(console, "warn", sinon.fake())
const spy = sinon.spy(console, "warn")

await fixture('<div id="element"></div>')

const expectedWarning = '[TurboPower] no "name" provided for Turbo Streams operation "dispatch_event"'

assert(!console.warn.calledWith(expectedWarning), `console.warn wasn't called with "${expectedWarning}"`)
assert.equal(spy.callCount, 0)

await executeStream('<turbo-stream action="dispatch_event" name="" target="element"></turbo-stream>')

assert(console.warn.calledWith(expectedWarning), `console.warn wasn't called with "${expectedWarning}"`)
assert.equal(spy.callCount, 1)
assert.equal(
spy.firstCall.firstArg,
'[TurboPower] no "name" provided for Turbo Streams operation "dispatch_event"'
)
})

it('should do nothing and print warning if "attribute" attribute is missing', async () => {
sinon.replace(console, "warn", sinon.fake())
const spy = sinon.spy(console, "warn")

await fixture('<div id="element"></div>')

const expectedWarning = '[TurboPower] no "name" provided for Turbo Streams operation "dispatch_event"'

assert(!console.warn.calledWith(expectedWarning), `console.warn wasn't called with "${expectedWarning}"`)
assert.equal(spy.callCount, 0)

await executeStream('<turbo-stream action="dispatch_event" target="element"></turbo-stream>')

assert(console.warn.calledWith(expectedWarning), `console.warn wasn't called with "${expectedWarning}"`)
assert.equal(spy.callCount, 1)
assert.equal(
spy.firstCall.firstArg,
'[TurboPower] no "name" provided for Turbo Streams operation "dispatch_event"'
)
})

it("should error out when detail is not valid json", async () => {
sinon.replace(console, "error", sinon.fake())
const spy = sinon.spy(console, "error")

await fixture('<div id="element"></div>')

const expectedWarning =
'[TurboPower] error proccessing provided "detail" in "<template>" ("{ this is not valid }") for Turbo Streams operation "dispatch_event". Error: "Expected property name or \'}\' in JSON at position 2 (line 1 column 3)"'

assert(!console.error.calledWith(expectedWarning), `console.error wasn't called with "${expectedWarning}"`)
assert.equal(spy.callCount, 0)

await executeStream(
'<turbo-stream action="dispatch_event" name="my:event" target="element"><template>{ this is not valid }</template></turbo-stream>',
)

assert(console.error.calledWith(expectedWarning), `console.error wasn't called with "${expectedWarning}"`)
assert.equal(spy.callCount, 1)
assert.equal(
spy.firstCall.firstArg,
'[TurboPower] error proccessing provided "detail" in "<template>" ("{ this is not valid }") for Turbo Streams operation "dispatch_event".',
)
})
})

Expand Down

0 comments on commit 4374d1c

Please sign in to comment.