Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
f9762169
Commit
f9762169
authored
Jun 22, 2020
by
Paul Slaughter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Replace event_hub_factory with Vue impl
- Also add specs to harden the expected API from this function
parent
616cec34
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
70 additions
and
61 deletions
+70
-61
app/assets/javascripts/helpers/event_hub_factory.js
app/assets/javascripts/helpers/event_hub_factory.js
+15
-17
spec/frontend/helpers/event_hub_factory_spec.js
spec/frontend/helpers/event_hub_factory_spec.js
+55
-44
No files found.
app/assets/javascripts/helpers/event_hub_factory.js
View file @
f9762169
import
mitt
from
'
mitt
'
;
import
Vue
from
'
vue
'
;
/**
* Return a Vue like event hub
*
* - $on
* - $off
* - $once
* - $emit
*
* Please note, this was once implemented with `mitt`, but since then has been reverted
* because of some API issues. https://gitlab.com/gitlab-org/gitlab/-/merge_requests/35074
*
* We'd like to shy away from using a full fledged Vue instance from this in the future.
*/
export
default
()
=>
{
const
emitter
=
mitt
();
emitter
.
once
=
(
event
,
handler
)
=>
{
const
wrappedHandler
=
evt
=>
{
handler
(
evt
);
emitter
.
off
(
event
,
wrappedHandler
);
};
emitter
.
on
(
event
,
wrappedHandler
);
};
emitter
.
$on
=
emitter
.
on
;
emitter
.
$once
=
emitter
.
once
;
emitter
.
$off
=
emitter
.
off
;
emitter
.
$emit
=
emitter
.
emit
;
return
emitter
;
return
new
Vue
();
};
spec/frontend/helpers/event_hub_factory_spec.js
View file @
f9762169
import
createEventHub
from
'
~/helpers/event_hub_factory
'
;
const
TEST_EVENT
=
'
foobar
'
;
describe
(
'
event bus factory
'
,
()
=>
{
let
eventBus
;
let
handler
;
beforeEach
(()
=>
{
eventBus
=
createEventHub
();
handler
=
jest
.
fn
();
});
afterEach
(()
=>
{
eventBus
=
null
;
});
describe
(
'
underlying module
'
,
()
=>
{
let
mitt
;
describe
(
'
instance
'
,
()
=>
{
it
.
each
`
method
${
'
$on
'
}
${
'
$once
'
}
${
'
$off
'
}
${
'
$emit
'
}
`
(
'
has $method method
'
,
({
method
})
=>
{
expect
(
eventBus
[
method
]).
toEqual
(
expect
.
any
(
Function
));
});
});
describe
(
'
$on
'
,
()
=>
{
beforeEach
(()
=>
{
jest
.
resetModules
(
);
jest
.
mock
(
'
mitt
'
);
eventBus
.
$on
(
TEST_EVENT
,
handler
);
}
);
// eslint-disable-next-line global-require
mitt
=
require
(
'
mitt
'
);
mitt
.
mockReturnValue
(()
=>
({}));
it
(
'
calls handler when event is emitted
'
,
()
=>
{
eventBus
.
$emit
(
TEST_EVENT
);
expect
(
handler
).
toHaveBeenCalledWith
();
});
const
createEventHubActual
=
jest
.
requireActual
(
'
~/helpers/event_hub_factory
'
).
default
;
eventBus
=
createEventHubActual
();
it
(
'
calls handler with multiple args
'
,
()
=>
{
eventBus
.
$emit
(
TEST_EVENT
,
'
arg1
'
,
'
arg2
'
,
'
arg3
'
);
expect
(
handler
).
toHaveBeenCalledWith
(
'
arg1
'
,
'
arg2
'
,
'
arg3
'
);
});
it
(
'
creates an emitter
'
,
()
=>
{
expect
(
mitt
).
toHaveBeenCalled
();
it
(
'
calls handler multiple times
'
,
()
=>
{
eventBus
.
$emit
(
TEST_EVENT
,
'
arg1
'
,
'
arg2
'
,
'
arg3
'
);
eventBus
.
$emit
(
TEST_EVENT
,
'
arg1
'
,
'
arg2
'
,
'
arg3
'
);
expect
(
handler
).
toHaveBeenCalledTimes
(
2
);
});
});
describe
(
'
instance
'
,
()
=>
{
it
.
each
`
method
${
'
on
'
}
${
'
once
'
}
${
'
off
'
}
${
'
emit
'
}
`
(
'
binds $$method to $method
'
,
({
method
})
=>
{
expect
(
typeof
eventBus
[
method
]).
toBe
(
'
function
'
);
expect
(
eventBus
[
method
]).
toBe
(
eventBus
[
`$
${
method
}
`
]);
it
(
'
does not call handler after $off with handler
'
,
()
=>
{
eventBus
.
$off
(
TEST_EVENT
,
handler
);
eventBus
.
$emit
(
TEST_EVENT
);
expect
(
handler
).
not
.
toHaveBeenCalled
();
});
});
describe
(
'
once
'
,
()
=>
{
const
event
=
'
foobar
'
;
let
handler
;
it
(
'
does not call handler after $off
'
,
()
=>
{
eventBus
.
$off
(
TEST_EVENT
);
beforeEach
(()
=>
{
jest
.
spyOn
(
eventBus
,
'
on
'
);
jest
.
spyOn
(
eventBus
,
'
off
'
);
handler
=
jest
.
fn
();
eventBus
.
once
(
event
,
handler
);
eventBus
.
$emit
(
TEST_EVENT
);
expect
(
handler
).
not
.
toHaveBeenCalled
();
});
});
it
(
'
calls on internally
'
,
()
=>
{
expect
(
eventBus
.
on
).
toHaveBeenCalled
();
describe
(
'
$once
'
,
()
=>
{
beforeEach
(()
=>
{
eventBus
.
$once
(
TEST_EVENT
,
handler
);
});
it
(
'
calls handler when event is emitted
'
,
()
=>
{
eventBus
.
emit
(
event
);
eventBus
.
$emit
(
TEST_EVENT
);
expect
(
handler
).
toHaveBeenCalled
();
});
it
(
'
calls off when event is emitted
'
,
()
=>
{
eventBus
.
emit
(
event
);
expect
(
eventBus
.
off
).
toHaveBeenCalled
();
});
it
(
'
calls the handler only once when event is emitted multiple times
'
,
()
=>
{
eventBus
.
emit
(
event
);
eventBus
.
emit
(
event
);
eventBus
.
$emit
(
TEST_EVENT
);
eventBus
.
$emit
(
TEST_EVENT
);
expect
(
handler
).
toHaveBeenCalledTimes
(
1
);
});
...
...
@@ -80,14 +87,18 @@ describe('event bus factory', () => {
handler
=
jest
.
fn
().
mockImplementation
(()
=>
{
throw
new
Error
();
});
eventBus
.
once
(
event
,
handler
);
eventBus
.
$once
(
TEST_EVENT
,
handler
);
});
it
(
'
calls off when event is emitted
'
,
()
=>
{
expect
(()
=>
{
eventBus
.
emit
(
event
);
eventBus
.
$emit
(
TEST_EVENT
);
}).
toThrow
();
expect
(
eventBus
.
off
).
toHaveBeenCalled
();
expect
(()
=>
{
eventBus
.
$emit
(
TEST_EVENT
);
}).
not
.
toThrow
();
expect
(
handler
).
toHaveBeenCalledTimes
(
1
);
});
});
});
...
...
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