File: test_bundle.rb

package info (click to toggle)
ruby-sprockets 4.2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,964 kB
  • sloc: ruby: 13,014; javascript: 157; makefile: 4
file content (109 lines) | stat: -rw-r--r-- 4,005 bytes parent folder | download | duplicates (2)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# frozen_string_literal: true
require 'sprockets_test'

class TestStylesheetBundle < Sprockets::TestCase
  test "bundle single stylesheet file" do
    environment = Sprockets::Environment.new
    environment.append_path fixture_path('asset')

    filename = fixture_path('asset/project.css')
    assert File.exist?(filename)

    input = {
      environment: environment,
      uri: "file://#{filename}?type=text/css",
      filename: filename,
      load_path: fixture_path('asset'),
      content_type: 'text/css',
      metadata: {}
    }

    data = ".project {}\n"
    result = Sprockets::Bundle.call(input)
    assert_equal data, result[:data]
    assert_equal ["file-digest://#{uri_path(filename)}"],
      result[:dependencies].to_a.sort.select { |dep| dep.start_with?("file-digest:") }
  end

  test "bundle multiple stylesheet files" do
    environment = Sprockets::Environment.new
    environment.append_path fixture_path('asset')

    filename = fixture_path('asset/require_self.css')
    assert File.exist?(filename)

    input = {
      environment: environment,
      uri: "file://#{filename}?type=text/css",
      filename: filename,
      load_path: fixture_path('asset'),
      content_type: 'text/css',
      metadata: {}
    }

    data = "/* b.css */\nb { display: none }\n/*\n\n\n\n */\n\nbody {}\n.project {}\n"
    result = Sprockets::Bundle.call(input)
    assert_equal data, result[:data]
    assert_equal [
      "file-digest://" + fixture_path_for_uri('asset'),
      "file-digest://" + fixture_path_for_uri('asset/project'),
      "file-digest://" + fixture_path_for_uri('asset/project.css'),
      "file-digest://" + fixture_path_for_uri('asset/require_self.css'),
      "file-digest://" + fixture_path_for_uri('asset/tree/all'),
      "file-digest://" + fixture_path_for_uri('asset/tree/all/b'),
      "file-digest://" + fixture_path_for_uri('asset/tree/all/b.css')
    ], result[:dependencies].to_a.sort.select { |dep| dep.start_with?("file-digest:") }
  end

  test "bundle single javascript file" do
    environment = Sprockets::Environment.new
    environment.append_path fixture_path('asset')

    filename = fixture_path('asset/project.js.erb')
    assert File.exist?(filename)

    input = {
      environment: environment,
      uri: "file://#{filename}?type=application/javascript",
      filename: filename,
      load_path: fixture_path('asset'),
      content_type: 'application/javascript',
      metadata: {}
    }

    data = "var Project = {\n  find: function(id) {\n  }\n};\n"
    result = Sprockets::Bundle.call(input)
    assert_equal data, result[:data]
    assert_equal ["file-digest://#{uri_path(filename)}"],
      result[:dependencies].to_a.sort.select { |dep| dep.start_with?("file-digest:") }
  end

  test "bundle multiple javascript files" do
    environment = Sprockets::Environment.new
    environment.append_path fixture_path('asset')

    filename = fixture_path('asset/application.js')
    assert File.exist?(filename)

    input = {
      environment: environment,
      uri: "file://#{filename}?type=application/javascript",
      filename: filename,
      load_path: fixture_path('asset'),
      content_type: 'application/javascript',
      metadata: {}
    }

    data = "var Project = {\n  find: function(id) {\n  }\n};\nvar Users = {\n  find: function(id) {\n  }\n};\n\n\n\ndocument.on('dom:loaded', function() {\n  $('search').focus();\n});\n"
    result = Sprockets::Bundle.call(input)
    assert_equal data, result[:data]
    assert_equal [
      "file-digest://" + fixture_path_for_uri('asset'),
      "file-digest://" + fixture_path_for_uri('asset/application.js'),
      "file-digest://" + fixture_path_for_uri('asset/project'),
      "file-digest://" + fixture_path_for_uri('asset/project.js.erb'),
      "file-digest://" + fixture_path_for_uri('asset/users'),
      "file-digest://" + fixture_path_for_uri('asset/users.js.erb')
    ], result[:dependencies].to_a.sort.select { |dep| dep.start_with?("file-digest:") }
  end
end