Skip to content

Commit

Permalink
added specs for all_fixtures.js.erb, improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Crismali committed Apr 23, 2015
1 parent bbb2048 commit 9b45544
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
10 changes: 10 additions & 0 deletions app/assets/javascripts/magic_lamp/all_fixtures.js.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
<% all_fixtures = MagicLamp.generate_all_fixtures.to_json rescue nil %>

<% if all_fixtures %>

MagicLamp.genie.cacheOnly = true;
MagicLamp.genie.cache = <%= MagicLamp.generate_all_fixtures.to_json %>;

<% else %>

throw new Error(MagicLamp.genericError);

<% end %>
2 changes: 1 addition & 1 deletion app/assets/javascripts/magic_lamp/genie.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
if (this.xhrStatus(xhr) === 400) {
this.handleError(xhr.responseText);
} else if (this.xhrStatus(xhr) > 400) {
this.handleError('Something went wrong, please check the server log or run `rake magic_lamp:lint` for more information');
this.handleError(MagicLamp.genericError);
}
return xhr;
},
Expand Down
4 changes: 3 additions & 1 deletion app/assets/javascripts/magic_lamp/magic_lamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

preload: function() {
this.genie.preload.apply(this.genie, arguments);
}
},

genericError: 'Something went wrong, please check the server log, run `rake magic_lamp:lint`, or visit `/magic_lamp/lint` for more information'
};

MagicLamp.clean = function() {
Expand Down
4 changes: 2 additions & 2 deletions spec/javascripts/genie_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,15 +308,15 @@ describe('Genie', function() {
stub(subject, 'xhrStatus', 500);
var path = '/magic_lamp/foo/bar';
subject.xhrRequest(path);
expect(subject.handleError).to.have.been.calledWith('Something went wrong, please check the server log or run `rake magic_lamp:lint` for more information');
expect(subject.handleError).to.have.been.calledWith(MagicLamp.genericError);
});

it('calls handleError with the default error message if the status was 404', function() {
stub(subject, 'handleError', true);
stub(subject, 'xhrStatus', 404);
var path = '/magic_lamp/foo/bar';
subject.xhrRequest(path);
expect(subject.handleError).to.have.been.calledWith('Something went wrong, please check the server log or run `rake magic_lamp:lint` for more information');
expect(subject.handleError).to.have.been.calledWith(MagicLamp.genericError);
});
});

Expand Down
32 changes: 32 additions & 0 deletions spec/lib/all_fixtures_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require "rails_helper"

describe "All fixtures js erb" do
subject do
sprockets_env = Sprockets::Environment.new
sprockets_env.append_path("app/assets/javascripts/")
sprockets_env["magic_lamp/all_fixtures.js"].to_s
end

it "sets cache only to true" do
expect(subject).to match(/MagicLamp.genie.cacheOnly = true/)
end

it "provides all of the fixtures as json in the cache" do
excaped_json = Regexp.escape(MagicLamp.generate_all_fixtures.to_json)
expect(subject).to match(/MagicLamp.genie.cache = #{excaped_json}/)
end

it "does not throw an error" do
expect(subject).to_not match(/throw new Error\(MagicLamp.genericError\)/)
end

context "errors" do
before do
allow(MagicLamp).to receive(:generate_all_fixtures).and_raise("Some error")
end

it "throws an error" do
expect(subject).to match(/throw new Error\(MagicLamp.genericError\)/)
end
end
end

0 comments on commit 9b45544

Please sign in to comment.