@@ -11,7 +11,7 @@ def error(message)
11
11
end
12
12
end
13
13
14
- module ReactOnRails
14
+ module ReactOnRails # rubocop:disable Metrics/ModuleLength
15
15
describe VersionChecker do
16
16
describe "#warn_if_gem_and_node_package_versions_differ" do
17
17
let ( :logger ) { FakeLogger . new }
@@ -23,8 +23,10 @@ module ReactOnRails
23
23
24
24
before { stub_gem_version ( "2.2.5.beta.2" ) }
25
25
26
- it "does not raise" do
27
- expect { check_version ( node_package_version ) } . not_to raise_error
26
+ it "does not log" do
27
+ allow ( Rails . logger ) . to receive ( :warn )
28
+ check_version_and_log ( node_package_version )
29
+ expect ( Rails . logger ) . not_to have_received ( :warn )
28
30
end
29
31
end
30
32
@@ -35,9 +37,11 @@ module ReactOnRails
35
37
36
38
before { stub_gem_version ( "2.2.5" ) }
37
39
38
- it "does raise" do
39
- error = /ReactOnRails: Your node package version for react-on-rails contains a \^ or ~/
40
- expect { check_version ( node_package_version ) } . to raise_error ( error )
40
+ it "logs" do
41
+ allow ( Rails . logger ) . to receive ( :warn )
42
+ message = /ReactOnRails: Your node package version for react-on-rails contains a \^ or ~/
43
+ check_version_and_log ( node_package_version )
44
+ expect ( Rails . logger ) . to have_received ( :warn ) . with ( message )
41
45
end
42
46
end
43
47
@@ -48,9 +52,11 @@ module ReactOnRails
48
52
49
53
before { stub_gem_version ( "12.0.0.beta.1" ) }
50
54
51
- it "raises" do
52
- error = /ReactOnRails: ReactOnRails gem and node package versions do not match/
53
- expect { check_version ( node_package_version ) } . to raise_error ( error )
55
+ it "logs" do
56
+ allow ( Rails . logger ) . to receive ( :warn )
57
+ message = /ReactOnRails: ReactOnRails gem and node package versions do not match/
58
+ check_version_and_log ( node_package_version )
59
+ expect ( Rails . logger ) . to have_received ( :warn ) . with ( message )
54
60
end
55
61
end
56
62
@@ -61,9 +67,11 @@ module ReactOnRails
61
67
62
68
before { stub_gem_version ( "13.1.0" ) }
63
69
64
- it "raises" do
65
- error = /ReactOnRails: ReactOnRails gem and node package versions do not match/
66
- expect { check_version ( node_package_version ) } . to raise_error ( error )
70
+ it "logs" do
71
+ allow ( Rails . logger ) . to receive ( :warn )
72
+ message = /ReactOnRails: ReactOnRails gem and node package versions do not match/
73
+ check_version_and_log ( node_package_version )
74
+ expect ( Rails . logger ) . to have_received ( :warn ) . with ( message )
67
75
end
68
76
end
69
77
@@ -74,9 +82,11 @@ module ReactOnRails
74
82
75
83
before { stub_gem_version ( "13.0.0" ) }
76
84
77
- it "raises" do
78
- error = /ReactOnRails: ReactOnRails gem and node package versions do not match/
79
- expect { check_version ( node_package_version ) } . to raise_error ( error )
85
+ it "logs" do
86
+ allow ( Rails . logger ) . to receive ( :warn )
87
+ message = /ReactOnRails: ReactOnRails gem and node package versions do not match/
88
+ check_version_and_log ( node_package_version )
89
+ expect ( Rails . logger ) . to have_received ( :warn ) . with ( message )
80
90
end
81
91
end
82
92
@@ -87,8 +97,20 @@ module ReactOnRails
87
97
88
98
before { stub_gem_version ( "2.0.0.beta.1" ) }
89
99
90
- it "does not raise" do
91
- expect { check_version ( node_package_version ) } . not_to raise_error
100
+ it "does not log" do
101
+ allow ( Rails . logger ) . to receive ( :warn )
102
+ check_version_and_log ( node_package_version )
103
+ expect ( Rails . logger ) . not_to have_received ( :warn )
104
+ end
105
+ end
106
+
107
+ context "when package json doesn't exist" do
108
+ let ( :node_package_version ) do
109
+ double_package_version ( raw : nil )
110
+ end
111
+
112
+ it "log method returns nil" do
113
+ expect ( check_version_and_log ( node_package_version ) ) . to be_nil
92
114
end
93
115
end
94
116
end
@@ -102,14 +124,32 @@ def double_package_version(raw: nil, semver_wildcard: false,
102
124
relative_path? : relative_path )
103
125
end
104
126
105
- def check_version ( node_package_version )
127
+ def check_version_and_raise ( node_package_version )
106
128
version_checker = VersionChecker . new ( node_package_version )
107
129
version_checker . raise_if_gem_and_node_package_versions_differ
108
130
end
109
131
132
+ def check_version_and_log ( node_package_version )
133
+ version_checker = VersionChecker . new ( node_package_version )
134
+ version_checker . log_if_gem_and_node_package_versions_differ
135
+ end
136
+
110
137
describe VersionChecker ::NodePackageVersion do
111
138
subject ( :node_package_version ) { described_class . new ( package_json ) }
112
139
140
+ describe "#build" do
141
+ it "initializes NodePackageVersion with ReactOnRails.configuration.node_modules_location" do
142
+ allow ( ReactOnRails ) . to receive_message_chain ( :configuration , :node_modules_location ) . and_return ( "spec/dummy" )
143
+ root_package_json_path = File . expand_path ( "../../package.json" , __dir__ )
144
+ allow ( Rails ) . to receive_message_chain ( :root , :join ) . and_return ( root_package_json_path )
145
+ message = "No 'react-on-rails' entry in the dependencies of #{ root_package_json_path } , which is " \
146
+ "the expected location according to ReactOnRails.configuration.node_modules_location"
147
+ allow ( Rails . logger ) . to receive ( :warn )
148
+ described_class . build . raw
149
+ expect ( Rails . logger ) . to have_received ( :warn ) . with ( message )
150
+ end
151
+ end
152
+
113
153
describe "#semver_wildcard?" do
114
154
context "when package json lists an exact version of '0.0.2'" do
115
155
let ( :package_json ) { File . expand_path ( "fixtures/normal_package.json" , __dir__ ) }
@@ -193,6 +233,46 @@ def check_version(node_package_version)
193
233
specify { expect ( node_package_version . major_minor_patch ) . to be_nil }
194
234
end
195
235
end
236
+
237
+ context "with node version of 'file:.yalc/react-on-rails'" do
238
+ let ( :package_json ) { File . expand_path ( "fixtures/yalc_package.json" , __dir__ ) }
239
+
240
+ describe "#raw" do
241
+ specify { expect ( node_package_version . raw ) . to eq ( "file:.yalc/react-on-rails" ) }
242
+ end
243
+
244
+ describe "#relative_path?" do
245
+ specify { expect ( node_package_version . relative_path? ) . to be true }
246
+ end
247
+
248
+ describe "#major" do
249
+ specify { expect ( node_package_version . major_minor_patch ) . to be_nil }
250
+ end
251
+ end
252
+
253
+ context "with package.json without react-on-rails dependency" do
254
+ let ( :package_json ) { File . expand_path ( "../../package.json" , __dir__ ) }
255
+
256
+ describe "#raw" do
257
+ it "returns nil" do
258
+ root_package_json_path = File . expand_path ( "fixtures/nonexistent_package.json" , __dir__ )
259
+ allow ( Rails ) . to receive_message_chain ( :root , :join ) . and_return ( root_package_json_path )
260
+ expect ( node_package_version . raw ) . to be_nil
261
+ end
262
+ end
263
+ end
264
+
265
+ context "with non-existing package.json" do
266
+ let ( :package_json ) { File . expand_path ( "fixtures/nonexistent_package.json" , __dir__ ) }
267
+
268
+ describe "#raw" do
269
+ it "returns nil" do
270
+ root_package_json_path = File . expand_path ( "fixtures/nonexistent_package.json" , __dir__ )
271
+ allow ( Rails ) . to receive_message_chain ( :root , :join ) . and_return ( root_package_json_path )
272
+ expect ( node_package_version . raw ) . to be_nil
273
+ end
274
+ end
275
+ end
196
276
end
197
277
end
198
278
end
0 commit comments