mock-echo
Mocking framework for Laravel Echo
Installation
npm i mock-echo
Usage
Import mock-echo
import MockEcho from 'mock-echo'
Replace global object Echo
with new MockEcho()
before every unit test
let mockEcho
Remember to delete global object Echo
after every unit test
Determine whether channel has been listened
You can use channelExist
, privateChannelExist
or presenceChannelExist
to determine whether channel has been listened:
channelExist
: channelprivateChannelExist
: private channelpresenceChannelExist
: presence channel
Example:
All examples are using
expect
as assertion library
Get mock channel object
You can use getChannel
, getPrivateChannel
, getPresenceChannel
to get mock channel object
getChannel
: channelgetPrivateChannel
: private channelgetPresenceChannel
: presence channel
Mock channel object has functions eventExist
, broadcast
, etc.
Example:
Determine whether event has been listened
You can use getChannel(channelName).eventExist
, getPrivateChannel(channelName).eventExist
or getPresenceChannel(channelName).eventExist
to determine whether event has been listened:
getChannel(channelName).eventExist
: channelgetPrivateChannel(channelName).eventExist
: private channelgetPresenceChannel(channelName).eventExist
: presence channel
Example:
Broadcast event
You can use broadcast
to broadcast an event.
Note: If you are using
vue-test-utils
, call$nextTick
before assertion.
Example:
mockEchowrappervm
Presence channel actions
You can use iJoin
, userJoin
, userLeave
to trigger presence channel actions:
iJoin
: triggerhere
listeneruserJoin
: triggerjoining
listner. It will returnsubId
after callinguserJoin
. You can use thissubId
to get this user away from this channel.userLeave
: triggerleaving
listner. UsesubId
which got fromuserJoin
to get this user away from this channel.
Note: If you are using
vue-test-utils
, call$nextTick
before assertion.
Example:
mockEchowrappervm // You will need paulSubId to get this user away from this channellet paulSubId = mockEchowrappervm mockEchowrappervm
Client events
You can use whisper
to send user event. Only private channel object and presence channel object have whisper
.
Note: If you are using
vue-test-utils
, call$nextTick
before assertion.
Example:
// private channelmockEchowrappervm // presence channelmockEchowrappervm
If you found any bugs
Please create the issue
可以用中文