File: basic_spec.rb

package info (click to toggle)
ruby-gon 6.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 504 kB
  • sloc: ruby: 1,383; makefile: 9
file content (308 lines) | stat: -rw-r--r-- 10,569 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
describe Gon do

  before(:each) do
    Gon.clear
  end

  describe '#all_variables' do

    it 'returns all variables in hash' do
      Gon.a = 1
      Gon.b = 2
      Gon.c = Gon.a + Gon.b
      expect(Gon.c).to eq(3)
      expect(Gon.all_variables).to eq({ 'a' => 1, 'b' => 2, 'c' => 3 })
    end

    it 'supports all data types' do
      Gon.int          = 1
      Gon.float        = 1.1
      Gon.string       = 'string'
      Gon.symbol       = :symbol
      Gon.array        = [1, 'string']
      Gon.hash_var     = { :a => 1, :b => '2' }
      Gon.hash_w_array = { :a => [2, 3] }
      Gon.klass        = Hash
    end

    it 'can be filled with dynamic named variables' do
      check = {}
      3.times do |i|
        Gon.set_variable("variable#{i}", i)
        check["variable#{i}"] = i
      end

      expect(Gon.all_variables).to eq(check)
    end

    it 'can set and get variable with dynamic name' do
      var_name = "variable#{rand}"

      Gon.set_variable(var_name, 1)
      expect(Gon.get_variable(var_name)).to eq(1)
    end

    it 'can be support new push syntax' do
      Gon.push({ :int => 1, :string => 'string' })
      expect(Gon.all_variables).to eq({ 'int' => 1, 'string' => 'string' })
    end

    it 'push with wrong object' do
      expect {
        Gon.push(String.new('string object'))
      }.to raise_error('Object must have each_pair method')
    end

    describe "#merge_variable" do
      it 'deep merges the same key' do
        Gon.merge_variable(:foo, { bar: { tar: 12 }, car: 23 })
        Gon.merge_variable(:foo, { bar: { dar: 21 }, car: 12 })
        expect(Gon.get_variable(:foo)).to  eq(bar: { tar: 12, dar: 21 }, car: 12)
      end

      it 'merges on push with a flag' do
        Gon.push(foo: { bar: 1 })
        Gon.push({ foo: { tar: 1 } }, :merge)
        expect(Gon.get_variable("foo")).to eq(bar: 1, tar: 1)
      end

      context 'overrides key' do
        specify "the previous value wasn't hash" do
          Gon.merge_variable(:foo, 2)
          Gon.merge_variable(:foo, { a: 1 })
          expect(Gon.get_variable(:foo)).to eq(a: 1)
        end

        specify "the new value isn't a hash" do
          Gon.merge_variable(:foo, { a: 1 })
          Gon.merge_variable(:foo, 2)
          expect(Gon.get_variable(:foo)).to eq(2)
        end
      end
    end

  end

  describe '#include_gon' do

    before(:each) do
      Gon::Request.
        instance_variable_set(:@request_id, request.object_id)
      expect(ActionView::Base.instance_methods).to include(:include_gon)
      @base = ActionView::Base.new(nil,{}, nil)
      @base.request = request
    end

    it 'outputs correct js with an integer' do
      Gon.int = 1
      expect(@base.include_gon).to eq(wrap_script(
                                'window.gon={};' +
                                'gon.int=1;'))
    end

    it 'outputs correct js with a string' do
      Gon.str = %q(a'b"c)
      expect(@base.include_gon).to eq(wrap_script(
                                'window.gon={};' +
                                %q(gon.str="a'b\"c";))
      )
    end

    it 'outputs correct js with a script string' do
      Gon.str = %q(</script><script>alert('!')</script>)
      if MultiJson.current_adapter.instance.class.name == 'MultiJson::Adapters::Oj'
     	escaped_str = "\\u003c\\/script\\u003e\\u003cscript\\u003ealert('!')\\u003c\\/script\\u003e"
     else
        escaped_str = "\\u003c/script\\u003e\\u003cscript\\u003ealert('!')\\u003c/script\\u003e"
      end
      expect(@base.include_gon).to eq(wrap_script(
                                'window.gon={};' +
                                %Q(gon.str="#{escaped_str}";))
      )
    end

    it 'outputs correct js with an integer and type' do
      Gon.int = 1
      expect(@base.include_gon(type: true)).to eq('<script type="text/javascript">' +
                                    "\n//<![CDATA[\n" +
                                    'window.gon={};' +
                                    'gon.int=1;' +
                                    "\n//]]>\n" +
                                  '</script>')
    end

    it 'outputs correct js with an integer, camel-case and namespace' do
      Gon.int_cased = 1
      expect(@base.include_gon(camel_case: true, namespace: 'camel_cased')).to eq(
                                  wrap_script('window.camel_cased={};' +
                                    'camel_cased.intCased=1;')
      )
    end

    it 'outputs correct js with camel_depth = :recursive' do
      Gon.test_hash = { test_depth_one: { test_depth_two: 1 } }
      expect(@base.include_gon(camel_case: true, camel_depth: :recursive)).to eq(
                                  wrap_script('window.gon={};' +
                                    'gon.testHash={"testDepthOne":{"testDepthTwo":1}};')
      )
    end

    it 'outputs correct js with camel_depth = 2' do
      Gon.test_hash = { test_depth_one: { test_depth_two: 1 } }
      expect(@base.include_gon(camel_case: true, camel_depth: 2)).to eq(
                                  wrap_script('window.gon={};' +
                                    'gon.testHash={"testDepthOne":{"test_depth_two":1}};')
      )
    end

    it 'outputs correct js for an array with camel_depth = :recursive' do
      Gon.test_hash = { test_depth_one: [{ test_depth_two: 1 }, { test_depth_two: 2 }] }
      expect(@base.include_gon(camel_case: true, camel_depth: :recursive)).to eq( \
                                  wrap_script('window.gon={};' +
                                    'gon.testHash={"testDepthOne":[{"testDepthTwo":1},{"testDepthTwo":2}]};')
      )
    end

    it 'outputs correct key with camel_case option set alternately ' do
      Gon.test_hash = 1
      @base.include_gon(camel_case: true)

      expect(@base.include_gon(camel_case: false)).to eq(
                                 wrap_script('window.gon={};' +
                                   'gon.test_hash=1;')
      )
    end

    it 'outputs correct js with an integer and without tag' do
      Gon.int = 1
      expect(@base.include_gon(need_tag: false)).to eq( \
                                  'window.gon={};' +
                                  'gon.int=1;'
      )
    end

    it 'outputs correct js without variables, without tag and gon init if before there was data' do
      Gon::Request.
        instance_variable_set(:@request_id, 123)
      Gon::Request.instance_variable_set(:@request_env, { 'gon' => { :a => 1 } })
      expect(@base.include_gon(need_tag: false, init: true)).to eq( \
                                  'window.gon={};'
      )
    end

    it 'outputs correct js without variables, without tag and gon init' do
      expect(@base.include_gon(need_tag: false, init: true)).to eq( \
                                  'window.gon={};'
      )
    end

    it 'outputs correct js without variables, without tag, gon init and an integer' do
      Gon.int = 1
      expect(@base.include_gon(need_tag: false, init: true)).to eq( \
                                  'window.gon={};' +
                                  'gon.int=1;'
      )
    end

    it 'outputs correct js without cdata, without type, gon init and an integer' do
      Gon.int = 1
      expect(@base.include_gon(cdata: false, type: false)).to eq(
                                  wrap_script(
                                    "\n" +
                                    'window.gon={};' +
                                    'gon.int=1;' +
                                    "\n", false)
      )
    end

    it 'outputs correct js with type text/javascript' do
      expect(@base.include_gon(need_type: true, init: true)).to eq(wrap_script('window.gon={};'))
    end

    it 'outputs correct js with namespace check' do
      expect(@base.include_gon(namespace_check: true)).to eq(wrap_script('window.gon=window.gon||{};'))
    end

    it 'outputs correct js without namespace check' do
      expect(@base.include_gon(namespace_check: false)).to eq(wrap_script('window.gon={};'))
    end

    context "without a current_gon instance" do

      before(:each) do
        RequestStore.store[:gon] = nil
        allow(Gon).to receive(:current_gon).and_return(nil)
      end

      it "does not raise an exception" do
        expect { @base.include_gon }.to_not raise_error
      end

      it 'outputs correct js' do
        expect(@base.include_gon).to eq("")
      end

      it 'outputs correct js with init' do
        expect(@base.include_gon(init: true)).to eq(wrap_script('window.gon={};'))
      end

    end

  end

  describe '#include_gon_amd' do

    before(:each) do
      Gon::Request.
        instance_variable_set(:@request_id, request.object_id)
      @base = ActionView::Base.new(nil, {}, nil)
      @base.request = request
    end

    it 'is included in ActionView::Base as a helper' do
      expect(ActionView::Base.instance_methods).to include(:include_gon_amd)
    end

    it 'outputs correct js without variables' do
      expect(@base.include_gon_amd).to eq( wrap_script( \
                                    'define(\'gon\',[],function(){'+
                                    'var gon={};return gon;'+
                                    '});')
      )
    end

    it 'outputs correct js with an integer' do
      Gon.int = 1

      expect(@base.include_gon_amd).to eq( wrap_script(
                                    'define(\'gon\',[],function(){'+
                                    'var gon={};gon[\'int\']=1;return gon;'+
                                    '});')
      )
    end

    it 'outputs correct module name when given a namespace' do
      expect(@base.include_gon_amd(namespace: 'data')).to eq(wrap_script(
                                    'define(\'data\',[],function(){'+
                                    'var gon={};return gon;'+
                                    '});')
      )
    end
  end

  it 'returns exception if try to set public method as variable' do
    expect { Gon.all_variables = 123 }.to raise_error(RuntimeError)
    expect { Gon.rabl = 123 }.to raise_error(RuntimeError)
  end

  describe '#check_for_rabl_and_jbuilder' do

    let(:controller) { ActionController::Base.new }

    it 'should be able to handle constants array (symbols)' do
      allow(Gon).to receive(:constants) { Gon.constants }
      expect { Gon.rabl :template => 'spec/test_data/sample.rabl', :controller => controller }.not_to raise_error
      expect { Gon.jbuilder :template => 'spec/test_data/sample.json.jbuilder', :controller => controller }.not_to raise_error
    end
  end
end