Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
871bc4e0
Commit
871bc4e0
authored
Jul 09, 2017
by
Arunoda Susiripala
Committed by
GitHub
Jul 09, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add tests for EventEmitter. (#2515)
parent
2d510d64
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
86 additions
and
1 deletion
+86
-1
EventEmitter.js
lib/EventEmitter.js
+4
-1
EventEmitter.test.js
test/unit/EventEmitter.test.js
+82
-0
No files found.
lib/EventEmitter.js
View file @
871bc4e0
...
@@ -6,6 +6,10 @@ export default class EventEmitter {
...
@@ -6,6 +6,10 @@ export default class EventEmitter {
this
.
listeners
[
event
]
=
new
Set
()
this
.
listeners
[
event
]
=
new
Set
()
}
}
if
(
this
.
listeners
[
event
].
has
(
cb
))
{
throw
new
Error
(
`The listener already exising in event:
${
event
}
`
)
}
this
.
listeners
[
event
].
add
(
cb
)
this
.
listeners
[
event
].
add
(
cb
)
}
}
...
@@ -15,7 +19,6 @@ export default class EventEmitter {
...
@@ -15,7 +19,6 @@ export default class EventEmitter {
}
}
off
(
event
,
cb
)
{
off
(
event
,
cb
)
{
if
(
!
this
.
listeners
[
event
])
return
this
.
listeners
[
event
].
delete
(
cb
)
this
.
listeners
[
event
].
delete
(
cb
)
}
}
}
}
test/unit/EventEmitter.test.js
0 → 100644
View file @
871bc4e0
/* global describe, it, expect */
import
EventEmitter
from
'../../dist/lib/EventEmitter'
describe
(
'EventEmitter'
,
()
=>
{
describe
(
'With listeners'
,
()
=>
{
it
(
'should listen to a event'
,
(
done
)
=>
{
const
ev
=
new
EventEmitter
()
ev
.
on
(
'sample'
,
done
)
ev
.
emit
(
'sample'
)
})
it
(
'should listen to multiple listeners'
,
()
=>
{
const
ev
=
new
EventEmitter
()
let
cnt
=
0
ev
.
on
(
'sample'
,
()
=>
{
cnt
+=
1
})
ev
.
on
(
'sample'
,
()
=>
{
cnt
+=
1
})
ev
.
emit
(
'sample'
)
expect
(
cnt
).
toBe
(
2
)
})
it
(
'should listen to multiple events'
,
()
=>
{
const
ev
=
new
EventEmitter
()
const
data
=
[]
const
cb
=
(
name
)
=>
{
data
.
push
(
name
)
}
ev
.
on
(
'sample1'
,
cb
)
ev
.
on
(
'sample2'
,
cb
)
ev
.
emit
(
'sample1'
,
'one'
)
ev
.
emit
(
'sample2'
,
'two'
)
expect
(
data
).
toEqual
([
'one'
,
'two'
])
})
it
(
'should support multiple arguments'
,
()
=>
{
const
ev
=
new
EventEmitter
()
let
data
ev
.
on
(
'sample'
,
(...
args
)
=>
{
data
=
args
})
ev
.
emit
(
'sample'
,
'one'
,
'two'
)
expect
(
data
).
toEqual
([
'one'
,
'two'
])
})
it
(
'should possible to stop listening an event'
,
()
=>
{
const
ev
=
new
EventEmitter
()
let
cnt
=
0
const
cb
=
()
=>
{
cnt
+=
1
}
ev
.
on
(
'sample'
,
cb
)
ev
.
emit
(
'sample'
)
expect
(
cnt
).
toBe
(
1
)
ev
.
off
(
'sample'
,
cb
)
ev
.
emit
(
'sample'
)
expect
(
cnt
).
toBe
(
1
)
})
it
(
'should throw when try to add the same listener multiple times'
,
()
=>
{
const
ev
=
new
EventEmitter
()
const
cb
=
()
=>
{}
ev
.
on
(
'sample'
,
cb
)
const
run
=
()
=>
ev
.
on
(
'sample'
,
cb
)
expect
(
run
).
toThrow
(
/The listener already exising in event: sample/
)
})
})
describe
(
'Without a listener'
,
()
=>
{
it
(
'should not fail to emit'
,
()
=>
{
const
ev
=
new
EventEmitter
()
ev
.
emit
(
'aaaa'
,
10
,
20
)
})
})
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment