Skip to content

Commit 052f743

Browse files
author
Jake Boxer
committed
Add specs for no live reloading in production
* Add set_rack_env and reset_rack_env spec helpers
1 parent 4072504 commit 052f743

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

spec/application_spec.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,5 +281,63 @@
281281
expect(text_in(response.body, "h2")).to eql("Changed")
282282
end
283283
end
284+
285+
context "in production" do
286+
before :each do
287+
set_rack_env :production
288+
end
289+
290+
after :each do
291+
reset_rack_env
292+
end
293+
294+
it "doesn't reload changed assets" do
295+
root = prepare_fixture(:assets)
296+
app = Hyperloop::Application.new(root)
297+
request = Rack::MockRequest.new(app)
298+
299+
# On the first request, stylesheet should have `display: block` and not
300+
# `display: inline`.
301+
response = request.get("/assets/stylesheets/app.css")
302+
expect(response).to be_ok
303+
expect(response.body).to match(/display: ?block/)
304+
expect(response.body).not_to match(/display: ?inline/)
305+
306+
# Load layout and change the title to "Changed"
307+
asset_path = File.join(root, "app", "assets", "stylesheets", "my-styles.scss")
308+
asset_data = File.read(asset_path)
309+
asset_data.sub!("display: block", "display: inline")
310+
File.write(asset_path, asset_data)
311+
312+
# On the second request, stylesheet should still have `display: inline`
313+
# and not `display: block`.
314+
response = request.get("/assets/stylesheets/app.css")
315+
expect(response).to be_ok
316+
expect(response.body).to match(/display: ?block/)
317+
expect(response.body).not_to match(/display: ?inline/)
318+
end
319+
320+
it "doesn't reload changed views" do
321+
root = prepare_fixture(:erb)
322+
app = Hyperloop::Application.new(root)
323+
request = Rack::MockRequest.new(app)
324+
325+
# On the first request, <title> text should be "ERB"
326+
response = request.get("/")
327+
expect(response).to be_ok
328+
expect(text_in(response.body, "title")).to eql("ERB")
329+
330+
# Load index.html.erb and change the title to "Changed"
331+
index_file_path = File.join(root, "app", "views", "index.html.erb")
332+
index_file_data = File.read(index_file_path)
333+
index_file_data.sub!(/<h2>[^<]*<\/h2>/, "<h2>Changed</h2>")
334+
File.write(index_file_path, index_file_data)
335+
336+
# On the second request, <title> text should still be "ERB"
337+
response = request.get("/")
338+
expect(response).to be_ok
339+
expect(text_in(response.body, "title")).to eql("ERB")
340+
end
341+
end
284342
end
285343
end

spec/spec_helper.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@ def prepare_fixture(name)
4242
tmp_root
4343
end
4444

45+
# Public: Set the RACK_ENV environment variable back to whatever it was when
46+
# the spec started running. If RACK_ENV wasn't set before the spec started
47+
# running, it will be deleted.
48+
#
49+
# Returns nothing.
50+
def reset_rack_env
51+
if defined?(@old_rack_env)
52+
ENV["RACK_ENV"] = @old_rack_env
53+
else
54+
ENV.delete("RACK_ENV")
55+
end
56+
end
57+
58+
# Public: Set the RACK_ENV environment variable to the specified value.
59+
#
60+
# name - Symbol environment name to set RACK_ENV to. Should be :development,
61+
# :test, or :production.
62+
#
63+
# Returns nothing.
64+
def set_rack_env(name)
65+
@old_rack_env = ENV["RACK_ENV"] if ENV.key?("RACK_ENV")
66+
ENV["RACK_ENV"] = name.to_s
67+
end
68+
4569
def text_in(html_str, selector)
4670
node = html(html_str).at_css(selector)
4771
node && node.text

0 commit comments

Comments
 (0)