# frozen_string_literal: true using StringCleanMultiline RSpec.describe 'Quill editor' do let(:author) { Author.create!(email: 'some_email@example.com', name: 'John Doe', age: 30) } let(:post) do Post.create!(title: 'Test', author: author, description: '
Some content
', summary: 'Post summary
') end let(:submit_button) { find('#post_submit_action [type="submit"]') } context 'with a Quill editor' do let(:edit_page) do path = edit_admin_post_path(post) Admin::Posts::EditPage.new(path: path) end let(:editor) { edit_page.lookup_editor(editor_container: '#post_description_input') } let(:input_field) { find('#post_description[data-aa-quill-editor]') } before do edit_page.load end it 'initializes the editor', :aggregate_failures do expect(editor.content_element).to be_present expect(editor.content).to eq('Some content
') expect(input_field).to be_present end it 'edits some content using the editor' do editor.select_all editor.toggle_link editor.tooltip_editing.send_keys(['https://blocknot.es', :return]) editor << :right << :return << 'More content' editor.toggle_bold editor << 'Some bold' editor.toggle_italic editor << 'Some italic' editor.toggle_underline editor << 'Some underline' << :return editor.toggle_blockquote editor << 'blockquote enabled' << :return editor.toggle_blockquote editor.toggle_code_block editor << 'code block enabled' << :return editor.toggle_code_block editor << 'Some text' editor.toggle_sub editor << 'sub text' editor.toggle_sub editor << ' More text' editor.toggle_super editor << 'sup text' editor.toggle_super editor << :return editor.open_dropdown(:align).toggle_align_right editor << 'Text aligned on the right' expect(editor.content).to eq <<~HTML.clean_multilineMore contentSome boldSome italicSome underline
blockquote enabled
Some textsub text More textsup text
Text aligned on the right
HTML end it 'updates the field when submitting', :aggregate_failures do editor.toggle_bold editor << 'More content' before = 'Some content
' after = 'More contentSome content
' expect { submit_button.click }.to change { post.reload.description }.from(before).to(after) expect(page).to have_content('was successfully updated') end end context 'with 2 Quill editors' do let(:edit_page) do path = edit_admin_post_path(post) Admin::Posts::EditPage.new(path: path) end let(:first_editor) { edit_page.lookup_editor(editor_container: '#post_description_input') } let(:second_editor) { edit_page.lookup_editor(editor_container: '#post_summary_input') } before do edit_page.load end it 'updates some HTML content for 2 fields', :aggregate_failures do # Check the initial states expect(first_editor.content).to eq('Some content
') expect(second_editor.content).to eq('Post summary
') # Add some content to both the editors first_editor.toggle_bold first_editor << 'Some bold' second_editor.toggle_italic second_editor << 'Some italic' # Check that both the fields change before = 'Some content
' after = 'Some boldSome content
' expect { submit_button.click }.to change { post.reload.description }.from(before).to(after) expect(post.summary).to eq 'Some italicPost summary
' end end context 'with a Quill editor in a nested resource' do let(:edit_page) do path = edit_admin_author_path(author) Admin::Authors::EditPage.new(path: path) end let(:submit_button) { find('#author_submit_action [type="submit"]') } before do post edit_page.load end it 'updates some HTML content of a new nested resource', :aggregate_failures do # Count initial posts initial_post_count = all('[id^="author_posts_attributes_"][id$="_title"]').count click_on 'Add New Post' # Wait for new fields to appear expect(page).to have_css('[id^="author_posts_attributes_"][id$="_title"]', count: initial_post_count + 1, wait: 5) first_editor = edit_page.lookup_editor(editor_container: '#author_posts_attributes_0_description_input') expect(first_editor.content).to eq('Some content
') # Find all post title fields and get the last one (newly added) title_fields = all('[id^="author_posts_attributes_"][id$="_title"]') new_title_field = title_fields.last # Only fill the title if the field is empty (new post) if new_title_field.value.empty? new_title_field.fill_in(with: 'Some title') # Extract the index from the field ID new_editor_id = new_title_field[:id].match(/posts_attributes_(\d+)_title/)[1] container_id = "#author_posts_attributes_#{new_editor_id}_description_input" second_editor = edit_page.lookup_editor(editor_container: container_id) # Clear any existing content and add new content second_editor.clear second_editor.toggle_underline second_editor << 'Some underline' expect { submit_button.click }.to change(Post, :count).by(1) expect(Post.last.description).to eq 'Some underline
' else # If Add New Post didn't work, skip this test for older AA versions skip 'Add New Post functionality not working properly in this ActiveAdmin version' end end end end