Skip to content

Commit b5d2738

Browse files
authored
Merge pull request blocknotes#60 from blocknotes/test/improve-tests
Improve tests
2 parents 6946b78 + 8cac91f commit b5d2738

File tree

4 files changed

+100
-93
lines changed

4 files changed

+100
-93
lines changed

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ RSpec/ExampleLength:
2727
RSpec/MultipleExpectations:
2828
# default 1
2929
Max: 4
30+
31+
RSpec/MultipleMemoizedHelpers:
32+
# default 5
33+
Max: 10

spec/page_objects/shared/quill_editor.rb

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,34 @@
22

33
module Shared
44
class QuillEditor < HtmlEditor
5-
SELECTOR = '.ql-container'
6-
TOOLBAR_SELECTOR = '.ql-toolbar'
5+
attr_reader :editor_selector, :toolbar_selector
76

8-
attr_reader :toolbar, :toolbar_selector
9-
10-
def initialize(selector: SELECTOR, toolbar_selector: TOOLBAR_SELECTOR)
11-
super(selector: selector)
12-
@toolbar = find(toolbar_selector)
13-
@toolbar_selector = toolbar_selector
7+
def initialize(container)
8+
@editor_selector = "#{container} .ql-container"
9+
@toolbar_selector = "#{container} .ql-toolbar"
10+
super(selector: editor_selector)
1411
end
1512

13+
def content = content_element['innerHTML']
14+
1615
def content_element
1716
@content_element ||= find("#{selector} .ql-editor")
1817
end
1918

20-
def control_selector(control)
21-
case control&.to_sym
22-
when :bold then "#{toolbar_selector} button.ql-bold"
23-
when :italic then "#{toolbar_selector} button.ql-italic"
24-
when :underline then "#{toolbar_selector} button.ql-underline"
25-
when :link then "#{toolbar_selector} button.ql-link"
26-
else raise "Invalid control #{control}"
27-
end
28-
end
29-
3019
def toggle_bold
31-
find(control_selector(:bold)).click
20+
find("#{toolbar_selector} button.ql-bold").click
3221
end
3322

3423
def toggle_italic
35-
find(control_selector(:italic)).click
24+
find("#{toolbar_selector} button.ql-italic").click
3625
end
3726

3827
def toggle_underline
39-
find(control_selector(:underline)).click
28+
find("#{toolbar_selector} button.ql-underline").click
4029
end
4130

4231
def toggle_link
43-
find(control_selector(:link)).click
32+
find("#{toolbar_selector} button.ql-link").click
4433
end
4534
end
4635
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
module StringCleanMultiline
4+
refine String do
5+
def clean_multiline
6+
# Get rid of newlines and indentation spaces
7+
strip.gsub(/\s*\n\s*/, "")
8+
end
9+
end
10+
end

spec/system/quill_editor_spec.rb

Lines changed: 75 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,118 @@
11
# frozen_string_literal: true
22

3+
using StringCleanMultiline
4+
35
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>')
89
end
910

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"]') }
1212

1313
context 'with a Quill editor' do
14-
let(:editor) { lookup_editor(field: 'post_description') }
15-
16-
before do
14+
let(:edit_page) do
1715
path = edit_admin_post_path(post)
18-
Admin::Posts::EditPage.new(path: path).load
16+
Admin::Posts::EditPage.new(path: path)
1917
end
18+
let(:editor) { edit_page.lookup_editor(editor_container: '#post_description_input') }
19+
let(:input_field) { find('#post_description[data-aa-quill-editor]') }
2020

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
3023
end
3124

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
3929
end
4030

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
4844
end
4945

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'
5349

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)
5853

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')
6855
end
6956
end
7057

7158
context 'with 2 Quill editors' do
72-
before do
59+
let(:edit_page) do
7360
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
7568
end
7669

7770
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'
8178

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'
8581

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)
8886

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>'
9188
end
9289
end
9390

9491
context 'with a Quill editor in a nested resource' do
95-
before do
92+
let(:edit_page) do
9693
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
98101
end
99102

100103
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'
103105

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>')
106108

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'
109113

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>'
112116
end
113117
end
114118
end

0 commit comments

Comments
 (0)