Skip to content

Commit

Permalink
Merge pull request #4090 from alphagov/rspec-conversion-test
Browse files Browse the repository at this point in the history
Convert Frontend Test Runner from Minitest to RSpec
  • Loading branch information
KludgeKML authored Jul 30, 2024
2 parents 0c51981 + aaef2fc commit eb281ac
Show file tree
Hide file tree
Showing 176 changed files with 6,915 additions and 7,687 deletions.
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,7 +54,7 @@ jobs:

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

pact-tests:
name: Run Pact tests
Expand Down
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
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ group :development, :test do
end

group :test do
gem "capybara"
gem "govuk_schemas"
gem "i18n-coverage"
gem "mocha"
gem "rails-controller-testing"
gem "shoulda-context"
gem "rspec-rails"
gem "simplecov"
gem "timecop"
gem "webmock"
Expand Down
14 changes: 10 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,14 @@ GEM
rspec-mocks (3.12.6)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-rails (6.1.1)
actionpack (>= 6.1)
activesupport (>= 6.1)
railties (>= 6.1)
rspec-core (~> 3.12)
rspec-expectations (~> 3.12)
rspec-mocks (~> 3.12)
rspec-support (~> 3.12)
rspec-support (3.12.1)
rubocop (1.64.1)
json (~> 2.3)
Expand Down Expand Up @@ -624,7 +632,6 @@ GEM
rubocop-rspec (3.0.1)
rubocop (~> 1.61)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
rubyzip (2.3.2)
sass-embedded (1.69.7)
google-protobuf (~> 3.25)
Expand All @@ -640,7 +647,6 @@ GEM
sentry-ruby (5.18.1)
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
shoulda-context (2.0.0)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
Expand Down Expand Up @@ -703,6 +709,7 @@ DEPENDENCIES
better_errors
binding_of_caller
bootsnap
capybara
climate_control
dalli
dartsass-rails
Expand All @@ -718,7 +725,6 @@ DEPENDENCIES
i18n-coverage
invalid_utf8_rejector
listen
mocha
pact
pact_broker-client
plek
Expand All @@ -727,8 +733,8 @@ DEPENDENCIES
rails-controller-testing
rails-i18n
rails_translation_manager
rspec-rails
rubocop-govuk
shoulda-context
simplecov
slimmer
sprockets-rails
Expand Down
13 changes: 4 additions & 9 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@

begin
require "pact/tasks"
require "rspec/core/rake_task"
RSpec::Core::RakeTask.new
rescue LoadError
# Pact isn't available in all environments
# Pact/RSpec not available in all environments
end

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

Rails.application.load_tasks

Rake::TestTask.new(:test_unit) do |t|
t.libs << "test"
t.test_files = FileList["test/**/*_test.rb"]
t.warning = false
end

Rake::Task[:default].clear if Rake::Task.task_defined?(:default)
task default: %i[test_unit jasmine pact:verify lint]
task default: %i[lint spec jasmine pact:verify]
2 changes: 1 addition & 1 deletion app/controllers/account_home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ class AccountHomeController < ApplicationController
include GovukPersonalisation::ControllerConcern

def show
redirect_with_analytics GovukPersonalisation::Urls.your_account
redirect_with_analytics GovukPersonalisation::Urls.one_login_your_services
end
end
4 changes: 0 additions & 4 deletions app/controllers/concerns/previewable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,4 @@ module Previewable
def set_edition_for_viewing_draft_content
@edition = params[:edition]
end

def viewing_draft_content?
params.include?("edition")
end
end
2 changes: 1 addition & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def create
redirect_path = http_referer_path
redirect_path = nil unless is_valid_redirect_path? redirect_path

return redirect_with_analytics GovukPersonalisation::Urls.your_account unless redirect_path
return redirect_with_analytics GovukPersonalisation::Urls.one_login_your_services unless redirect_path

redirect_with_analytics GdsApi.account_api.get_sign_in_url(redirect_path:)["auth_uri"]
end
Expand Down
7 changes: 0 additions & 7 deletions app/helpers/links_helper.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/models/calendar.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Calendar
REPOSITORY_PATH = Rails.env.test? ? "test/fixtures" : "lib/data"
REPOSITORY_PATH = "lib/data".freeze

class CalendarNotFound < StandardError
end
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/licence_details_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def offered_by_county?
end

def single_licence_authority_present?
licence_authority_specific? && authority
licence_authority_specific? && authority.present?
end

