Skip to content

Commit

Permalink
Merge pull request #314 from alphagov/use-govuk-env-for-default-styling
Browse files Browse the repository at this point in the history
Use GOVUK_ENVIRONMENT env var to set styling
  • Loading branch information
theseanything authored Jun 7, 2024
2 parents 3700ec9 + 2867e52 commit 426a2cf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
1 change: 1 addition & 0 deletions govuk_admin_template.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Gem::Specification.new do |gem|
gem.add_dependency "rails", ">= 6"

gem.add_development_dependency "capybara", "~> 3"
gem.add_development_dependency "climate_control", "~> 1"
gem.add_development_dependency "rspec-rails", "~> 5"
gem.add_development_dependency "rubocop-govuk", "4.18.0"
gem.add_development_dependency "sassc-rails", "~> 2"
Expand Down
12 changes: 2 additions & 10 deletions lib/govuk_admin_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,10 @@ module GovukAdminTemplate
mattr_accessor :environment_style, :environment_label

def self.environment_style
@@environment_style || default_environment_style
@environment_style ||= ENV["GOVUK_ENVIRONMENT"] == "production" ? "production" : "preview"
end

def self.environment_label
@@environment_label || environment_style.try(:titleize)
end

# In development we can't consistently set an environment
# variable, so use a default based on Rails.env
def self.default_environment_style
if Rails.env.development?
"development"
end
@environment_label ||= ENV.fetch("GOVUK_ENVIRONMENT", "development").titleize
end
end
47 changes: 30 additions & 17 deletions spec/layout/layout_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
require "climate_control"
require "spec_helper"

describe "Layout" do
subject(:body) { page.body }

before do
# Reset the class instance variables before each test
GovukAdminTemplate.instance_variable_set(:@environment_style, nil)
GovukAdminTemplate.instance_variable_set(:@environment_label, nil)
end

it "yields the specified content" do
visit "/"
expect(body).to include("app_title")
Expand All @@ -16,31 +23,37 @@
expect(page).to have_title "page_title"
end

context "when no environment set" do
it "defaults to not showing any environment details" do
GovukAdminTemplate.environment_style = nil
visit "/"
expect(page).not_to have_selector(".environment-label")
expect(page).not_to have_selector(".environment-message")
expect(page.body).to match(/favicon-.*.png/)
context "when GOVUK_ENVIRONMENT not set" do
it "defaults to development environment details" do
ClimateControl.modify GOVUK_ENVIRONMENT: nil do
visit "/"
expect(page).to have_selector(".environment-label", text: "Development")
expect(page).to have_selector(".environment-preview")
expect(page.body).to match(/favicon-preview.*.png/)
end
end
end

context "when in a development environment" do
context "when GOVUK_ENVIRONMENT is set to production" do
it "includes details about the current environment" do
GovukAdminTemplate.environment_style = "development"
visit "/"
expect(page).to have_selector(".environment-label", text: "Development")
expect(page).to have_selector(".environment-development")
expect(page.body).to match(/favicon-development-.*.png/)
ClimateControl.modify GOVUK_ENVIRONMENT: "production" do
visit "/"
expect(page).to have_selector(".environment-label", text: "Production")
expect(page).to have_selector(".environment-production")
expect(page.body).to match(/favicon-production-.*.png/)
end
end
end

context "when in a test environment" do
context "when GOVUK_ENVIRONMENT is set to integration" do
it "includes details about the current environment" do
GovukAdminTemplate.environment_style = "test"
visit "/"
expect(page.body).to match(/favicon-test-.*.png/)
ClimateControl.modify GOVUK_ENVIRONMENT: "integration" do
visit "/"
p body
expect(page).to have_selector(".environment-label", text: "Integration")
expect(page).to have_selector(".environment-preview")
expect(page.body).to match(/favicon-preview-.*.png/)
end
end
end

Expand Down

0 comments on commit 426a2cf

Please sign in to comment.