Skip to content

Commit 0672798

Browse files
authored
Merge pull request #42 from blocknotes/test-suite-improvements
Improve data setup for specs
2 parents 10ce78b + f11bd98 commit 0672798

10 files changed

+55
-36
lines changed

spec/rails_helper.rb

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@
3737
module SpecHelpers
3838
class << self
3939
def setup_data
40-
Rails.application.load_seed
41-
author = Author.find_by!(name: 'A test author')
42-
tag = Tag.find_by!(name: 'A test tag')
43-
Post.first.update!(title: 'A post', author: author, category: 'news', published: true, dt: Time.zone.today)
44-
Post.second.update!(title: 'Another post', author: author, category: 'story', dt: Date.yesterday, tags: [tag])
45-
Post.third.update!(title: 'Last post', author: author, category: 'news', position: 234, dt: Date.tomorrow)
40+
author1 = Author.create!(name: 'First author', email: '[email protected]', age: 33)
41+
author2 = Author.create!(name: 'Second author', email: '[email protected]', age: 42)
42+
tag = Tag.create!(name: 'A test tag')
43+
Post.create!(title: 'A post', author: author1, category: 'news', published: true, dt: Time.zone.today)
44+
Post.create!(title: 'Another post', author: author1, category: 'story', dt: Date.yesterday, tags: [tag])
45+
Post.create!(title: 'Last post', author: author1, category: 'news', position: 234, dt: Date.tomorrow)
46+
Post.create!(title: 'More post', author: author2, category: 'gallery', dt: Date.yesterday)
4647
end
4748
end
4849
end
@@ -61,10 +62,6 @@ def setup_data
6162
config.use_instantiated_fixtures = false
6263
config.render_views = false
6364

64-
config.before(:suite) do
65-
SpecHelpers.setup_data
66-
end
67-
6865
config.before(:suite) do
6966
require 'administrate/version'
7067

spec/system/belongs_to_filter_spec.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
# frozen_string_literal: true
22

33
RSpec.describe 'Belongs to filter' do
4-
let(:author) { Author.find_by!(name: 'A test author') }
4+
let(:author) { Author.find_by!(name: 'First author') }
55
let(:posts) { Post.where(author: author) }
66

7+
before do
8+
SpecHelpers.setup_data
9+
end
10+
711
it 'filters the posts by author', :aggregate_failures do
8-
visit '/admin/posts'
12+
visit admin_posts_path
913

1014
find('.filter-author .selectize-input').click
1115
find(".filter-author .option[data-value='#{author.id}']").click

spec/system/boolean_filter_spec.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
# frozen_string_literal: true
22

33
RSpec.describe 'Boolean filter' do
4-
let(:first_post) { Post.first }
5-
let(:second_post) { Post.second }
4+
before do
5+
SpecHelpers.setup_data
6+
end
67

78
it 'filters the posts by published', :aggregate_failures do
8-
visit '/admin/posts'
9+
visit admin_posts_path
910

1011
find('.filter-published .selectize-input').click
1112
find('.filter-published .option[data-value="true"]').click
1213
find('input[type="submit"]').click
1314

1415
expect(page).to have_current_path %r{/admin/posts\?.+q%5Bpublished_eq%5D=true.*}
15-
expect(page).to have_css('a.action-show', text: first_post.title)
16-
expect(page).not_to have_css('a.action-show', text: second_post.title)
16+
expect(page).to have_css('a.action-show', text: "A post")
17+
expect(page).not_to have_css('a.action-show', text: "Last post")
1718
end
1819
end

spec/system/date_filter_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
# frozen_string_literal: true
22

33
RSpec.describe 'Date filter' do
4-
let(:post3) { Post.third }
4+
before do
5+
SpecHelpers.setup_data
6+
end
57

68
it 'filters the posts by date', :aggregate_failures do
7-
visit '/admin/posts'
9+
visit admin_posts_path
810

911
date = Date.tomorrow
1012
fill_in('q[dt_gteq]', with: date)
1113
find('input[type="submit"]').click
1214

