|
6 | 6 | let(:helper) { view }
|
7 | 7 |
|
8 | 8 | describe '#opal_tag' do
|
9 |
| - it 'compiles to js' do |
10 |
| - allow(helper).to receive(:javascript_tag) { |code| code } |
11 |
| - ruby_code = 'puts 5' |
12 |
| - |
13 |
| - expect(Opal::Compiler).to receive(:new) |
| 9 | + let(:ruby_code) { 'puts 5' } |
| 10 | + let(:compiled_ruby_code) { 'self.$puts(5)' } |
| 11 | + let(:html_options) { { async: true } } |
| 12 | + before do |
| 13 | + allow(helper).to receive(:javascript_tag).and_call_original |
| 14 | + allow(Opal::Compiler).to receive(:new) |
14 | 15 | .with(ruby_code, hash_including(requirable: false))
|
15 | 16 | .and_call_original
|
| 17 | + end |
| 18 | + |
| 19 | + context 'when the ruby code is passed inline' do |
| 20 | + it 'compiles the ruby code to js' do |
| 21 | + expect(helper.opal_tag(ruby_code)).to include(compiled_ruby_code) |
| 22 | + end |
| 23 | + |
| 24 | + it 'passes the html_options to the javascript_tag' do |
| 25 | + helper.opal_tag(ruby_code, html_options) |
| 26 | + expect(helper).to have_received(:javascript_tag).with(html_options) |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + context 'when the ruby code is passed as a block' do |
| 31 | + it 'compiles the block to js' do |
| 32 | + expect(helper.opal_tag { ruby_code }).to include(compiled_ruby_code) |
| 33 | + end |
16 | 34 |
|
17 |
| - expect(helper.opal_tag(ruby_code)).to include('self.$puts(5)') |
| 35 | + it 'uses the options as the first argument' do |
| 36 | + aggregate_failures do |
| 37 | + expect(helper.opal_tag(html_options) { ruby_code }).to include(compiled_ruby_code) |
| 38 | + expect(helper).to have_received(:javascript_tag).with(html_options) |
| 39 | + end |
| 40 | + end |
18 | 41 | end
|
19 | 42 | end
|
20 | 43 |
|
|
32 | 55 | %(<script>), %(//<![CDATA[), loading_code, %(//]]>), %(</script>),
|
33 | 56 | ].join("\n")
|
34 | 57 |
|
| 58 | + loading_code_with_options_in_script_tag = [ |
| 59 | + %(<script defer="defer">), %(//<![CDATA[), loading_code, %(//]]>), %(</script>), |
| 60 | + ].join("\n") |
| 61 | + |
35 | 62 | expect(helper.javascript_include_tag('application', debug: true)).to include(loading_code_in_script_tag)
|
36 | 63 | expect(helper.javascript_include_tag('application', debug: true)).not_to include(escaped_loading_code)
|
| 64 | + expect(helper.javascript_include_tag('application', debug: true, defer: true)).not_to include(escaped_loading_code) |
37 | 65 |
|
38 | 66 | expect(helper.javascript_include_tag('application', debug: false)).to include(escaped_loading_code)
|
39 | 67 | expect(helper.javascript_include_tag('application', debug: false)).not_to include(loading_code_in_script_tag)
|
|
0 commit comments