Skip to content

Commit 63fc738

Browse files
committed
Add helper specs
1 parent f93aaf2 commit 63fc738

File tree

6 files changed

+57
-3
lines changed

6 files changed

+57
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ spec/dummy/db/*.sqlite3
55
spec/dummy/db/*.sqlite3-journal
66
spec/dummy/log/*.log
77
spec/dummy/tmp/
8+
coverage

bin/rake

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
#
4+
# This file was generated by Bundler.
5+
#
6+
# The application 'rake' is installed as part of a gem, and
7+
# this file is here to facilitate running it.
8+
#
9+
10+
require "pathname"
11+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12+
Pathname.new(__FILE__).realpath)
13+
14+
require "rubygems"
15+
require "bundler/setup"
16+
17+
load Gem.bin_path("rake", "rake")

spec/dummy/app/models/user.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class User < ApplicationRecord
2+
attr_accessor :first_name, :last_name, :email
3+
end

spec/dummy/config/routes.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Rails.application.routes.draw do
2-
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
2+
resources :users
33
end
+32-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
11
require 'rails_helper'
22

33
describe SortableBy::TableHelper, type: :helper do
4-
describe '#sortable_table_header' do
5-
it 'generates an html table header'
4+
it 'returns a table header with rows' do
5+
result = helper.sortable_table_header(:users_path)
6+
expect(result).to include('<thead class="tb-table-header"><tr></tr></thead>')
7+
expect(result).to include('</thead>')
68
end
9+
10+
it 'has some headers' do
11+
result = helper.sortable_table_header(:users_path, model: User) do |t|
12+
view.concat(t.header(:first_name))
13+
view.concat(t.header(:email))
14+
end
15+
expect(result).to include('<th>First name</th>')
16+
expect(result).to include('<th>Email</th>')
17+
end
18+
19+
it 'generates links' do
20+
result = helper.sortable_table_header(:users_path, model: User) do |t|
21+
view.concat(t.sortable(:first_name))
22+
view.concat(t.sortable(:email))
23+
end
24+
expect(result).to include('<a href="/users?dir=asc&amp;sort=first_name">First name</a>')
25+
expect(result).to include('<a href="/users?dir=asc&amp;sort=email">Email</a>')
26+
end
27+
28+
it 'sets the active class' do
29+
helper.params[:sort] = 'first_name'
30+
helper.params[:dir] = 'desc'
31+
result = helper.sortable_table_header(:users_path, model: User) do |t|
32+
view.concat(t.sortable(:first_name))
33+
end
34+
expect(result).to include('<th class="sortable sortable-active sortable-desc">')
35+
end
36+
737
end

spec/rails_helper.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
require 'simplecov'
2+
SimpleCov.start 'rails'
3+
14
# This file is copied to spec/ when you run 'rails generate rspec:install'
25
require 'spec_helper'
36
ENV['RAILS_ENV'] ||= 'test'

0 commit comments

Comments
 (0)