Skip to content

Commit

Permalink
Add specs
Browse files Browse the repository at this point in the history
  • Loading branch information
hunchr committed Nov 6, 2024
1 parent 268e3cf commit fb19099
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 13 deletions.
15 changes: 12 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,30 @@ on: push
jobs:
test:
runs-on: ubuntu-latest
env:
BUNDLE_PATH: vendor/bundle
strategy:
matrix:
ruby: [3.0, 3.3]
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Restore cache
uses: actions/cache@v4
with:
key: gems
path: |
gemfiles
vendor/bundle
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
ruby-version: ${{ matrix.ruby }}

- name: Install dependencies
run: bundle && cd spec/dummy && bundle && cd ../.. && bundle exec appraisal install
- name: Set up Hotsheet
run: bundle && bundle exec appraisal install && bundle exec rails db:create db:schema:load

- name: Run tests
run: bundle exec appraisal bin/check
Expand Down
8 changes: 7 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ gemspec

group :test do
gem "appraisal"
gem "capybara"
gem "database_cleaner-active_record", require: false
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
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: 6 additions & 0 deletions spec/dummy/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ gem "puma"
gem "sqlite3"

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 "renuocop", require: false
gem "rspec-rails"
gem "selenium-webdriver", require: false
end
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
30 changes: 30 additions & 0 deletions spec/dummy/spec/hotsheet/configuration_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 "Hotsheet::Configuration" do
let(:config) do
Hotsheet.configure do |config|
config.model :User 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

it "doesn't have any other model configs" do # rubocop:disable RSpec/ExampleLength
expect do
Hotsheet.configure do |config|
config.model :User do |model|
model.attributes = %i[name birthdate]
end
end
end.to raise_error NoMethodError
end
end
2 changes: 1 addition & 1 deletion spec/dummy/spec/hotsheet_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

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

ENV["RAILS_ENV"] = "test"
DRIVER = ENV.fetch("SELENIUM_DRIVER", "firefox_headless").to_sym

require_relative "../config/environment"
require "database_cleaner-active_record"
require "rspec/rails"
require_relative "config/environment"
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

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
23 changes: 23 additions & 0 deletions spec/dummy/spec/system/pages_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe "Hotsheet::PagesController", :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"
end

it "can edit attributes" do
find(".readonly-attribute", text: "Stephen").click
fill_in "author_name", with: "King"
find_by_id("author_name").native.send_keys :return

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

0 comments on commit fb19099

Please sign in to comment.