|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | RSpec.describe 'Quill editor' do |
| 4 | + def lookup_editor(field:) |
| 5 | + selector = ["##{field}_input.quill_editor", QuillEditor::SELECTOR].join(' ') |
| 6 | + toolbar_selector = ["##{field}_input.quill_editor", QuillEditor::TOOLBAR_SELECTOR].join(' ') |
| 7 | + QuillEditor.new(selector: selector, toolbar_selector: toolbar_selector) |
| 8 | + end |
| 9 | + |
4 | 10 | let(:author) { Author.create!(email: 'some_email@example.com', name: 'John Doe', age: 30) } |
5 | | - let!(:post) { Post.create!(title: 'Test', author: author, description: 'Some content...') } |
| 11 | + let!(:post) { Post.create!(title: 'Test', author: author, description: '') } |
6 | 12 |
|
7 | 13 | context 'with a Quill editor' do |
8 | | - it 'initialize the editor' do |
9 | | - visit "/admin/posts/#{post.id}/edit" |
| 14 | + let(:editor) { lookup_editor(field: 'post_description') } |
10 | 15 |
|
11 | | - %w[bold italic underline link].each do |button| |
12 | | - expect(page).to have_css(".ql-toolbar button.ql-#{button}") |
13 | | - end |
14 | | - expect(page).to have_css('#post_description[data-aa-quill-editor]') |
15 | | - expect(page).to have_css('#post_description_input .ql-editor', text: 'Some content...') |
| 16 | + before do |
| 17 | + path = edit_admin_post_path(post) |
| 18 | + Admin::Posts::EditPage.new(path: path).load |
16 | 19 | end |
17 | 20 |
|
18 | | - it 'adds some text to the description' do |
19 | | - visit "/admin/posts/#{post.id}/edit" |
| 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>' |
| 30 | + end |
20 | 31 |
|
21 | | - find('#post_description_input .ql-editor').click |
22 | | - find('#post_description_input .ql-editor').base.send_keys('more text') |
23 | | - find('[type="submit"]').click |
| 32 | + it 'adds some bold text to the description', :aggregate_failures do |
| 33 | + editor.toolbar_control(:bold) |
| 34 | + editor << 'Some bold text' |
24 | 35 |
|
25 | | - expect(page).to have_content('was successfully updated') |
26 | | - expect(post.reload.description).to eq '<p>Some content...more text</p>' |
| 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>' |
27 | 39 | end |
28 | 40 |
|
29 | | - it 'adds some bold text to the description' do |
30 | | - visit "/admin/posts/#{post.id}/edit" |
31 | | - |
32 | | - find('#post_description_input .ql-editor').click |
33 | | - find('#post_description_input .ql-toolbar .ql-bold').click |
34 | | - find('#post_description_input .ql-editor').base.send_keys('more text') |
35 | | - find('[type="submit"]').click |
| 41 | + it 'adds some italic text to the description', :aggregate_failures do |
| 42 | + editor.toolbar_control(:italic) |
| 43 | + editor << 'Some italic text' |
36 | 44 |
|
37 | | - expect(post.reload.description).to eq '<p>Some content...<strong>more text</strong></p>' |
| 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>' |
38 | 48 | end |
39 | 49 |
|
40 | | - it 'adds some italic text to the description' do |
41 | | - visit "/admin/posts/#{post.id}/edit" |
| 50 | + it 'adds some underline text to the description', :aggregate_failures do |
| 51 | + editor.toolbar_control(:underline) |
| 52 | + editor << 'Some underline text' |
42 | 53 |
|
43 | | - find('#post_description_input .ql-editor').click |
44 | | - find('#post_description_input .ql-toolbar .ql-italic').click |
45 | | - find('#post_description_input .ql-editor').base.send_keys('more text') |
46 | | - find('[type="submit"]').click |
| 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 |
47 | 58 |
|
48 | | - expect(post.reload.description).to eq '<p>Some content...<em>more text</em></p>' |
| 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 |
49 | 68 | end |
50 | 69 | end |
51 | 70 |
|
52 | 71 | context 'with 2 Quill editors' do |
53 | | - it 'updates some HTML content for 2 fields' do |
54 | | - visit "/admin/posts/#{post.id}/edit" |
55 | | - |
56 | | - find('#post_description_input .ql-editor').click |
57 | | - find('#post_description_input .ql-toolbar .ql-bold').click |
58 | | - find('#post_description_input .ql-editor').base.send_keys('more text') |
59 | | - find('#post_summary_input .ql-editor').click |
60 | | - find('#post_summary_input .ql-toolbar .ql-italic').click |
61 | | - find('#post_summary_input .ql-editor').base.send_keys('Summary text') |
| 72 | + before do |
| 73 | + path = edit_admin_post_path(post) |
| 74 | + Admin::Posts::EditPage.new(path: path).load |
| 75 | + end |
| 76 | + |
| 77 | + 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" |
| 81 | + |
| 82 | + editor2 = lookup_editor(field: 'post_summary') |
| 83 | + editor2.clear.toolbar_control(:italic) |
| 84 | + editor2 << "Some summary" |
| 85 | + |
62 | 86 | find('[type="submit"]').click |
63 | 87 | post.reload |
64 | 88 |
|
65 | | - expect(post.description).to eq '<p>Some content...<strong>more text</strong></p>' |
66 | | - expect(post.summary).to eq '<p><em>Summary text</em></p>' |
| 89 | + expect(post.description).to eq '<p><strong>Some description</strong></p>' |
| 90 | + expect(post.summary).to eq '<p><em>Some summary</em></p>' |
67 | 91 | end |
68 | 92 | end |
69 | 93 |
|
70 | 94 | context 'with a Quill editor in a nested resource' do |
71 | | - it 'updates some HTML content of a new nested resource' do |
72 | | - visit "/admin/authors/#{author.id}/edit" |
| 95 | + before do |
| 96 | + path = edit_admin_author_path(author) |
| 97 | + Admin::Authors::EditPage.new(path: path).load |
| 98 | + end |
73 | 99 |
|
74 | | - expect(page).to have_css('.posts.has_many_container .ql-editor', text: 'Some content...') |
| 100 | + it 'updates some HTML content of a new nested resource', :aggregate_failures do |
75 | 101 | find('.posts.has_many_container .has_many_add').click |
76 | 102 | expect(page).to have_css('.posts.has_many_container .ql-editor', count: 2) |
77 | 103 |
|
| 104 | + editor = lookup_editor(field: 'author_posts_attributes_1_description') |
| 105 | + editor << "Some post text" |
| 106 | + |
78 | 107 | fill_in('author[posts_attributes][1][title]', with: 'A new post') |
79 | | - find('#author_posts_attributes_1_description_input .ql-editor').base.send_keys('new post text') |
80 | 108 | find('[type="submit"]').click |
81 | 109 |
|
82 | 110 | expect(page).to have_content('was successfully updated') |
83 | | - expect(author.posts.last.description).to eq '<p>new post text</p>' |
| 111 | + expect(author.posts.last.description).to eq '<p>Some post text</p>' |
84 | 112 | end |
85 | 113 | end |
86 | 114 | end |
0 commit comments