Skip to content

Commit 0795066

Browse files
author
Alex Yang
committed
change version to 0.0.2, add README.md
1 parent 4ce9953 commit 0795066

File tree

2 files changed

+149
-2
lines changed

2 files changed

+149
-2
lines changed

README.md

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,149 @@
11
# mock-echo
2-
Mock laravel Echo
2+
Mocking framework claravel Echo
3+
4+
# install
5+
6+
`npm i mock-echo`
7+
8+
# Usage
9+
10+
> Use `expect` as assertion library
11+
12+
Import mock-echo
13+
`import MockEcho from 'mock-echo'`
14+
15+
Replace global object `Echo` with `new MockEcho()` before every unit test
16+
```javascript
17+
beforeEach(() => {
18+
mockEcho = new MockEcho()
19+
global.Echo = mockEcho
20+
})
21+
```
22+
23+
Remember to delete global object `Echo` after every unit test
24+
```javascript
25+
afterEach(() => {
26+
delete global.Echo
27+
})
28+
```
29+
30+
## Determine whether channel has been listened
31+
32+
You can use `channelExist`, `privateChannelExist` or `presenceChannelExist` to determine whether channel has been listened:
33+
34+
* `channelExist`: channel
35+
* `privateChannelExist`: private channel
36+
* `presenceChannelExist`: presence channel
37+
38+
Example:
39+
```javascript
40+
expect(mockEcho.channelExist('news')).toBe(true)
41+
expect(mockEcho.privateChannelExist('meeting')).toBe(true)
42+
expect(mockEcho.presenceChannelExist('chat')).toBe(true)
43+
```
44+
45+
## Get mock channel object
46+
47+
You can use `getChannel`, `getPrivateChannel`, `getPresenceChannel` to get mock channel object
48+
49+
* `getChannel`: channel
50+
* `getPrivateChannel`: private channel
51+
* `getPresenceChannel`: presence channel
52+
53+
Mock channel object has functions `eventExist`, `broadcast`, etc.
54+
55+
Example:
56+
```javascript
57+
expect(mockEcho.getChannel('news').eventExist('NewsMessage')).toBe(true)
58+
```
59+
60+
## Determine whether event has been listened
61+
62+
You can use `getChannel(channelName).eventExist`, `getPrivateChannel(channelName).eventExist` or `getPresenceChannel(channelName).eventExist` to determine whether event has been listened:
63+
64+
* `getChannel(channelName).eventExist`: channel
65+
* `getPrivateChannel(channelName).eventExist`: private channel
66+
* `getPresenceChannel(channelName).eventExist`: presence channel
67+
68+
Example:
69+
```javascript
70+
expect(mockEcho.getChannel('news').eventExist('NewsMessage')).toBe(true)
71+
expect(mockEcho.getPrivateChannel('meeting').eventExist('MeetingMessage')).toBe(true)
72+
expect(mockEcho.getPresenceChannel('chat').eventExist('ChatMessage')).toBe(true)
73+
```
74+
75+
## Broadcast event
76+
77+
You can use `broadcast` to broadcast an event.
78+
79+
> Note: If you are using `vue-test-utils`, call `$nextTick` before assertion.
80+
81+
Example:
82+
```javascript
83+
mockEcho.getChannel('news').broadcast('NewsMessage', { message: 'Hello World' })
84+
wrapper.vm.$nextTick(() => {
85+
expect(wrapper.find('.message').text()).toBe('It said Hello World')
86+
done()
87+
})
88+
```
89+
90+
## Presence channel actions
91+
92+
You can use `iJoin`, `userJoin`, `userLeave` to trigger presence channel actions:
93+
94+
* `iJoin`: trigger `here` listener
95+
* `userJoin`: trigger `joining` listner. It will return `subId` after calling `userJoin`. You can use this `subId` to get this user away from this channel.
96+
* `userLeave`: trigger `leaving` listner. Use `subId` which got from `userJoin` to get this user away from this channel.
97+
98+
> Note: If you are using `vue-test-utils`, call `$nextTick` before assertion.
99+
100+
Example:
101+
```javascript
102+
mockEcho.getPresenceChannel('chat').iJoin({id: 1, name: 'Alex'})
103+
wrapper.vm.$nextTick(() => {
104+
expect(wrapper.find('.here-message').text()).toBe('There are 1 users')
105+
done()
106+
})
107+
108+
// You will need paulSubId to get this user away from this channel
109+
let paulSubId = mockEcho.getPresenceChannel('chat').userJoin({id: 2, name: 'Paul'})
110+
wrapper.vm.$nextTick(() => {
111+
expect(wrapper.find('.join-message').text()).toBe('Paul joined')
112+
done()
113+
})
114+
115+
mockEcho.getPresenceChannel('chat').userLeave(paulSubId)
116+
wrapper.vm.$nextTick(() => {
117+
expect(wrapper.find('.leave-message').text()).toBe('Paul leaved')
118+
done()
119+
})
120+
```
121+
122+
## Client events
123+
124+
You can use `userWhisper` to send user event. Only private channel object and presence channel object have `userWhisper`.
125+
126+
> Note: If you are using `vue-test-utils`, call `$nextTick` before assertion.
127+
128+
Example:
129+
```javascript
130+
// private channel
131+
mockEcho.getPrivateChannel('meeting').userWhisper('meetingClicking', { username: username })
132+
wrapper.vm.$nextTick(() => {
133+
expect(wrapper.find('.whisper-message').text()).toBe(`${username} is clicking the button`)
134+
done()
135+
})
136+
137+
// presence channel
138+
mockEcho.getPresenceChannel('chat').userWhisper('chatClicking', { username: username })
139+
wrapper.vm.$nextTick(() => {
140+
expect(wrapper.find('.whisper-message').text()).toBe(`${username} is clicking the button`)
141+
done()
142+
})
143+
```
144+
145+
# If you found any bugs
146+
147+
Please create the issue
148+
149+
可以用中文

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mock-echo",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "Mock laravel Echo",
55
"main": "dist/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)