def authority
Expand Down
2 changes: 1 addition & 1 deletion app/views/roadmap/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% content_for :title, t('roadmap.page_title') %>
<% content_for :extra_headers do %>
<meta name="description" content="<%= t('roadmap.hero.description') %>" />
<meta name="twitter:card" content="summary"></meta>
<meta name="twitter:card" content="summary" />
<meta property="og:url" content="https://www.gov.uk/roadmap" />
<meta property="og:title" content="Roadmap - GOV.UK" />
<meta property="og:description" content="<%= t('roadmap.hero.description') %>" />
Expand Down
48 changes: 48 additions & 0 deletions spec/components/all_components_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
RSpec.describe "AllComponents" 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.safe_load_file(yaml_file, permitted_classes: [Time])
expect(yaml["name"]).not_to be_empty
expect(yaml["description"]).not_to be_empty
expect(yaml["examples"]).not_to be_empty
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` in it's partial" do
file = File.read(filename)
expect(file).not_to include("html_safe")
end
end
end
end
47 changes: 47 additions & 0 deletions spec/components/calendar_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
RSpec.describe "CalendarComponent", type: :view do
def component_name
"calendar"
end

it "renders the basic component" do
render_component({})

expect(rendered).to have_css(".app-c-calendar")
end

it "renders the component with correct data passed" do
render_component(
title: "Marvel films",
year: "2008 onwards",
headings: [
{ text: "Date" },
{ text: "Day of the week" },
{ text: "Film" },
],
events: [
{
title: "Iron Man",
date: Date.parse("2-5-2008"),
notes: "first film in the MCU",
},
{
title: "The Incredible Hulk",
date: Date.parse("13-6-2008"),
notes: "",
},
],
)

expect(rendered).to have_css(".govuk-table__caption .govuk-visually-hidden", text: "Marvel films")
expect(rendered).to have_css(".govuk-table__caption", text: "Marvel films 2008 onwards")
expect(rendered).to have_css(".govuk-table__head .govuk-table__header:nth-child(1)", text: "Date")
expect(rendered).to have_css(".govuk-table__head .govuk-table__header:nth-child(2)", text: "Day of the week")
expect(rendered).to have_css(".govuk-table__head .govuk-table__header:nth-child(3)", text: "Film")
expect(rendered).to have_css(".govuk-table__row:nth-child(1) .govuk-table__header", text: "2 May")
expect(rendered).to have_css(".govuk-table__row:nth-child(1) .govuk-table__cell:nth-child(2)", text: "Friday")
expect(rendered).to have_css(".govuk-table__row:nth-child(1) .govuk-table__cell:nth-child(3)", text: "Iron Man (first film in the mcu)")
expect(rendered).to have_css(".govuk-table__row:nth-child(2) .govuk-table__header", text: "13 June")
expect(rendered).to have_css(".govuk-table__row:nth-child(2) .govuk-table__cell:nth-child(2)", text: "Friday")
expect(rendered).to have_css(".govuk-table__row:nth-child(2) .govuk-table__cell:nth-child(3)", text: "The Incredible Hulk")
end
end
34 changes: 34 additions & 0 deletions spec/components/subscribe_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
RSpec.describe "SubscribeComponent", type: :view do
def component_name
"subscribe"
end

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

it "renders the component when link data is passed" do
render_component(
label: "label",
url: "https://www.gov.uk",
title: "title",
)

expect(rendered).to have_css(".app-c-subscribe a[href='https://www.gov.uk'][title='title']", text: "label")
end

it "renders the component with data attributes" do
render_component(
label: "label",
url: "https://www.gov.uk",
title: "title",
data: {
module: "test-module",
ok: "go",
},
)

expect(rendered).to have_css(".app-c-subscribe a[href='https://www.gov.uk'][title='title']", text: "label")
expect(rendered).to have_css(".app-c-subscribe a[data-module='test-module'][data-ok='go']")
end
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
51 changes: 51 additions & 0 deletions spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
RSpec.describe ApplicationHelper do
include ContentStoreHelpers

def dummy_publication
ContentItemPresenter.new(content_store_has_random_item(base_path: "/dummy"))
end

describe "#page_title" do
it "doesn't contain consecutive pipes" do
expect(page_title(dummy_publication)).not_to match(/\|\s*\|/)
end

it "doesn't fail if the publication titles are nil" do
publication = OpenStruct.new(title: nil)

expect(page_title(publication)).to be_truthy
end
end

describe "#wrapper_class" do
it "marks local transactions as a service" do
local_transaction = OpenStruct.new(format: "local_transaction")

expect(wrapper_class(local_transaction).split.include?("service")).to be true
end
end

it "builds title from content items" do
publication = OpenStruct.new(title: "Title")

expect(page_title(publication)).to eq("Title - GOV.UK")
end

it "omits first part of title if publication is omitted" do
expect(page_title).to eq("GOV.UK")
end

describe "#current_path_without_query_string" do
it "returns the path of the current request" do
allow(self).to receive(:request).and_return(ActionDispatch::TestRequest.new("PATH_INFO" => "/foo/bar"))

expect(current_path_without_query_string).to eq("/foo/bar")
end

it "returns the path of the current request stripping off any query string parameters" do
allow(self).to receive(:request).and_return(ActionDispatch::TestRequest.new("PATH_INFO" => "/foo/bar"))

expect(current_path_without_query_string).to eq("/foo/bar")
end
end
end
Loading

0 comments on commit eb281ac

Please sign in to comment.