-
-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update asset helpers to accept `Hanami::Assets::Asset` instances and use the `#url` from those, as an alternative to looking up assets via string names from the `assets` in the view context.
- Loading branch information
Showing
3 changed files
with
150 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
spec/integration/assets/cross_slice_assets_helpers_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rack/test" | ||
require "stringio" | ||
|
||
RSpec.describe "Cross-slice assets via helpers", :app_integration do | ||
include Rack::Test::Methods | ||
let(:app) { Hanami.app } | ||
let(:root) { make_tmp_directory } | ||
|
||
before do | ||
with_directory(root) do | ||
write "config/app.rb", <<~RUBY | ||
module TestApp | ||
class App < Hanami::App | ||
config.logger.stream = StringIO.new | ||
end | ||
end | ||
RUBY | ||
|
||
write "config/slices/admin.rb", <<~RUBY | ||
module Admin | ||
class Slice < Hanami::Slice | ||
# TODO: we should update `import` to make importing from the app nicer | ||
# TODO: this test failed when I tried doing `as: "app"` (string instead of symbol); fix this in dry-system | ||
import keys: ["assets"], from: Hanami.app.container, as: :app | ||
end | ||
end | ||
RUBY | ||
|
||
write "config/assets.js", <<~JS | ||
import * as assets from "hanami-assets"; | ||
await assets.run(); | ||
JS | ||
|
||
write "package.json", <<~JSON | ||
{ | ||
"type": "module" | ||
} | ||
JSON | ||
|
||
write "app/view.rb", <<~RUBY | ||
# auto_register: false | ||
require "hanami/view" | ||
module TestApp | ||
class View < Hanami::View | ||
config.layout = nil | ||
end | ||
end | ||
RUBY | ||
|
||
write "app/assets/js/app.ts", <<~TS | ||
import "../css/app.css"; | ||
console.log("Hello from index.ts"); | ||
TS | ||
|
||
write "app/assets/css/app.css", <<~CSS | ||
.btn { | ||
background: #f00; | ||
} | ||
CSS | ||
|
||
write "slices/admin/assets/js/app.ts", <<~TS | ||
import "../css/app.css"; | ||
console.log("Hello from admin's index.ts"); | ||
TS | ||
|
||
write "slices/admin/assets/css/app.css", <<~CSS | ||
.btn { | ||
background: #f00; | ||
} | ||
CSS | ||
|
||
write "slices/admin/view.rb", <<~RUBY | ||
# auto_register: false | ||
module Admin | ||
class View < TestApp::View | ||
end | ||
end | ||
RUBY | ||
|
||
write "slices/admin/views/posts/show.rb", <<~RUBY | ||
module Admin | ||
module Views | ||
module Posts | ||
class Show < Admin::View | ||
end | ||
end | ||
end | ||
end | ||
RUBY | ||
|
||
write "slices/admin/views/context.rb", <<~RUBY | ||
# auto_register: false | ||
require "hanami/view" | ||
module Admin | ||
module Views | ||
class Context < Hanami::View::Context | ||
include Deps[app_assets: "app.assets"] | ||
end | ||
end | ||
end | ||
RUBY | ||
|
||
write "slices/admin/templates/posts/show.html.erb", <<~ERB | ||
<%= stylesheet_tag(app_assets["app.css"]) %> | ||
<%= javascript_tag(app_assets["app.js"]) %> | ||
ERB | ||
|
||
before_prepare if respond_to?(:before_prepare) | ||
require "hanami/prepare" | ||
end | ||
end | ||
|
||
specify "assets are available in helpers and in `assets` component" do | ||
compile_assets! | ||
|
||
output = Admin::Slice["views.posts.show"].call.to_s | ||
|
||
expect(output).to match(%r{<link href="/assets/app-[A-Z0-9]{8}.css" type="text/css" rel="stylesheet">}) | ||
expect(output).to match(%r{<script src="/assets/app-[A-Z0-9]{8}.js" type="text/javascript"></script>}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters