Skip to content

Commit

Permalink
Add specs (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
hunchr authored Nov 7, 2024
1 parent 2a5bf63 commit 8a3ac8f
Show file tree
Hide file tree
Showing 25 changed files with 155 additions and 81 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ jobs:
with:
ruby-version: ${{ matrix.ruby }}

- name: Install dependencies
run: bundle && bundle exec appraisal install
- name: Set up Hotsheet
run: bin/setup

- name: Run tests
run: bundle exec appraisal bin/check
run: bin/check

lint:
runs-on: ubuntu-latest
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/gemfiles
/spec/dummy/log
/spec/dummy/storage
/spec/dummy/tmp

/Gemfile.lock
/spec/dummy/Gemfile.lock
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ AllCops:
Exclude:
- gemfiles/**/*
- spec/dummy/db/schema.rb

RSpec/ExampleLength:
CountAsOne:
- array
- hash
- method_call
20 changes: 12 additions & 8 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
# frozen_string_literal: true

appraise "rails_6_1" do
gem "rails", "6.1.0"
end
appraise "rails_7_0" do
gem "rails", "~> 7.0.0"

appraise "rails_7" do
gem "rails", "7.0.0"
group :test do
gem "sqlite3", "~> 1.4"
end
end

appraise "rails_7_1" do
gem "rails", "7.1.0"
gem "rails", "~> 7.1.0"

group :test do
gem "sqlite3", "~> 1.4"
end
end

appraise "rails_7_2" do
gem "rails", "7.2.0"
gem "rails", "~> 7.2.0"
end

appraise "rails_8" do
appraise "rails_8_0" do
gem "rails", "8.0.0.rc2"
end
11 changes: 10 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ gemspec

group :test do
gem "appraisal"
gem "better_errors"
gem "binding_of_caller"
gem "capybara"
gem "database_cleaner-active_record", require: false
gem "debug"
gem "faker", require: false
gem "puma"
gem "renuocop", require: false
gem "rspec"
gem "rspec-rails"
gem "selenium-webdriver", require: false
gem "sqlite3"
end
4 changes: 2 additions & 2 deletions app/controllers/hotsheet/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ def update # rubocop:disable Metrics/AbcSize
record = model.find params[:id]

if record.update model_params
flash[:notice] = t("hotsheet.success", record: record)
flash[:notice] = t("hotsheet.success", record: model.model_name.human)
else
flash[:alert] = t("hotsheet.error", record: record,
flash[:alert] = t("hotsheet.error", record: model.model_name.human,
errors: record.errors.full_messages.join(", "))
end

Expand Down
3 changes: 2 additions & 1 deletion app/views/hotsheet/pages/_editable_attribute.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
data-editable-attribute-resource-id-value="<%= record.id %>">
<div class="readonly-attribute"
data-editable-attribute-target="readonlyAttribute"
data-action="click->editable-attribute#displayInputField">
data-action="click->editable-attribute#displayInputField"
role="button">
<%= record.public_send attribute %>
</div>

Expand Down
5 changes: 2 additions & 3 deletions bin/check
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env sh
set -eu
set -e

cd spec/dummy
bundle exec rspec --fail-fast=3 -f documentation
bundle exec appraisal "$@" rspec --fail-fast=3 -f documentation
4 changes: 2 additions & 2 deletions bin/fastcheck
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
set -eu
set -e

bundle exec rubocop -c .rubocop.yml
bundle exec rubocop -c .rubocop.yml "$@"
11 changes: 7 additions & 4 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/usr/bin/env sh
set -eu
set -e

echo ---- Installing dependencies ----
bundle
cd spec/dummy
bundle
bundle exec appraisal install

echo ---- Setting up database ----
bundle exec rails db:drop db:setup
if [ -n "$CI" ]; then
bundle exec rails db:create db:schema:load
else
bundle exec rails db:drop db:setup
fi
2 changes: 1 addition & 1 deletion config/initializers/hotsheet/editable_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Rails.application.config.after_initialize do # rubocop:disable Metrics/BlockLength
# Only run this initializer when running the Rails server
next unless defined? Rails::Server
next unless Rails.env.test? || defined? Rails::Server

Hotsheet.configuration.models.each_key do |model| # rubocop:disable Metrics/BlockLength
model.constantize.class_eval do
Expand Down
6 changes: 0 additions & 6 deletions spec/dummy/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion spec/dummy/.ruby-version

This file was deleted.

14 changes: 0 additions & 14 deletions spec/dummy/Gemfile

This file was deleted.

1 change: 0 additions & 1 deletion spec/dummy/config/credentials.yml.enc

This file was deleted.

15 changes: 5 additions & 10 deletions spec/dummy/config/database.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
default: &default
development:
adapter: sqlite3
database: storage/development.sqlite3
pool: <%= ENV.fetch "RAILS_MAX_THREADS", 5 %>
timeout: 5000

development:
<<: *default
database: storage/development.sqlite3

test:
<<: *default
adapter: sqlite3
database: storage/test.sqlite3

production:
<<: *default
# database: path/to/persistent/storage/production.sqlite3
pool: <%= ENV.fetch "RAILS_MAX_THREADS", 5 %>
timeout: 5000
1 change: 0 additions & 1 deletion spec/dummy/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Rails.application.configure do
config.action_controller.allow_forgery_protection = false
config.action_controller.perform_caching = false
config.action_controller.raise_on_missing_callback_actions = true
config.action_dispatch.show_exceptions = :all
config.action_view.logger = nil
config.active_record.migration_error = :raise
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class CreateAuthorsAndPosts < ActiveRecord::Migration[8.1]
class CreateAuthorsAndPosts < ActiveRecord::Migration[6.1]
def change
authors
posts
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/db/migrate/20241104095444_create_more_models.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class CreateMoreModels < ActiveRecord::Migration[8.1]
class CreateMoreModels < ActiveRecord::Migration[6.1]
def change
overflow_test
different_db_name
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[8.1].define(version: 20_241_104_095_444) do
ActiveRecord::Schema[6.1].define(version: 20_241_104_095_444) do
create_table "authors", force: :cascade do |t|
t.string "name"
t.date "birthdate"
Expand Down
19 changes: 0 additions & 19 deletions spec/dummy/spec/spec_helper.rb

This file was deleted.

20 changes: 20 additions & 0 deletions spec/lib/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe "Hotsheet::Configuration" do
let(:config) do
Hotsheet.configure do |config|
config.model :Author do |model|
model.included_attributes = %i[name birthdate]
model.excluded_attributes = %i[created_at]
end
end
end

it "has included and excluded attributes" do
expect(config).to be_a Hotsheet::Configuration::ModelConfig
expect(config.included_attributes).to eq %i[name birthdate]
expect(config.excluded_attributes).to eq %i[created_at]
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

RSpec.describe Hotsheet do
require "spec_helper"

RSpec.describe "Hotsheet::VERSION" do
it "has a version number" do
expect(Hotsheet::VERSION).not_to be_nil
end
Expand Down
43 changes: 43 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

ENV["RAILS_ENV"] = "test"

require_relative "dummy/config/environment"
require "database_cleaner-active_record"
require "rspec/rails"
require "selenium-webdriver"

%i[firefox firefox_headless].each do |driver|
Capybara.register_driver driver do |app|
args = ["--headless"] if driver == :firefox_headless
options = Selenium::WebDriver::Firefox::Options.new args: args
Capybara::Selenium::Driver.new app, browser: :firefox, options: options
end
end

driver = ENV.fetch("SELENIUM_DRIVER", "firefox_headless").to_sym
Capybara.default_driver = :rack_test
Capybara.javascript_driver = driver
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
Kernel.srand config.seed

config.include Capybara::DSL

config.disable_monkey_patching!
config.filter_rails_from_backtrace!
config.infer_spec_type_from_file_location!
config.mock_with(:rspec) { |mocks| mocks.verify_partial_doubles = true }
config.order = :random
config.run_all_when_everything_filtered = true

config.before :all, type: :system do
FileUtils.rm_rf Rails.root.join "tmp/capybara"
driven_by driver, screen_size: [1280, 800]
end

config.before :each, type: :system do
DatabaseCleaner.clean_with :truncation
end
end
30 changes: 30 additions & 0 deletions spec/system/editable_attributes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe "editable attributes", :js do
let!(:author) { Author.create! name: "Stephen", birthdate: "1947-09-21", gender: "male" }

describe "update strings" do
before do
visit "/hotsheet"
click_link "Author"
find(".readonly-attribute", text: "Stephen").click
fill_in "author_name", with: "King"
end

it "updates attribute when pressing enter" do
find_by_id("author_name").native.send_keys :return

expect(page).to have_content("King")
expect(author.reload.name).to eq("King")
end

it "updates attribute when clicking outside the input" do
find("th", text: "name").click

expect(page).to have_content("King")
expect(author.reload.name).to eq("King")
end
end
end

0 comments on commit 8a3ac8f

Please sign in to comment.