Skip to content

Commit

Permalink
New testing module (#774)
Browse files Browse the repository at this point in the history
Introducing a new testing module to replace the external gems
`phlex-testing-capybara` and `phlex-testing-nokogiri`.
  • Loading branch information
joeldrapper authored Sep 7, 2024
1 parent 815c94e commit ceffded
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/phlex/testing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

require "phlex"

module Phlex::Testing
autoload :SGML, "phlex/testing/sgml"
autoload :Capybara, "phlex/testing/capybara"
autoload :Nokogiri, "phlex/testing/nokogiri"
autoload :Nokolexbor, "phlex/testing/nokolexbor"
end
28 changes: 28 additions & 0 deletions lib/phlex/testing/capybara.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require "capybara"

module Phlex::Testing::Capybara
include Phlex::Testing::SGML

def self.included(mod)
if defined?(Minitest::Test) && Minitest::Test > mod
require "capybara/minitest"
include Capybara::Minitest::Assertions
end

if defined?(RSpec::Core::ExampleGroup) && RSpec::Core::ExampleGroup > mod
require "capybara/rspec"
include Capybara::RSpecMatchers
end
end

attr_reader :page
alias_method :component, :page

def render(...)
@page = ::Capybara::Node::Simple.new(
render_to_string(...),
)
end
end
19 changes: 19 additions & 0 deletions lib/phlex/testing/nokogiri.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require "nokogiri"

module Phlex::Testing::Nokogiri
include Phlex::Testing::SGML

def render_fragment(...)
::Nokogiri::HTML5.fragment(
render_to_string(...),
)
end

def render_document(...)
::Nokogiri::HTML5(
render_to_string(...),
)
end
end
19 changes: 19 additions & 0 deletions lib/phlex/testing/nokolexbor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require "nokolexbor"

module Phlex::Testing::Nokolexbor
include Phlex::Testing::SGML

def render_fragment(...)
Nokolexbor::DocumentFragment.parse(
render_to_string(...),
)
end

def render_document(...)
::Nokolexbor::HTML(
render_to_string(...),
)
end
end
9 changes: 9 additions & 0 deletions lib/phlex/testing/sgml.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module Phlex::Testing::SGML
def render_to_string(component, &)
component.call(view_context:, &)
end

def view_context = nil
end

0 comments on commit ceffded

Please sign in to comment.