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

Use GOVUK_ENVIRONMENT env var to set styling #314

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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.17.1"
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