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
fc9f53ab
Commit
fc9f53ab
authored
Jan 22, 2021
by
Jannik Lehmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
remove unnecessary DOM Content Loaded Event Listener
parent
c31fcc99
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
16 deletions
+28
-16
app/assets/javascripts/behaviors/autosize.js
app/assets/javascripts/behaviors/autosize.js
+5
-7
spec/frontend/behaviors/autosize_spec.js
spec/frontend/behaviors/autosize_spec.js
+23
-9
No files found.
app/assets/javascripts/behaviors/autosize.js
View file @
fc9f53ab
import
Autosize
from
'
autosize
'
;
import
{
waitForCSSLoaded
}
from
'
~/helpers/startup_css_helper
'
;
document
.
addEventListener
(
'
DOMContentLoaded
'
,
()
=>
{
waitForCSSLoaded
(()
=>
{
const
autosizeEls
=
document
.
querySelectorAll
(
'
.js-autosize
'
);
waitForCSSLoaded
(()
=>
{
const
autosizeEls
=
document
.
querySelectorAll
(
'
.js-autosize
'
);
Autosize
(
autosizeEls
);
Autosize
.
update
(
autosizeEls
);
Autosize
(
autosizeEls
);
Autosize
.
update
(
autosizeEls
);
autosizeEls
.
forEach
((
el
)
=>
el
.
classList
.
add
(
'
js-autosize-initialized
'
));
});
autosizeEls
.
forEach
((
el
)
=>
el
.
classList
.
add
(
'
js-autosize-initialized
'
));
});
spec/frontend/behaviors/autosize_spec.js
View file @
fc9f53ab
import
'
~/behaviors/autosize
'
;
function
load
()
{
document
.
dispatchEvent
(
new
Event
(
'
DOMContentLoaded
'
));
}
jest
.
mock
(
'
~/helpers/startup_css_helper
'
,
()
=>
{
return
{
waitForCSSLoaded
:
jest
.
fn
().
mockImplementation
((
cb
)
=>
cb
.
apply
()),
waitForCSSLoaded
:
jest
.
fn
().
mockImplementation
((
cb
)
=>
{
// This is a hack:
// autosize.js will execute and modify the DOM
// whenever waitForCSSLoaded calls its callback function.
// This setTimeout is here because everything within setTimeout will be queued
// as async code until the current call stack is executed.
// If we would not do this, the mock for waitForCSSLoaded would call its callback
// before the fixture in the beforeEach is set and the Test would fail.
// more on this here: https://johnresig.com/blog/how-javascript-timers-work/
setTimeout
(()
=>
{
cb
.
apply
();
},
0
);
}),
};
});
...
...
@@ -16,9 +24,15 @@ describe('Autosize behavior', () => {
});
it
(
'
is applied to the textarea
'
,
()
=>
{
load
();
const
textarea
=
document
.
querySelector
(
'
textarea
'
);
expect
(
textarea
.
classList
).
toContain
(
'
js-autosize-initialized
'
);
// This is the second part of the Hack:
// Because we are forcing the mock for WaitForCSSLoaded and the very end of our callstack
// to call its callback. This querySelector needs to go to the very end of our callstack
// as well, if we would not have this setTimeout Function here, the querySelector
// would run before the mockImplementation called its callBack Function
// the DOM Manipulation didn't happen yet and the test would fail.
setTimeout
(()
=>
{
const
textarea
=
document
.
querySelector
(
'
textarea
'
);
expect
(
textarea
.
classList
).
toContain
(
'
js-autosize-initialized
'
);
},
0
);
});
});
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