Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rspec conversion #3243

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3628b82
Fix deprecation warning in test.rb
KludgeKML Jun 27, 2024
757d129
Install RSpec and init
KludgeKML Jun 27, 2024
19488f3
Update spec_helper
KludgeKML Jun 27, 2024
a7f5449
Update CI, Rake to use RSpec
KludgeKML Jun 27, 2024
1286680
TEMP: Add minitest-to-rspec
KludgeKML Jun 27, 2024
2412d48
Convert component tests
KludgeKML Jun 27, 2024
7ebd6e4
Convert asset_manager_redirect_controller test
KludgeKML Jun 27, 2024
466ba32
Convert content_items_controller tests
KludgeKML Jul 1, 2024
c805c67
Add system spec replacement for slimmer tests
KludgeKML Jul 3, 2024
baec668
Convert service_sign_in tests
KludgeKML Jul 1, 2024
ea34454
Convert step_navigation tests
KludgeKML Jul 1, 2024
fcebf17
Convert govuk_content_schema tests
KludgeKML Jul 2, 2024
2c751ab
Convert answer integration test
KludgeKML Jul 2, 2024
7b694f3
Convert recruitment banner unit tests
KludgeKML Jul 3, 2024
f2ed9ab
Convert locales_validation tests
KludgeKML Jul 3, 2024
2510c00
Lint fix (recruitment banner)
KludgeKML Jul 3, 2024
713d35e
Convert publish static pages unit test
KludgeKML Jul 3, 2024
ce71b78
Convert webchat test
KludgeKML Jul 3, 2024
56587b4
Convert request helper service tests
KludgeKML Jul 3, 2024
e30b537
Convert world location base path service tests
KludgeKML Jul 3, 2024
364de2a
Conver application helper tests
KludgeKML Jul 3, 2024
d1f9016
Convert gone helper tests
KludgeKML Jul 3, 2024
4ff3854
Conver manuals helper tests
KludgeKML Jul 3, 2024
0b0dc75
Convert service manual topic helper tests
KludgeKML Jul 3, 2024
c0821bd
Conver statistics announcement helper tests
KludgeKML Jul 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
dependency-review:
name: Dependency Review scan
uses: alphagov/govuk-infrastructure/.github/workflows/dependency-review.yml@main

security-analysis:
name: Security Analysis
uses: alphagov/govuk-infrastructure/.github/workflows/brakeman.yml@main
Expand Down Expand Up @@ -54,5 +54,5 @@ jobs:

test-ruby:
name: Test Ruby
uses: ./.github/workflows/minitest.yml
uses: ./.github/workflows/rspec.yml

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Run Minitest
name: Run RSpec

on:
workflow_call:
Expand All @@ -15,7 +15,7 @@ on:

jobs:
run-minitest:
name: Run Minitest
name: Run RSpec
runs-on: ubuntu-latest
steps:
- name: Checkout repository
Expand All @@ -42,8 +42,8 @@ jobs:
- name: Precompile assets
uses: alphagov/govuk-infrastructure/.github/actions/precompile-rails-assets@main

- name: Run Minitest
- name: Run RSpec
env:
RAILS_ENV: test
GOVUK_CONTENT_SCHEMAS_PATH: vendor/publishing-api/content_schemas
run: bundle exec rake test
run: bundle exec rake spec
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ end
group :development do
gem "better_errors"
gem "binding_of_caller"
gem "minitest_to_rspec"
end

