-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mise en place de la première spec (#17)
- Loading branch information
1 parent
4aafdef
commit 53ef576
Showing
7 changed files
with
124 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,5 @@ jobs: | |
bundler-cache: true | ||
- name: Linting | ||
run: bundle exec rubocop | ||
- name: Run tests | ||
run: bundle exec rspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--require spec_helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'spec_helper' | ||
|
||
class TestHelper | ||
include ActionView::Helpers::FormHelper | ||
include ActionView::Helpers::FormTagHelper | ||
end | ||
|
||
class Record | ||
include ActiveModel::Model | ||
attr_accessor :name | ||
end | ||
|
||
RSpec.describe Dsfr::FormBuilder do | ||
let(:helper) { TestHelper.new } | ||
let(:object) { Record.new(name: 'Jean Paul') } | ||
let(:builder) { Dsfr::FormBuilder.new(:record, object, helper, {}) } | ||
|
||
describe '#dsfr_text_field' do | ||
it 'generates the correct HTML' do | ||
expect(builder.dsfr_text_field(:name)).to match_html(<<~HTML) | ||
<div class="fr-input-group"> | ||
<label class="fr-label " for="record_name">Name</label> | ||
<input class="fr-input" type="text" value="Jean Paul" name="record[name]" id="record_name" /> | ||
</div> | ||
HTML | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
RSpec.configure do |config| | ||
config.expect_with :rspec do |expectations| | ||
expectations.include_chain_clauses_in_custom_matcher_descriptions = true | ||
end | ||
|
||
config.mock_with :rspec do |mocks| | ||
mocks.verify_partial_doubles = true | ||
end | ||
|
||
config.shared_context_metadata_behavior = :apply_to_host_groups | ||
config.disable_monkey_patching! | ||
config.warnings = true | ||
end | ||
|
||
require 'uri' # necessary on github actions | ||
require 'action_view' | ||
require 'active_support' | ||
require 'active_model' | ||
require 'nokogiri' | ||
|
||
Dir[File.expand_path('{../lib/**/*.rb,support/**/*.rb}', __dir__)].sort.each { |f| require f } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
RSpec::Matchers.define :match_html do |expected| | ||
def normalize_html(html) | ||
# important to strip around the raw HTML input | ||
doc = Nokogiri::HTML.fragment(html.strip) | ||
# strip all attributes values | ||
doc.traverse do |node| | ||
if node.respond_to?(:attributes) | ||
node.attributes.each do |name, attr| | ||
attr.value = attr.value.strip | ||
end | ||
end | ||
end | ||
# remove all whitespaces between tags | ||
doc.to_s.gsub(/>\s+</, '><') | ||
end | ||
|
||
def beautify_html(html) | ||
# cf https://stackoverflow.com/a/7839017 | ||
Nokogiri::XML(html, &:noblanks).to_s.sub('<?xml version="1.0"?>', '') | ||
end | ||
|
||
match do |actual| | ||
@actual = actual | ||
normalize_html(expected) == normalize_html(actual) | ||
end | ||
|
||
failure_message do | ||
<<~MSG | ||
--- Expected --- | ||
#{beautify_html(expected)} | ||
--- Actual --- | ||
#{beautify_html(@actual)} | ||
MSG | ||
end | ||
end |