We were repeating the same code over and over (with a few variants) to
setup tests which require an administrator. We can use a tag and
simplify the code.
When a page with ckeditor is restored from browser cache by using browser
history back feature application was trying to re-initialize it but this was
throwing some javascript errors that left ckeditor useless. The ckeditor user
interface seemed to be loaded correctly but editor contents was not shown
and ckeditor locked.
This solution is about destroying all ckeditor instances on page before
leaving it and force the reinitialization after Turbolinks restored the cache.
Inspiration here [1].
There is a similar patch to make it work with Turbolinks 5.x versions [2].
[1] https://github.com/galetahub/ckeditor/issues/575#issuecomment-132757961
[2] https://github.com/galetahub/ckeditor/issues/575#issuecomment-241185136
Some specs involving CKEditor were failing sometimes in the Rails 5.1
branch. The reason why these specs pass with Rails 5.0 but fail with
Rails 5.1 are unknown. On my machine the tests pass when precompiling
the assets, which makes me think it's related to the way Rails handles
them, but it might have nothing to do with it.
The only (apparently) 100% reliable solution I've found is to wait for
CKEditor to load before trying to fill it in. After running the tests on
my machine hundreds of time, I didn't get a single failure.
CKEditor repositions the dialog to attach an image after showing it. If
its position changes right when Capybara is trying to click the "Upload"
link, the click does not work properly.
I haven't found a way to check the dialog has stopped moving, so I've
chosen to force its position to the top.
Without this change, this test failed a lot on my machine when using
Rails 5.1, both with an "iframe ckeditor" and an "inline ckeditor".
However, it didn't fail when using Rails 5.0.