hanami 2.0.3 → 2.1.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +37 -2
  3. data/LICENSE.md +1 -1
  4. data/README.md +26 -10
  5. data/hanami.gemspec +2 -2
  6. data/lib/hanami/app.rb +5 -0
  7. data/lib/hanami/config/actions.rb +4 -11
  8. data/lib/hanami/config/assets.rb +84 -0
  9. data/lib/hanami/config/null_config.rb +3 -0
  10. data/lib/hanami/config/views.rb +0 -4
  11. data/lib/hanami/config.rb +71 -5
  12. data/lib/hanami/extensions/action/slice_configured_action.rb +15 -7
  13. data/lib/hanami/extensions/action.rb +8 -6
  14. data/lib/hanami/extensions/router/errors.rb +58 -0
  15. data/lib/hanami/extensions/view/context.rb +129 -60
  16. data/lib/hanami/extensions/view/part.rb +26 -0
  17. data/lib/hanami/extensions/view/scope.rb +26 -0
  18. data/lib/hanami/extensions/view/slice_configured_context.rb +0 -2
  19. data/lib/hanami/extensions/view/slice_configured_helpers.rb +44 -0
  20. data/lib/hanami/extensions/view/slice_configured_view.rb +106 -21
  21. data/lib/hanami/extensions/view/standard_helpers.rb +18 -0
  22. data/lib/hanami/extensions.rb +10 -3
  23. data/lib/hanami/helpers/assets_helper.rb +752 -0
  24. data/lib/hanami/helpers/form_helper/form_builder.rb +1391 -0
  25. data/lib/hanami/helpers/form_helper/values.rb +75 -0
  26. data/lib/hanami/helpers/form_helper.rb +213 -0
  27. data/lib/hanami/middleware/assets.rb +21 -0
  28. data/lib/hanami/middleware/public_errors_app.rb +75 -0
  29. data/lib/hanami/middleware/render_errors.rb +90 -0
  30. data/lib/hanami/providers/assets.rb +44 -0
  31. data/lib/hanami/rake_tasks.rb +19 -18
  32. data/lib/hanami/settings.rb +1 -1
  33. data/lib/hanami/slice.rb +48 -2
  34. data/lib/hanami/slice_configurable.rb +3 -2
  35. data/lib/hanami/version.rb +1 -1
  36. data/lib/hanami/web/rack_logger.rb +1 -1
  37. data/lib/hanami.rb +3 -3
  38. data/spec/integration/action/view_rendering/view_context_spec.rb +221 -0
  39. data/spec/integration/action/view_rendering_spec.rb +0 -18
  40. data/spec/integration/assets/assets_spec.rb +101 -0
  41. data/spec/integration/assets/serve_static_assets_spec.rb +152 -0
  42. data/spec/integration/logging/exception_logging_spec.rb +115 -0
  43. data/spec/integration/logging/notifications_spec.rb +68 -0
  44. data/spec/integration/logging/request_logging_spec.rb +128 -0
  45. data/spec/integration/rack_app/middleware_spec.rb +22 -22
  46. data/spec/integration/rack_app/rack_app_spec.rb +3 -220
  47. data/spec/integration/rake_tasks_spec.rb +107 -0
  48. data/spec/integration/view/config/default_context_spec.rb +149 -0
  49. data/spec/integration/view/{inflector_spec.rb → config/inflector_spec.rb} +1 -1
  50. data/spec/integration/view/config/part_class_spec.rb +147 -0
  51. data/spec/integration/view/config/part_namespace_spec.rb +103 -0
  52. data/spec/integration/view/config/paths_spec.rb +119 -0
  53. data/spec/integration/view/config/scope_class_spec.rb +147 -0
  54. data/spec/integration/view/config/scope_namespace_spec.rb +103 -0
  55. data/spec/integration/view/config/template_spec.rb +38 -0
  56. data/spec/integration/view/context/assets_spec.rb +3 -9
  57. data/spec/integration/view/context/request_spec.rb +3 -7
  58. data/spec/integration/view/helpers/form_helper_spec.rb +174 -0
  59. data/spec/integration/view/helpers/part_helpers_spec.rb +124 -0
  60. data/spec/integration/view/helpers/scope_helpers_spec.rb +84 -0
  61. data/spec/integration/view/helpers/user_defined_helpers/part_helpers_spec.rb +162 -0
  62. data/spec/integration/view/helpers/user_defined_helpers/scope_helpers_spec.rb +119 -0
  63. data/spec/integration/view/slice_configuration_spec.rb +9 -9
  64. data/spec/integration/web/render_detailed_errors_spec.rb +107 -0
  65. data/spec/integration/web/render_errors_spec.rb +242 -0
  66. data/spec/spec_helper.rb +1 -1
  67. data/spec/support/app_integration.rb +46 -2
  68. data/spec/support/matchers.rb +32 -0
  69. data/spec/unit/hanami/config/actions/content_security_policy_spec.rb +24 -36
  70. data/spec/unit/hanami/config/actions/csrf_protection_spec.rb +4 -3
  71. data/spec/unit/hanami/config/actions/default_values_spec.rb +3 -6
  72. data/spec/unit/hanami/config/render_detailed_errors_spec.rb +25 -0
  73. data/spec/unit/hanami/config/render_errors_spec.rb +25 -0
  74. data/spec/unit/hanami/config/views_spec.rb +0 -18
  75. data/spec/unit/hanami/env_spec.rb +11 -25
  76. data/spec/unit/hanami/extensions/view/context_spec.rb +59 -0
  77. data/spec/unit/hanami/helpers/assets_helper/asset_url_spec.rb +109 -0
  78. data/spec/unit/hanami/helpers/assets_helper/audio_tag_spec.rb +132 -0
  79. data/spec/unit/hanami/helpers/assets_helper/favicon_link_tag_spec.rb +91 -0
  80. data/spec/unit/hanami/helpers/assets_helper/image_tag_spec.rb +92 -0
  81. data/spec/unit/hanami/helpers/assets_helper/javascript_tag_spec.rb +143 -0
  82. data/spec/unit/hanami/helpers/assets_helper/stylesheet_link_tag_spec.rb +126 -0
  83. data/spec/unit/hanami/helpers/assets_helper/video_tag_spec.rb +132 -0
  84. data/spec/unit/hanami/helpers/form_helper_spec.rb +2826 -0
  85. data/spec/unit/hanami/router/errors/not_allowed_error_spec.rb +27 -0
  86. data/spec/unit/hanami/router/errors/not_found_error_spec.rb +22 -0
  87. data/spec/unit/hanami/slice_configurable_spec.rb +18 -0
  88. data/spec/unit/hanami/version_spec.rb +1 -1
  89. data/spec/unit/hanami/web/rack_logger_spec.rb +1 -1
  90. metadata +95 -35
  91. data/lib/hanami/assets/app_config.rb +0 -61
  92. data/lib/hanami/assets/config.rb +0 -53
  93. data/spec/integration/action/view_integration_spec.rb +0 -165
  94. data/spec/integration/view/part_namespace_spec.rb +0 -96
  95. data/spec/integration/view/path_spec.rb +0 -56
  96. data/spec/integration/view/template_spec.rb +0 -68
  97. data/spec/isolation/hanami/application/already_configured_spec.rb +0 -19
  98. data/spec/isolation/hanami/application/inherit_anonymous_class_spec.rb +0 -10
  99. data/spec/isolation/hanami/application/inherit_concrete_class_spec.rb +0 -14
  100. data/spec/isolation/hanami/application/not_configured_spec.rb +0 -9
  101. data/spec/isolation/hanami/application/routes/configured_spec.rb +0 -44
  102. data/spec/isolation/hanami/application/routes/not_configured_spec.rb +0 -16
  103. data/spec/isolation/hanami/boot/success_spec.rb +0 -50