1315
expect(page).to have_current_path %r{/admin/posts\?.+q%5Bdt_gteq%5D=#{date}.*}
1416
expect(page).to have_css('.js-table-row', count: 1)
15-
expect(page).to have_css('.js-table-row a.action-show', text: post3.title)
17+
expect(page).to have_css('.js-table-row a.action-show', text: "Last post")
1618
end
1719
end

spec/system/filters_bar_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
RSpec.describe 'Filters bar' do
44
it 'checks that filters bar is present (looking for some specific elements)', :aggregate_failures do
5-
visit '/admin/posts'
5+
visit admin_posts_path
66

77
expect(page).to have_css('form#post_search')
88
expect(page).to have_css('input#q_title_cont')

spec/system/has_many_filter_spec.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
# frozen_string_literal: true
22

33
RSpec.describe 'Has many filter' do
4-
let(:post2) { Post.second }
54
let(:tag) { Tag.find_by!(name: 'A test tag') }
65

6+
before do
7+
SpecHelpers.setup_data
8+
end
9+
710
it 'filters the posts by tag (with Selectize)', :aggregate_failures do
8-
visit '/admin/posts'
11+
visit admin_posts_path
912

1013
find('.filter-tags .selectize-input').click
1114
find(".filter-tags .option[data-value='#{tag.id}']").click
@@ -14,13 +17,14 @@
1417
expected_param = CGI.escape("q[tags_id_in][]")
1518
expect(page).to have_current_path %r{/admin/posts\?.+#{expected_param}=#{tag.id}}
1619
expect(page).to have_css('.js-table-row', count: 1)
17-
expect(page).to have_css('.js-table-row a.action-show', text: post2.title)
20+
expect(page).to have_css('.js-table-row a.action-show', text: "Another post")
1821
end
1922

2023
it 'filters the tags by post', :aggregate_failures do
21-
visit '/admin/tags'
24+
visit admin_tags_path
2225

23-
find("#q_posts_id_in_#{post2.id}").set(true)
26+
post = Post.find_by!(title: "Another post")
27+
find("#q_posts_id_in_#{post.id}").set(true)
2428
find('input[type="submit"]').click
2529

2630
expect(page).to have_css('.js-table-row', count: 1)

spec/system/number_filter_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
# frozen_string_literal: true
22

33
RSpec.describe 'Number filter' do
4-
let(:post3) { Post.third }
4+
before do
5+
SpecHelpers.setup_data
6+
end
57

68
it 'filters the posts by position', :aggregate_failures do
7-
visit '/admin/posts'
9+
visit admin_posts_path
810

911
fill_in('q[position_eq]', with: '234')
1012
find('input[type="submit"]').click
1113

1214
expect(page).to have_current_path %r{/admin/posts\?.+q%5Bposition_eq%5D=234.*}
1315
expect(page).to have_css('.js-table-row', count: 1)
14-
expect(page).to have_css('.js-table-row a.action-show', text: post3.title)
16+
expect(page).to have_css('.js-table-row a.action-show', text: "Last post")
1517
end
1618
end

spec/system/scope_filter_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
RSpec.describe 'Scope filter' do
44
let(:posts) { Post.where(category: 'news') }
55

6+
before do
7+
SpecHelpers.setup_data
8+
end
9+
610
it 'filters the posts by category (using the scope)', :aggregate_failures do
7-
visit '/admin/posts'
11+
visit admin_posts_path
812

913
fill_in('q[by_category]', with: 'news')
1014
find('input[type="submit"]').click

spec/system/select_filter_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
# frozen_string_literal: true
22

33
RSpec.describe 'Select filter' do
4-
let(:post2) { Post.second }
4+
before do
5+
SpecHelpers.setup_data
6+
end
57

68
it 'filters the posts by category', :aggregate_failures do
7-
visit '/admin/posts'
9+
visit admin_posts_path
810

911
find('.filter-category .selectize-input').click
1012
find('.filter-category .option[data-value="story"]').click
1113
find('input[type="submit"]').click
1214

1315
expect(page).to have_current_path %r{/admin/posts\?.+q%5Bcategory_eq%5D=story.*}
1416
expect(page).to have_css('.js-table-row', count: 1)
15-
expect(page).to have_css('.js-table-row a.action-show', text: post2.title)
17+
expect(page).to have_css('.js-table-row a.action-show', text: "Another post")
1618
end
1719
end

spec/system/string_filter_spec.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# frozen_string_literal: true
22

33
RSpec.describe 'String filter' do
4-
let(:post2) { Post.second }
4+
before do
5+
SpecHelpers.setup_data
6+
end
57

68
it 'filters the posts by title', :aggregate_failures do
7-
visit '/admin/posts'
9+
visit admin_posts_path
810

911
fill_in('q[title_cont]', with: 'another')
1012
find('input[type="submit"]').click
1113

14+
expect(page).to have_current_path %r{/admin/posts?.*q%5Btitle_cont%5D=another}
1215
expect(page).to have_css('.js-table-row', count: 1)
13-
expect(page).to have_css('.js-table-row a.action-show', text: post2.title)
16+
expect(page).to have_css('.js-table-row a.action-show', text: "Another post")
1417
end
1518
end

0 commit comments

Comments
 (0)