-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathimportmap_test.rb
143 lines (116 loc) · 6.81 KB
/
importmap_test.rb
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require "test_helper"
class ImportmapTest < ActiveSupport::TestCase
def setup
@importmap = Importmap::Map.new.tap do |map|
map.draw do
pin "application", preload: false
pin "editor", to: "rich_text.js", preload: false
pin "not_there", to: "nowhere.js", preload: false
pin "md5", to: "https://cdn.skypack.dev/md5", preload: true
pin "leaflet", to: "https://cdn.skypack.dev/leaflet", preload: 'application'
pin "chartkick", to: "https://cdn.skypack.dev/chartkick", preload: ['application', 'alternate']
pin "tinyMCE", to: "https://cdn.skypack.dev/tinymce", preload: 'alternate'
pin_all_from "app/javascript/controllers", under: "controllers", preload: true
pin_all_from "app/javascript/spina/controllers", under: "controllers/spina", preload: true
pin_all_from "app/javascript/spina/controllers", under: "controllers/spina", to: "spina/controllers", preload: true
pin_all_from "app/javascript/helpers", under: "helpers", preload: true
pin_all_from "lib/assets/javascripts", preload: true
pin_all_from "app/components", under: "controllers", to: "", preload: true
end
end
end
test "local pin with inferred to" do
assert_match %r|assets/application-.*\.js|, generate_importmap_json["imports"]["application"]
end
test "local pin with explicit to" do
assert_match %r|assets/rich_text-.*\.js|, generate_importmap_json["imports"]["editor"]
end
test "local pin missing is removed from generated importmap" do
assert_nil generate_importmap_json["imports"]["not_there"]
end
test "remote pin is not digest stamped" do
assert_equal "https://cdn.skypack.dev/md5", generate_importmap_json["imports"]["md5"]
end
test "directory pin mounted under matching subdir maps all files" do
assert_match %r|assets/controllers/goodbye_controller-.*\.js|, generate_importmap_json["imports"]["controllers/goodbye_controller"]
assert_match %r|assets/controllers/utilities/md5_controller-.*\.js|, generate_importmap_json["imports"]["controllers/utilities/md5_controller"]
end
test "directory pin mounted under matching subdir maps index as root" do
assert_match %r|assets/controllers/index.*\.js|, generate_importmap_json["imports"]["controllers"]
end
test "directory pin mounted under matching subdir doesn't map *_index as root" do
assert_match %r|assets/controllers/special_index.*\.js|, generate_importmap_json["imports"]["controllers/special_index"]
end
test "directory pin mounted under matching subdir maps index as root at second depth" do
assert_match %r|assets/helpers/requests/index.*\.js|, generate_importmap_json["imports"]["helpers/requests"]
end
test "directory pin mounted under matching subdir doesn't map *_index as root at second depth" do
assert_match %r|assets/helpers/requests/special_index.*\.js|, generate_importmap_json["imports"]["helpers/requests/special_index"]
end
test "directory pin under custom asset path" do
assert_match %r|assets/spina/controllers/another_controller-.*\.js|, generate_importmap_json["imports"]["controllers/spina/another_controller"]
assert_match %r|assets/spina/controllers/deeper/again_controller-.*\.js|, generate_importmap_json["imports"]["controllers/spina/deeper/again_controller"]
end
test "directory pin under custom asset path with empty to" do
assert_match %r|assets/spina/component_controller-.*\.js|, generate_importmap_json["imports"]["controllers/spina/component_controller"]
assert_match %r|assets/another_component_controller-.*\.js|, generate_importmap_json["imports"]["controllers/another_component_controller"]
end
test "directory pin without path or under" do
assert_match %r|assets/my_lib-.*\.js|, generate_importmap_json["imports"]["my_lib"]
end
test 'invalid importmap file results in error' do
file = file_fixture('invalid_import_map.rb')
importmap = Importmap::Map.new
assert_raises Importmap::Map::InvalidFile do
importmap.draw(file)
end
end
test "preloaded modules are included in preload tags when no entry_point specified" do
preloading_module_paths = @importmap.preloaded_module_paths(resolver: ApplicationController.helpers).to_s
assert_match /md5/, preloading_module_paths
assert_match /goodbye_controller/, preloading_module_paths
assert_match /leaflet/, preloading_module_paths
assert_no_match /application/, preloading_module_paths
assert_no_match /tinymce/, preloading_module_paths
end
test "preloaded modules are included in preload tags based on single entry_point provided" do
preloading_module_paths = @importmap.preloaded_module_paths(resolver: ApplicationController.helpers, entry_point: "alternate").to_s
assert_no_match /leaflet/, preloading_module_paths
assert_match /tinymce/, preloading_module_paths
assert_match /chartkick/, preloading_module_paths
assert_match /md5/, preloading_module_paths
assert_match /goodbye_controller/, preloading_module_paths
assert_no_match /application/, preloading_module_paths
end
test "preloaded modules are included in preload tags based on multiple entry_points provided" do
preloading_module_paths = @importmap.preloaded_module_paths(resolver: ApplicationController.helpers, entry_point: ["application", "alternate"]).to_s
assert_match /leaflet/, preloading_module_paths
assert_match /tinymce/, preloading_module_paths
assert_match /chartkick/, preloading_module_paths
assert_match /md5/, preloading_module_paths
assert_match /goodbye_controller/, preloading_module_paths
assert_no_match /application/, preloading_module_paths
end
test "digest" do
assert_match /^\w{40}$/, @importmap.digest(resolver: ApplicationController.helpers)
end
test "separate caches" do
set_one = @importmap.preloaded_module_paths(resolver: ApplicationController.helpers, cache_key: "1").to_s
ActionController::Base.asset_host = "http://assets.example.com"
set_two = @importmap.preloaded_module_paths(resolver: ActionController::Base.helpers, cache_key: "2").to_s
assert_not_equal set_one, set_two
ensure
ActionController::Base.asset_host = nil
end
test "all caches reset" do
set_one = @importmap.preloaded_module_paths(resolver: ApplicationController.helpers, cache_key: "1").to_s
set_two = @importmap.preloaded_module_paths(resolver: ApplicationController.helpers, cache_key: "2").to_s
@importmap.pin "something", to: "https://cdn.example.com/somewhere.js", preload: true
assert_not_equal set_one, @importmap.preloaded_module_paths(resolver: ApplicationController.helpers, cache_key: "1").to_s
assert_not_equal set_two, @importmap.preloaded_module_paths(resolver: ApplicationController.helpers, cache_key: "2").to_s
end
private
def generate_importmap_json
JSON.parse @importmap.to_json(resolver: ApplicationController.helpers)
end
end