@@ -0,0 +1,59 @@
1
+ require "hanami/view"
2
+ require "hanami/view/context"
3
+ require "hanami/extensions/view/context"
4
+
5
+ RSpec.describe(Hanami::View::Context) do
6
+ subject(:context) { described_class.new(**args) }
7
+ let(:args) { {} }
8
+
9
+ describe "#assets" do
10
+ context "assets given" do
11
+ let(:args) { {assets: assets} }
12
+ let(:assets) { double(:assets) }
13
+
14
+ it "returns the assets" do
15
+ expect(context.assets).to be assets
16
+ end
17
+ end
18
+
19
+ context "no assets given" do
20
+ it "raises a Hanami::ComponentLoadError" do
21
+ expect { context.assets }.to raise_error Hanami::ComponentLoadError
22
+ end
23
+ end
24
+ end
25
+
26
+ describe "#request" do
27
+ context "request given" do
28
+ let(:args) { {request: request} }
29
+ let(:request) { double(:request) }
30
+
31
+ it "returns the request" do
32
+ expect(context.request).to be request
33
+ end
34
+ end
35
+
36
+ context "no request given" do
37
+ it "raises a Hanami::ComponentLoadError" do
38
+ expect { context.request }.to raise_error Hanami::ComponentLoadError
39
+ end
40
+ end
41
+ end
42
+
43
+ describe "#routes" do
44
+ context "routes given" do
45
+ let(:args) { {routes: routes} }
46
+ let(:routes) { double(:routes) }
47
+
48
+ it "returns the routes" do
49
+ expect(context.routes).to be routes
50
+ end
51
+ end
52
+
53
+ context "no routes given" do
54
+ it "raises a Hanami::ComponentLoadError" do
55
+ expect { context.routes }.to raise_error Hanami::ComponentLoadError
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Hanami::Helpers::AssetsHelper, "#asset_url", :app_integration do
4
+ subject(:obj) {
5
+ helpers = described_class
6
+ Class.new {
7
+ include helpers
8
+
9
+ attr_reader :_context
10
+
11
+ def initialize(context)
12
+ @_context = context
13
+ end
14
+ }.new(context)
15
+ }
16
+
17
+ def asset_url(...)
18
+ subject.asset_url(...)
19
+ end
20
+
21
+ let(:context) { TestApp::Views::Context.new }
22
+ let(:root) { make_tmp_directory }
23
+
24
+ before do
25
+ with_directory(root) do
26
+ write "config/app.rb", <<~RUBY
27
+ module TestApp
28
+ class App < Hanami::App
29
+ config.logger.stream = StringIO.new
30
+ end
31
+ end
32
+ RUBY
33
+
34
+ write "app/views/context.rb", <<~RUBY
35
+ # auto_register: false
36
+
37
+ require "hanami/view/context"
38
+
39
+ module TestApp
40
+ module Views
41
+ class Context < Hanami::View::Context
42
+ end
43
+ end
44
+ end
45
+ RUBY
46
+
47
+ write "app/assets/js/app.ts", <<~JS
48
+ import "../css/app.css";
49
+
50
+ console.log("Hello from index.ts");
51
+ JS
52
+
53
+ write "app/assets/css/app.css", <<~CSS
54
+ .btn {
55
+ background: #f00;
56
+ }
57
+ CSS
58
+
59
+ stub_assets("app.js")
60
+
61
+ require "hanami/setup"
62
+ before_prepare if respond_to?(:before_prepare)
63
+ require "hanami/prepare"
64
+ end
65
+ end
66
+
67
+ context "when configurated relative path only" do
68
+ context "without manifest" do
69
+ it "returns the relative URL to the asset" do
70
+ expect(asset_url("app.js")).to eq("/assets/app.js")
71
+ end
72
+
73
+ it "returns absolute URL if the argument is an absolute URL" do
74
+ result = asset_url("http://assets.hanamirb.org/assets/application.css")
75
+ expect(result).to eq("http://assets.hanamirb.org/assets/application.css")
76
+ end
77
+ end
78
+
79
+ context "with manifest" do
80
+ before { compile_assets! }
81
+
82
+ it "returns the relative URL to the asset" do
83
+ expect(asset_url("app.js")).to match(%r{/assets/app-[A-Z0-9]{8}\.js})
84
+ end
85
+ end
86
+ end
87
+
88
+ context "when configured with base url" do
89
+ let(:base_url) { "https://hanami.test" }
90
+
91
+ def before_prepare
92
+ Hanami.app.config.assets.base_url = base_url
93
+ end
94
+
95
+ context "without manifest" do
96
+ it "returns the absolute URL to the asset" do
97
+ expect(asset_url("app.js")).to eq("#{base_url}/assets/app.js")
98
+ end
99
+ end
100
+
101
+ context "with manifest" do
102
+ before { compile_assets! }
103
+
104
+ it "returns the relative path to the asset" do
105
+ expect(asset_url("app.js")).to match(%r{#{base_url}/assets/app-[A-Z0-9]{8}.js})
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Hanami::Helpers::AssetsHelper, "#audio_tag", :app_integration do
4
+ subject(:obj) {
5
+ helpers = described_class
6
+ Class.new {
7
+ include helpers
8
+
9
+ attr_reader :_context
10
+
11
+ def initialize(context)
12
+ @_context = context
13
+ end
14
+ }.new(context)
15
+ }
16
+
17
+ def audio_tag(...)
18
+ subject.audio_tag(...)
19
+ end
20
+
21
+ let(:context) { TestApp::Views::Context.new }
22
+ let(:root) { make_tmp_directory }
23
+
24
+ before do
25
+ with_directory(root) do
26
+ write "config/app.rb", <<~RUBY
27
+ module TestApp
28
+ class App < Hanami::App
29
+ config.logger.stream = StringIO.new
30
+ end
31
+ end
32
+ RUBY
33
+
34
+ write "app/views/context.rb", <<~RUBY
35
+ # auto_register: false
36
+
37
+ require "hanami/view/context"
38
+
39
+ module TestApp
40
+ module Views
41
+ class Context < Hanami::View::Context
42
+ end
43
+ end
44
+ end
45
+ RUBY
46
+
47
+ stub_assets("song.ogg", "song.pt-BR.vtt")
48
+
49
+ require "hanami/setup"
50
+ before_prepare if respond_to?(:before_prepare)
51
+ require "hanami/prepare"
52
+ end
53
+ end
54
+
55
+ it "returns an instance of HtmlBuilder" do
56
+ actual = audio_tag("song.ogg")
57
+ expect(actual).to be_instance_of(::Hanami::View::HTML::SafeString)
58
+ end
59
+
60
+ it "renders <audio> tag" do
61
+ actual = audio_tag("song.ogg").to_s
62
+ expect(actual).to eq(%(<audio src="/assets/song.ogg"></audio>))
63
+ end
64
+
65
+ it "renders with html attributes" do
66
+ actual = audio_tag("song.ogg", autoplay: true, controls: true).to_s
67
+ expect(actual).to eq(%(<audio autoplay="autoplay" controls="controls" src="/assets/song.ogg"></audio>))
68
+ end
69
+
70
+ it "renders with fallback content" do
71
+ actual = audio_tag("song.ogg") do
72
+ "Your browser does not support the audio tag"
73
+ end.to_s
74
+
75
+ expect(actual).to eq(%(<audio src="/assets/song.ogg">Your browser does not support the audio tag</audio>))
76
+ end
77
+
78
+ it "renders with tracks" do
79
+ actual = audio_tag("song.ogg") do
80
+ tag.track kind: "captions", src: subject.asset_url("song.pt-BR.vtt"), srclang: "pt-BR", label: "Portuguese"
81
+ end.to_s
82
+
83
+ expect(actual).to eq(%(<audio src="/assets/song.ogg"><track kind="captions" src="/assets/song.pt-BR.vtt" srclang="pt-BR" label="Portuguese"></audio>))
84
+ end
85
+
86
+ xit "renders with sources" do
87
+ actual = audio_tag do
88
+ tag.text "Your browser does not support the audio tag"
89
+ tag.source src: subject.asset_url("song.ogg"), type: "audio/ogg"
90
+ tag.source src: subject.asset_url("song.wav"), type: "audio/wav"
91
+ end.to_s
92
+
93
+ expect(actual).to eq(%(<audio>Your browser does not support the audio tag<source src="/assets/song.ogg" type="audio/ogg"><source src="/assets/song.wav" type="audio/wav"></audio>))
94
+ end
95
+
96
+ it "raises an exception when no arguments" do
97
+ expect do
98
+ audio_tag
99
+ end.to raise_error(
100
+ ArgumentError,
101
+ "You should provide a source via `src` option or with a `source` HTML tag"
102
+ )
103
+ end
104
+
105
+ it "raises an exception when no src and no block" do
106
+ expect do
107
+ audio_tag(controls: true)
108
+ end.to raise_error(
109
+ ArgumentError,
110
+ "You should provide a source via `src` option or with a `source` HTML tag"
111
+ )
112
+ end
113
+
114
+ describe "cdn mode" do
115
+ let(:base_url) { "https://hanami.test" }
116
+
117
+ def before_prepare
118
+ Hanami.app.config.assets.base_url = base_url
119
+ end
120
+
121
+ it "returns absolute url for src attribute" do
122
+ actual = audio_tag("song.ogg").to_s
123
+ expect(actual).to eq(%(<audio src="#{base_url}/assets/song.ogg"></audio>))
124
+ end
125
+ end
126
+
127
+ private
128
+
129
+ def tag(...)
130
+ subject.__send__(:tag, ...)
131
+ end
132
+ end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Hanami::Helpers::AssetsHelper, "#favicon_link_tag", :app_integration do
4
+ subject(:obj) {
5
+ helpers = described_class
6
+ Class.new {
7
+ include helpers
8
+
9
+ attr_reader :_context
10
+
11
+ def initialize(context)
12
+ @_context = context
13
+ end
14
+ }.new(context)
15
+ }
16
+
17
+ def favicon_link_tag(...)
18
+ obj.instance_eval { favicon_link_tag(...) }
19
+ end
20
+
21
+ let(:root) { make_tmp_directory }
22
+ let(:context) { TestApp::Views::Context.new }
23
+
24
+ before do
25
+ with_directory(root) do
26
+ write "config/app.rb", <<~RUBY
27
+ module TestApp
28
+ class App < Hanami::App
29
+ config.logger.stream = StringIO.new
30
+ end
31
+ end
32
+ RUBY
33
+
34
+ write "app/views/context.rb", <<~RUBY
35
+ # auto_register: false
36
+
37
+ require "hanami/view/context"
38
+
39
+ module TestApp
40
+ module Views
41
+ class Context < Hanami::View::Context
42
+ end
43
+ end
44
+ end
45
+ RUBY
46
+
47
+ stub_assets("favicon.ico", "favicon.png")
48
+
49
+ require "hanami/setup"
50
+ before_prepare if respond_to?(:before_prepare)
51
+ require "hanami/prepare"
52
+ end
53
+ end
54
+
55
+ it "returns an instance of SafeString" do
56
+ actual = favicon_link_tag
57
+ expect(actual).to be_instance_of(::Hanami::View::HTML::SafeString)
58
+ end
59
+
60
+ it "is aliased as `favicon`" do
61
+ expect(subject.favicon).to eq favicon_link_tag
62
+ end
63
+
64
+ it "renders <link> tag" do
65
+ actual = favicon_link_tag.to_s
66
+ expect(actual).to eq(%(<link href="/assets/favicon.ico" rel="shortcut icon" type="image/x-icon">))
67
+ end
68
+
69
+ it "renders with HTML attributes" do
70
+ actual = favicon_link_tag("favicon.png", rel: "icon", type: "image/png").to_s
71
+ expect(actual).to eq(%(<link href="/assets/favicon.png" rel="icon" type="image/png">))
72
+ end
73
+
74
+ it "ignores href passed as an option" do
75
+ actual = favicon_link_tag("favicon.png", href: "wrong").to_s
76
+ expect(actual).to eq(%(<link href="/assets/favicon.png" rel="shortcut icon" type="image/x-icon">))
77
+ end
78
+
79
+ describe "cdn mode" do
80
+ let(:base_url) { "https://hanami.test" }
81
+
82
+ def before_prepare
83
+ Hanami.app.config.assets.base_url = "https://hanami.test"
84
+ end
85
+
86
+ it "returns absolute url for href attribute" do
87
+ actual = favicon_link_tag.to_s
88
+ expect(actual).to eq(%(<link href="#{base_url}/assets/favicon.ico" rel="shortcut icon" type="image/x-icon">))
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Hanami::Helpers::AssetsHelper, "#image_tag", :app_integration do
4
+ subject(:obj) {
5
+ helpers = described_class
6
+ Class.new {
7
+ include helpers
8
+
9
+ attr_reader :_context
10
+
11
+ def initialize(context)
12
+ @_context = context
13
+ end
14
+ }.new(context)
15
+ }
16
+
17
+ def image_tag(...)
18
+ subject.image_tag(...)
19
+ end
20
+
21
+ let(:root) { make_tmp_directory }
22
+ let(:context) { TestApp::Views::Context.new }
23
+
24
+ before do
25
+ with_directory(root) do
26
+ write "config/app.rb", <<~RUBY
27
+ module TestApp
28
+ class App < Hanami::App
29
+ config.logger.stream = StringIO.new
30
+ end
31
+ end
32
+ RUBY
33
+
34
+ write "app/views/context.rb", <<~RUBY
35
+ # auto_register: false
36
+
37
+ require "hanami/view/context"
38
+
39
+ module TestApp
40
+ module Views
41
+ class Context < Hanami::View::Context
42
+ end
43
+ end
44
+ end
45
+ RUBY
46
+
47
+ stub_assets("application.jpg")
48
+
49
+ require "hanami/setup"
50
+ before_prepare if respond_to?(:before_prepare)
51
+ require "hanami/prepare"
52
+ end
53
+ end
54
+
55
+ it "returns an instance of HtmlBuilder" do
56
+ actual = image_tag("application.jpg")
57
+ expect(actual).to be_instance_of(::Hanami::View::HTML::SafeString)
58
+ end
59
+
60
+ it "renders an <img> tag" do
61
+ actual = image_tag("application.jpg").to_s
62
+ expect(actual).to eq(%(<img src="/assets/application.jpg" alt="Application">))
63
+ end
64
+
65
+ it "custom alt" do
66
+ actual = image_tag("application.jpg", alt: "My Alt").to_s
67
+ expect(actual).to eq(%(<img src="/assets/application.jpg" alt="My Alt">))
68
+ end
69
+
70
+ it "custom data attribute" do
71
+ actual = image_tag("application.jpg", "data-user-id" => 5).to_s
72
+ expect(actual).to eq(%(<img src="/assets/application.jpg" alt="Application" data-user-id="5">))
73
+ end
74
+
75
+ it "ignores src passed as an option" do
76
+ actual = image_tag("application.jpg", src: "wrong").to_s
77
+ expect(actual).to eq(%(<img src="/assets/application.jpg" alt="Application">))
78
+ end
79
+
80
+ describe "cdn mode" do
81
+ let(:base_url) { "https://hanami.test" }
82
+
83
+ def before_prepare
84
+ Hanami.app.config.assets.base_url = "https://hanami.test"
85
+ end
86
+
87
+ it "returns absolute url for src attribute" do
88
+ actual = image_tag("application.jpg").to_s
89
+ expect(actual).to eq(%(<img src="#{base_url}/assets/application.jpg" alt="Application">))
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,143 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Hanami::Helpers::AssetsHelper, "#javascript_tag", :app_integration do
4
+ subject(:obj) {
5
+ helpers = described_class
6
+ Class.new {
7
+ include helpers
8
+
9
+ attr_reader :_context
10
+
11
+ def initialize(context)
12
+ @_context = context
13
+ end
14
+ }.new(context)
15
+ }
16
+
17
+ def javascript_tag(...)
18
+ subject.javascript_tag(...)
19
+ end
20
+
21
+ let(:context) { TestApp::Views::Context.new }
22
+ let(:root) { make_tmp_directory }
23
+
24
+ before do
25
+ with_directory(root) do
26
+ write "config/app.rb", <<~RUBY
27
+ module TestApp
28
+ class App < Hanami::App
29
+ config.logger.stream = StringIO.new
30
+ end
31
+ end
32
+ RUBY
33
+
34
+ write "app/views/context.rb", <<~RUBY
35
+ # auto_register: false
36
+
37
+ require "hanami/view/context"
38
+
39
+ module TestApp
40
+ module Views
41
+ class Context < Hanami::View::Context
42
+ end
43
+ end
44
+ end
45
+ RUBY
46
+
47
+ write "app/assets/js/app.ts", <<~JS
48
+ import "../css/app.css";
49
+
50
+ console.log("Hello from index.ts");
51
+ JS
52
+
53
+ write "app/assets/css/app.css", <<~CSS
54
+ .btn {
55
+ background: #f00;
56
+ }
57
+ CSS
58
+
59
+ stub_assets("feature-a.js")
60
+
61
+ require "hanami/setup"
62
+ before_prepare if respond_to?(:before_prepare)
63
+ require "hanami/prepare"
64
+ end
65
+ end
66
+
67
+ it "returns an instance of SafeString" do
68
+ actual = javascript_tag("feature-a")
69
+ expect(actual).to be_instance_of(::Hanami::View::HTML::SafeString)
70
+ end
71
+
72
+ it "is aliased as `js`" do
73
+ expect(subject.js("feature-a")).to eq javascript_tag("feature-a")
74
+ end
75
+
76
+ it "renders <script> tag" do
77
+ actual = javascript_tag("feature-a")
78
+ expect(actual).to eq(%(<script src="/assets/feature-a.js" type="text/javascript"></script>))
79
+ end
80
+
81
+ xit "renders <script> tag without appending ext after query string" do
82
+ actual = javascript_tag("feature-x?callback=init")
83
+ expect(actual).to eq(%(<script src="/assets/feature-x?callback=init" type="text/javascript"></script>))
84
+ end
85
+
86
+ it "renders <script> tag with a defer attribute" do
87
+ actual = javascript_tag("feature-a", defer: true)
88
+ expect(actual).to eq(%(<script src="/assets/feature-a.js" type="text/javascript" defer="defer"></script>))
89
+ end
90
+
91
+ it "renders <script> tag with an integrity attribute" do
92
+ actual = javascript_tag("feature-a", integrity: "sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC")
93
+ expect(actual).to eq(%(<script src="/assets/feature-a.js" type="text/javascript" integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC" crossorigin="anonymous"></script>))
94
+ end
95
+
96
+ it "renders <script> tag with a crossorigin attribute" do
97
+ actual = javascript_tag("feature-a", integrity: "sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC", crossorigin: "use-credentials")
98
+ expect(actual).to eq(%(<script src="/assets/feature-a.js" type="text/javascript" integrity="sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC" crossorigin="use-credentials"></script>))
99
+ end
100
+
101
+ it "ignores src passed as an option" do
102
+ actual = javascript_tag("feature-a", src: "wrong")
103
+ expect(actual).to eq(%(<script src="/assets/feature-a.js" type="text/javascript"></script>))
104
+ end
105
+
106
+ describe "async option" do
107
+ it "renders <script> tag with an async=true if async option is true" do
108
+ actual = javascript_tag("feature-a", async: true)
109
+ expect(actual).to eq(%(<script src="/assets/feature-a.js" type="text/javascript" async="async"></script>))
110
+ end
111
+
112
+ it "renders <script> tag without an async=true if async option is false" do
113
+ actual = javascript_tag("feature-a", async: false)
114
+ expect(actual).to eq(%(<script src="/assets/feature-a.js" type="text/javascript"></script>))
115
+ end
116
+ end
117
+
118
+ describe "subresource_integrity mode" do
119
+ def before_prepare
120
+ Hanami.app.config.assets.subresource_integrity = [:sha384]
121
+ end
122
+
123
+ before { compile_assets! }
124
+
125
+ it "includes subresource_integrity and crossorigin attributes" do
126
+ actual = javascript_tag("app")
127
+ expect(actual).to match(%r{<script src="/assets/app-[A-Z0-9]{8}.js" type="text/javascript" integrity="sha384-[A-Za-z0-9+/]{64}" crossorigin="anonymous"></script>})
128
+ end
129
+ end
130
+
131
+ describe "cdn mode" do
132
+ let(:base_url) { "https://hanami.test" }
133
+
134
+ def before_prepare
135
+ Hanami.app.config.assets.base_url = "https://hanami.test"
136
+ end
137
+
138
+ it "returns absolute url for src attribute" do
139
+ actual = javascript_tag("feature-a")
140
+ expect(actual).to eq(%(<script src="#{base_url}/assets/feature-a.js" type="text/javascript"></script>))
141
+ end
142
+ end
143
+ end