|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
| 3 | +using StringCleanMultiline |
| 4 | + |
3 | 5 | RSpec.describe 'Quill editor' do |
4 | | - def lookup_editor(field:) |
5 | | - selector = ["##{field}_input.quill_editor", Shared::QuillEditor::SELECTOR].join(' ') |
6 | | - toolbar_selector = ["##{field}_input.quill_editor", Shared::QuillEditor::TOOLBAR_SELECTOR].join(' ') |
7 | | - Shared::QuillEditor.new(selector: selector, toolbar_selector: toolbar_selector) |
| 6 | + let(:author) { Author.create!(email: '[email protected]', name: 'John Doe', age: 30) } |
| 7 | + let(:post) do |
| 8 | + Post.create!(title: 'Test', author: author, description: '<p>Some content</p>', summary: '<p>Post summary</p>') |
8 | 9 | end |
9 | 10 |
|
10 | | - let(:author) { Author.create!(email: '[email protected]', name: 'John Doe', age: 30) } |
11 | | - let!(:post) { Post.create!(title: 'Test', author: author, description: '') } |
| 11 | + let(:submit_button) { find('#post_submit_action [type="submit"]') } |
12 | 12 |
|
13 | 13 | context 'with a Quill editor' do |
14 | | - let(:editor) { lookup_editor(field: 'post_description') } |
15 | | - |
16 | | - before do |
| 14 | + let(:edit_page) do |
17 | 15 | path = edit_admin_post_path(post) |
18 | | - Admin::Posts::EditPage.new(path: path).load |
| 16 | + Admin::Posts::EditPage.new(path: path) |
19 | 17 | end |
| 18 | + let(:editor) { edit_page.lookup_editor(editor_container: '#post_description_input') } |
| 19 | + let(:input_field) { find('#post_description[data-aa-quill-editor]') } |
20 | 20 |
|
21 | | - it 'adds some text to the description', :aggregate_failures do |
22 | | - expect(page).to have_css('#post_description[data-aa-quill-editor]') |
23 | | - editor << 'Some content...' |
24 | | - %i[bold italic underline link].each do |control| |
25 | | - expect(page).to have_css editor.control_selector(control) |
26 | | - end |
27 | | - expect(editor.content_element).to have_content('Some content...') |
28 | | - expect { find('[type="submit"]').click } |
29 | | - .to change { post.reload.description }.to '<p>Some content...</p>' |
| 21 | + before do |
| 22 | + edit_page.load |
30 | 23 | end |
31 | 24 |
|
32 | | - it 'adds some bold text to the description', :aggregate_failures do |
33 | | - editor.toolbar_control(:bold) |
34 | | - editor << 'Some bold text' |
35 | | - |
36 | | - expect(editor.content_element).to have_content('Some bold text') |
37 | | - expect { find('[type="submit"]').click } |
38 | | - .to change { post.reload.description }.to '<p><strong>Some bold text</strong></p>' |
| 25 | + it 'initializes the editor', :aggregate_failures do |
| 26 | + expect(editor.content_element).to be_present |
| 27 | + expect(editor.content).to eq('<p>Some content</p>') |
| 28 | + expect(input_field).to be_present |
39 | 29 | end |
40 | 30 |
|
41 | | - it 'adds some italic text to the description', :aggregate_failures do |
42 | | - editor.toolbar_control(:italic) |
43 | | - editor << 'Some italic text' |
44 | | - |
45 | | - expect(editor.content_element).to have_content('Some italic text') |
46 | | - expect { find('[type="submit"]').click } |
47 | | - .to change { post.reload.description }.to '<p><em>Some italic text</em></p>' |
| 31 | + it 'edits some content using the editor' do |
| 32 | + editor << :return << 'More content' |
| 33 | + editor.toggle_bold |
| 34 | + editor << 'Some bold' |
| 35 | + editor.toggle_italic |
| 36 | + editor << 'Some italic' |
| 37 | + editor.toggle_underline |
| 38 | + editor << 'Some underline' |
| 39 | + |
| 40 | + expect(editor.content).to eq <<~HTML.clean_multiline |
| 41 | + <p>Some content</p> |
| 42 | + <p>More content<strong>Some bold<em>Some italic<u>Some underline</u></em></strong></p> |
| 43 | + HTML |
48 | 44 | end |
49 | 45 |
|
50 | | - it 'adds some underline text to the description', :aggregate_failures do |
51 | | - editor.toolbar_control(:underline) |
52 | | - editor << 'Some underline text' |
| 46 | + it 'updates the field when submitting', :aggregate_failures do |
| 47 | + editor.toggle_bold |
| 48 | + editor << 'More content' |
53 | 49 |
|
54 | | - expect(editor.content_element).to have_content('Some underline text') |
55 | | - expect { find('[type="submit"]').click } |
56 | | - .to change { post.reload.description }.to '<p><u>Some underline text</u></p>' |
57 | | - end |
| 50 | + before = '<p>Some content</p>' |
| 51 | + after = '<p><strong>More content</strong>Some content</p>' |
| 52 | + expect { submit_button.click }.to change { post.reload.description }.from(before).to(after) |
58 | 53 |
|
59 | | - it 'adds a link to the description', :aggregate_failures do |
60 | | - editor << "Just a link" |
61 | | - editor.select_all |
62 | | - editor.toolbar_control(:link) |
63 | | - editor.element.find('[data-link]').send_keys("https://www.google.com", :return) |
64 | | - |
65 | | - expect(editor.content_element).to have_content('Just a link') |
66 | | - html = '<p><a href="Just a linkhttps://www.google.com" rel="noopener noreferrer" target="_blank">Just a link</a></p>' |
67 | | - expect { find('[type="submit"]').click }.to change { post.reload.description }.to html |
| 54 | + expect(page).to have_content('was successfully updated') |
68 | 55 | end |
69 | 56 | end |
70 | 57 |
|
71 | 58 | context 'with 2 Quill editors' do |
72 | | - before do |
| 59 | + let(:edit_page) do |
73 | 60 | path = edit_admin_post_path(post) |
74 | | - Admin::Posts::EditPage.new(path: path).load |
| 61 | + Admin::Posts::EditPage.new(path: path) |
| 62 | + end |
| 63 | + let(:first_editor) { edit_page.lookup_editor(editor_container: '#post_description_input') } |
| 64 | + let(:second_editor) { edit_page.lookup_editor(editor_container: '#post_summary_input') } |
| 65 | + |
| 66 | + before do |
| 67 | + edit_page.load |
75 | 68 | end |
76 | 69 |
|
77 | 70 | it 'updates some HTML content for 2 fields', :aggregate_failures do |
78 | | - editor1 = lookup_editor(field: 'post_description') |
79 | | - editor1.clear.toolbar_control(:bold) |
80 | | - editor1 << "Some description" |
| 71 | + # Check the initial states |
| 72 | + expect(first_editor.content).to eq('<p>Some content</p>') |
| 73 | + expect(second_editor.content).to eq('<p>Post summary</p>') |
| 74 | + |
| 75 | + # Add some content to both the editors |
| 76 | + first_editor.toggle_bold |
| 77 | + first_editor << 'Some bold' |
81 | 78 |
|
82 | | - editor2 = lookup_editor(field: 'post_summary') |
83 | | - editor2.clear.toolbar_control(:italic) |
84 | | - editor2 << "Some summary" |
| 79 | + second_editor.toggle_italic |
| 80 | + second_editor << 'Some italic' |
85 | 81 |
|
86 | | - find('[type="submit"]').click |
87 | | - post.reload |
| 82 | + # Check that both the fields change |
| 83 | + before = '<p>Some content</p>' |
| 84 | + after = '<p><strong>Some bold</strong>Some content</p>' |
| 85 | + expect { submit_button.click }.to change { post.reload.description }.from(before).to(after) |
88 | 86 |
|
89 | | - expect(post.description).to eq '<p><strong>Some description</strong></p>' |
90 | | - expect(post.summary).to eq '<p><em>Some summary</em></p>' |
| 87 | + expect(post.summary).to eq '<p><em>Some italic</em>Post summary</p>' |
91 | 88 | end |
92 | 89 | end |
93 | 90 |
|
94 | 91 | context 'with a Quill editor in a nested resource' do |
95 | | - before do |
| 92 | + let(:edit_page) do |
96 | 93 | path = edit_admin_author_path(author) |
97 | | - Admin::Authors::EditPage.new(path: path).load |
| 94 | + Admin::Authors::EditPage.new(path: path) |
| 95 | + end |
| 96 | + let(:submit_button) { find('#author_submit_action [type="submit"]') } |
| 97 | + |
| 98 | + before do |
| 99 | + post |
| 100 | + edit_page.load |
98 | 101 | end |
99 | 102 |
|
100 | 103 | it 'updates some HTML content of a new nested resource', :aggregate_failures do |
101 | | - find('.posts.has_many_container .has_many_add').click |
102 | | - expect(page).to have_css('.posts.has_many_container .ql-editor', count: 2) |
| 104 | + click_on 'Add New Post' |
103 | 105 |
|
104 | | - editor = lookup_editor(field: 'author_posts_attributes_1_description') |
105 | | - editor << "Some post text" |
| 106 | + first_editor = edit_page.lookup_editor(editor_container: '#author_posts_attributes_0_description_input') |
| 107 | + expect(first_editor.content).to eq('<p>Some content</p>') |
106 | 108 |
|
107 | | - fill_in('author[posts_attributes][1][title]', with: 'A new post') |
108 | | - find('[type="submit"]').click |
| 109 | + fill_in('author[posts_attributes][1][title]', with: 'Some title') |
| 110 | + second_editor = edit_page.lookup_editor(editor_container: '#author_posts_attributes_1_description_input') |
| 111 | + second_editor.toggle_underline |
| 112 | + second_editor << 'Some underline' |
109 | 113 |
|
110 | | - expect(page).to have_content('was successfully updated') |
111 | | - expect(author.posts.last.description).to eq '<p>Some post text</p>' |
| 114 | + expect { submit_button.click }.to change(Post, :count).by(1) |
| 115 | + expect(Post.last.description).to eq '<p><u>Some underline</u></p>' |
112 | 116 | end |
113 | 117 | end |
114 | 118 | end |
0 commit comments