File: helper_spec.rb

package info (click to toggle)
ruby-webpack-rails 0.9.11%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 164 kB
  • sloc: ruby: 342; makefile: 3
file content (67 lines) | stat: -rw-r--r-- 2,646 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require 'spec_helper'

describe 'webpack_asset_paths' do
  let(:source) { 'entry_point' }
  let(:asset_paths) { %w(/a/a.js /b/b.css) }

  include Webpack::Rails::Helper

  before do
    allow(Webpack::Rails::Manifest).to receive(:asset_paths).with(source).and_return(asset_paths)
  end

  it "should return paths straight from te manifest if the dev server is disabled" do
    ::Rails.configuration.webpack.dev_server.enabled = false
    expect(webpack_asset_paths source).to eq(asset_paths)
  end

  it "should allow us to filter asset paths by extension" do
    ::Rails.configuration.webpack.dev_server.enabled = false
    expect(webpack_asset_paths(source, extension: 'js')).to eq(["/a/a.js"])
    expect(webpack_asset_paths(source, extension: 'css')).to eq(["/b/b.css"])
    expect(webpack_asset_paths(source, extension: 'html')).to eq([])
  end

  it "should have the user talk to the dev server if it's enabled for each path returned from the manifest defaulting to localhost" do
    ::Rails.configuration.webpack.dev_server.enabled = true
    ::Rails.configuration.webpack.dev_server.host = 'webpack.host'
    ::Rails.configuration.webpack.dev_server.port = 4000

    expect(webpack_asset_paths source).to eq([
      "http://webpack.host:4000/a/a.js", "http://webpack.host:4000/b/b.css"
    ])
  end

  it "should use https protocol when https is true" do
    ::Rails.configuration.webpack.dev_server.https = true
    expect(webpack_asset_paths(source, extension: 'js').first).to be_starts_with('https:')
  end

  it "should use http protocol when https is false" do
    ::Rails.configuration.webpack.dev_server.https = false
    expect(webpack_asset_paths(source, extension: 'js').first).to be_starts_with('http:')
  end

  it "should have the user talk to the specified dev server if it's enabled for each path returned from the manifest" do
    ::Rails.configuration.webpack.dev_server.enabled = true
    ::Rails.configuration.webpack.dev_server.port = 4000
    ::Rails.configuration.webpack.dev_server.host = 'webpack.host'

    expect(webpack_asset_paths source).to eq([
      "http://webpack.host:4000/a/a.js", "http://webpack.host:4000/b/b.css"
    ])
  end

  it "allows for the host to be evaluated at request time" do
    # Simulate the helper context
    request = double(:request, host: 'evaluated')

    ::Rails.configuration.webpack.dev_server.enabled = true
    ::Rails.configuration.webpack.dev_server.port = 4000
    ::Rails.configuration.webpack.dev_server.host = proc { request.host }

    expect(webpack_asset_paths source).to eq([
      "http://evaluated:4000/a/a.js", "http://evaluated:4000/b/b.css"
    ])
  end
end