Skip to content

Commit 69c7903

Browse files
committed
chore: wip
1 parent 86d3868 commit 69c7903

File tree

3 files changed

+120
-8
lines changed

3 files changed

+120
-8
lines changed

storage/framework/core/events/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ type StacksEvents = {
139139
'user:registered': object
140140
'user:logged-in': object
141141
'user:logged-out': object
142-
'user:updated': UserModel
143-
'user:created': UserModel
144-
'user:deleted': UserModel
142+
'user:updated': Partial<UserModel>
143+
'user:created': Partial<UserModel>
144+
'user:deleted': Partial<UserModel>
145145
'user:password-reset': object
146146
'user:password-changed': object
147147
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { describe, expect, mock, test } from 'bun:test'
2+
import type { UserModel } from '../../../orm/src/models/User'
3+
import { events, all, dispatch, listen, mitt, off, useEvent, useEvents } from '../src'
4+
5+
describe('@stacksjs/events', () => {
6+
test('mitt creates a functional event emitter', () => {
7+
const emitter = mitt<{ test: string }>()
8+
const handler = mock((data: string) => {})
9+
10+
emitter.on('test', handler)
11+
emitter.emit('test', 'hello')
12+
13+
expect(handler).toHaveBeenCalledWith('hello')
14+
})
15+
16+
test('off removes event listeners', () => {
17+
const emitter = mitt<{ test: string }>()
18+
const handler = mock((data: string) => {})
19+
20+
emitter.on('test', handler)
21+
emitter.off('test', handler)
22+
emitter.emit('test', 'hello')
23+
24+
expect(handler).not.toHaveBeenCalled()
25+
})
26+
27+
test('wildcard listeners receive all events', () => {
28+
const emitter = mitt<{ test1: string; test2: number }>()
29+
const handler = mock((type: string, data: any) => {})
30+
31+
emitter.on('*', handler)
32+
emitter.emit('test1', 'hello')
33+
emitter.emit('test2', 42)
34+
35+
expect(handler).toHaveBeenCalledTimes(2)
36+
expect(handler).toHaveBeenCalledWith('test1', 'hello')
37+
expect(handler).toHaveBeenCalledWith('test2', 42)
38+
})
39+
40+
test('off removes all listeners of a given type when no handler is provided', () => {
41+
const emitter = mitt<{ test: string }>()
42+
const handler1 = mock((data: string) => {})
43+
const handler2 = mock((data: string) => {})
44+
45+
emitter.on('test', handler1)
46+
emitter.on('test', handler2)
47+
emitter.off('test')
48+
emitter.emit('test', 'hello')
49+
50+
expect(handler1).not.toHaveBeenCalled()
51+
expect(handler2).not.toHaveBeenCalled()
52+
})
53+
54+
test('events creates a mitt instance with StacksEvents', () => {
55+
const emitter = events()
56+
expect(emitter).toHaveProperty('on')
57+
expect(emitter).toHaveProperty('off')
58+
expect(emitter).toHaveProperty('emit')
59+
})
60+
61+
test('listen is an alias for on', () => {
62+
const handler = mock((data: object) => {})
63+
listen('user:registered', handler)
64+
dispatch('user:registered', { name: 'John Doe' })
65+
expect(handler).toHaveBeenCalledWith({ name: 'John Doe' })
66+
})
67+
68+
test('dispatch is an alias for emit', () => {
69+
const handler = mock((data: object) => {})
70+
listen('user:logged-in', handler)
71+
dispatch('user:logged-in', { userId: 1 })
72+
expect(handler).toHaveBeenCalledWith({ userId: 1 })
73+
})
74+
75+
test('useEvent is an alias for emit', () => {
76+
const handler = mock((data: object) => {})
77+
listen('user:logged-out', handler)
78+
useEvent('user:logged-out', { userId: 1 })
79+
expect(handler).toHaveBeenCalledWith({ userId: 1 })
80+
})
81+
82+
test('useEvents provides access to the event emitter', () => {
83+
expect(useEvents).toHaveProperty('on')
84+
expect(useEvents).toHaveProperty('off')
85+
expect(useEvents).toHaveProperty('emit')
86+
})
87+
88+
test('all provides access to the event handler map', () => {
89+
const handler = mock((data: Partial<UserModel>) => {})
90+
listen('user:updated', handler)
91+
expect(all.get('user:updated')).toContain(handler)
92+
})
93+
94+
test('off removes a specific listener', () => {
95+
const handler = mock((data: object) => {})
96+
listen('user:password-changed', handler)
97+
off('user:password-changed', handler)
98+
dispatch('user:password-changed', { userId: 1 })
99+
expect(handler).not.toHaveBeenCalled()
100+
})
101+
102+
test('multiple events can be handled', () => {
103+
const registeredHandler = mock((data: object) => {})
104+
const updatedHandler = mock((data: UserModel) => {})
105+
106+
listen('user:registered', registeredHandler)
107+
listen('user:updated', updatedHandler)
108+
109+
const userModel: Partial<UserModel> = { id: 1, name: 'John Doe' }
110+
111+
dispatch('user:registered', { name: 'John Doe' })
112+
dispatch('user:updated', userModel)
113+
114+
expect(registeredHandler).toHaveBeenCalledWith({ name: 'John Doe' })
115+
expect(updatedHandler).toHaveBeenCalledWith(userModel)
116+
})
117+
})

storage/framework/core/events/tests/example.test.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)