group :test do
Expand All @@ -43,6 +44,7 @@ group :test do
gem "i18n-coverage"
gem "minitest-reporters"
gem "mocha"
gem "rspec-rails"
gem "shoulda-context"
gem "simplecov"
gem "timecop"
Expand Down
32 changes: 32 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ GEM
sass-embedded (~> 1.63)
date (3.3.4)
debug_inspector (1.2.0)
diff-lcs (1.5.1)
docile (1.4.0)
domain_name (0.6.20240107)
drb (2.2.1)
Expand Down Expand Up @@ -234,6 +235,10 @@ GEM
builder
minitest (>= 5.0)
ruby-progressbar
minitest_to_rspec (0.13.0)
ruby2ruby (~> 2.3)
ruby_parser (~> 3.8)
trollop (~> 2.1)
mocha (2.4.0)
ruby2_keywords (>= 0.0.5)
msgpack (1.7.2)
Expand Down Expand Up @@ -541,6 +546,23 @@ GEM
strscan
rinku (2.0.6)
rouge (4.3.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-rails (6.1.3)
actionpack (>= 6.1)
activesupport (>= 6.1)
railties (>= 6.1)
rspec-core (~> 3.13)
rspec-expectations (~> 3.13)
rspec-mocks (~> 3.13)
rspec-support (~> 3.13)
rspec-support (3.13.1)
rss (0.3.0)
rexml
rubocop (1.64.1)
Expand Down Expand Up @@ -576,6 +598,12 @@ GEM
rubocop (~> 1.61)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
ruby2ruby (2.5.0)
ruby_parser (~> 3.1)
sexp_processor (~> 4.6)
ruby_parser (3.21.0)
racc (~> 1.5)
sexp_processor (~> 4.16)
rubyzip (2.3.2)
sanitize (6.1.0)
crass (~> 1.0.2)
Expand All @@ -594,6 +622,7 @@ GEM
sentry-ruby (5.18.0)
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
sexp_processor (4.17.1)
shoulda-context (2.0.0)
simplecov (0.22.0)
docile (~> 1.1)
Expand Down Expand Up @@ -625,6 +654,7 @@ GEM
thor (1.3.1)
timecop (0.9.10)
timeout (0.4.1)
trollop (2.9.10)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
Expand Down Expand Up @@ -664,6 +694,7 @@ DEPENDENCIES
htmlentities
i18n-coverage
minitest-reporters
minitest_to_rspec
mocha
plek
pry-byebug
Expand All @@ -672,6 +703,7 @@ DEPENDENCIES
rails-i18n
rails_translation_manager
rinku
rspec-rails
rss
rubocop-govuk
shoulda-context
Expand Down
9 changes: 8 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

begin
require "rspec/core/rake_task"
RSpec::Core::RakeTask.new
rescue LoadError
# RSpec not available in all environments
end

require File.expand_path("config/application", __dir__)

Rails.application.load_tasks

Rake::Task[:default].clear if Rake::Task.task_defined?(:default)
task default: %i[lint test jasmine]
task default: %i[lint spec jasmine]
2 changes: 1 addition & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
config.cache_store = :null_store

# Raise exceptions instead of rendering exception templates.
config.action_dispatch.show_exceptions = false
config.action_dispatch.show_exceptions = :none

# Disable request forgery protection in test environment.
config.action_controller.allow_forgery_protection = false
Expand Down
54 changes: 54 additions & 0 deletions spec/components/all_components_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
RSpec.describe("AllComponents", type: :view) do
Dir.glob("app/views/components/*.erb").each do |filename|
template = filename.split("/").last
component_name = template.sub("_", "").sub(".html", "").sub(".erb", "").gsub("-", "_")

describe(component_name) do
yaml_file = "#{__dir__}/../../app/views/components/docs/#{component_name}.yml"

it "is documented" do
expect(File).to exist(yaml_file)
end

it "has the correct documentation" do
yaml = YAML.unsafe_load_file(yaml_file)

expect(yaml["name"]).to be_truthy
expect(yaml["description"]).to be_truthy
expect(yaml["examples"]).to be_truthy
expect((yaml["accessibility_criteria"] or yaml["shared_accessibility_criteria"])).to be_truthy
end

it "has the correct class in the ERB template" do
erb = File.read(filename)
class_name = "app-c-#{component_name.dasherize}"

expect(erb).to include(class_name)
end

it "has a correctly named template file" do
template_file = "#{__dir__}/../../app/views/components/_#{component_name}.html.erb"

expect(File).to exist(template_file)
end

it "has a correctly named spec file" do
rspec_file = "#{__dir__}/../../spec/components/#{component_name.tr('-', '_')}_spec.rb"

expect(File).to exist(rspec_file)
end

it "has a correctly named SCSS file" do
css_file = "#{__dir__}/../../app/assets/stylesheets/components/_#{component_name.tr('_', '-')}.scss"

expect(File).to exist(css_file)
end

it "does not use `html_safe`" do
file = File.read(filename)

expect(file).not_to include("html_safe")
end
end
end
end
21 changes: 21 additions & 0 deletions spec/components/back_to_top_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
RSpec.describe("BackToTop", type: :view) do
def component_name
"back_to_top"
end

it "raises an error when no parameters given" do
expect { render_component({}) }.to raise_error(ActionView::Template::Error)
end

it "renders when a href is given" do
render_component(href: "#contents")

assert_select(".app-c-back-to-top[href='#contents']")
end

it "renders with custom text" do
render_component(href: "#contents", text: "Back to top")

assert_select(".app-c-back-to-top[href='#contents']", text: "Back to top")
end
end
47 changes: 47 additions & 0 deletions spec/components/banner_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
RSpec.describe("Banner", type: :view) do
def component_name
"banner"
end

it "raises an error when no parameters given" do
expect { render_component({}) }.to raise_error(ActionView::Template::Error)
end

it "renders with text" do
render_component(title: "Summary", text: "This was published under the 2010 to 2015 Conservative government")

assert_select(".app-c-banner--aside", false)
assert_select(".app-c-banner__desc", text: "This was published under the 2010 to 2015 Conservative government")
end

it "renders with an aria label" do
render_component(title: "Summary", text: "Text")

assert_select("section[aria-label]")
end

it "renders with title and text" do
render_component(title: "Summary", text: "This was published under the 2010 to 2015 Conservative government")

assert_select(".app-c-banner--aside", false)
assert_select(".app-c-banner__title", text: "Summary")
assert_select(".app-c-banner__desc", text: "This was published under the 2010 to 2015 Conservative government")
end

it "renders with title, text and aside" do
render_component(title: "Summary", text: "This was published under the 2010 to 2015 Conservative government", aside: "This consultation ran from 9:30am on 30 January 2017 to 5pm on 28 February 2017")

assert_select(".app-c-banner--aside")
assert_select(".app-c-banner__title", text: "Summary")
assert_select(".app-c-banner__desc", text: "This was published under the 2010 to 2015 Conservative government")
assert_select(".app-c-banner__desc", text: "This consultation ran from 9:30am on 30 January 2017 to 5pm on 28 February 2017")
end

it "renders with GA4 tracking" do
render_component(title: "Summary", text: "This was published under the 2010 to 2015 Conservative government", aside: "This consultation ran from 9:30am on 30 January 2017 to 5pm on 28 February 2017")

assert_select(".app-c-banner--aside[data-module=ga4-link-tracker]")
assert_select(".app-c-banner--aside[data-ga4-track-links-only]")
assert_select(".app-c-banner--aside[data-ga4-link='{\"event_name\":\"navigation\",\"type\":\"callout\"}']")
end
end
47 changes: 47 additions & 0 deletions spec/components/contents_list_with_body_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
RSpec.describe("ContentsListWithBody", type: :view) do
def component_path
"components/contents_list_with_body"
end

def contents_list
[{ href: "/one", text: "1. One" }, { href: "/two", text: "2. Two" }]
end

def block
"<p>Foo</p>".html_safe
end

it "renders nothing without a block" do
expect(render(component_path, contents: contents_list)).to be_empty
end

it "yields the block without contents data" do
assert_includes(render(component_path, {}) { block }, block)
end

it "renders a sticky-element-container" do
render(component_path, contents: contents_list) { block }

assert_select("#contents.app-c-contents-list-with-body")
assert_select("#contents[data-module='sticky-element-container']")
end

it "does not apply the sticky-element-container data-module without contents data" do
render(component_path, {}) { block }

assert_select("#contents[data-module='sticky-element-container']", count: 0)
end

it "renders a contents-list component" do
render(component_path, contents: contents_list) { block }

assert_select(".app-c-contents-list-with-body .gem-c-contents-list")
assert_select(".gem-c-contents-list__link[href='/one']", text: "1. One")
end

it "renders a back-to-top component" do
render(component_path, contents: contents_list) { block }

assert_select(".app-c-contents-list-with-body\n .app-c-contents-list-with-body__link-wrapper\n .app-c-contents-list-with-body__link-container\n .app-c-back-to-top[href='#contents']")
end
end
21 changes: 21 additions & 0 deletions spec/components/download_link_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
RSpec.describe("DownloadLink", type: :view) do
def component_name
"download_link"
end

it "raises an error when no href given" do
expect { render_component({}) }.to raise_error(ActionView::Template::Error)
end

it "renders" do
render_component(href: "/download-me")

assert_select(".app-c-download-link[href=\"/download-me\"]")
end

it "renders with custom link text" do
render_component(href: "/download-map", link_text: "Download this file")

assert_select(".app-c-download-link[href=\"/download-map\"]", text: "Download this file")
end
end
Loading
Loading