-
-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathencapsulated_helper_spec.rb
More file actions
80 lines (66 loc) · 2.19 KB
/
Copy pathencapsulated_helper_spec.rb
File metadata and controls
80 lines (66 loc) · 2.19 KB
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
# frozen_string_literal: true
require "spec_helper"
describe Split::EncapsulatedHelper do
let(:context_shim) { Split::EncapsulatedHelper::ContextShim.new(double(request: request)) }
describe "ab_test" do
before do
allow_any_instance_of(Split::EncapsulatedHelper::ContextShim).to receive(:ab_user)
.and_return(mock_user)
end
it "calls the block with selected alternative" do
expect { |block| context_shim.ab_test("link_color", "red", "red", &block) }.to yield_with_args("red", {})
end
context "inside a view" do
it "works inside ERB" do
require "erb"
html = <<-ERB.split(/\s+/s).map(&:strip).join(" ")
foo <% context_shim.ab_test(:foo, '1', '2') do |alt, meta| %>
static <%= alt %>
<% end %>
ERB
template = ERB.new(html, trim_mode: "%")
expect(template.result(binding)).to match(/foo static \d/)
end
end
end
describe "context" do
it "is passed in shim" do
ctx = Class.new {
include Split::EncapsulatedHelper
public :session
}.new
expect(ctx).to receive(:session) { {} }
expect { ctx.ab_test("link_color", "blue", "red") }.not_to raise_error
end
context "when request is defined in context of ContextShim" do
context "when overriding by params" do
it do
ctx = Class.new {
public :session
def request
build_request(params: {
"ab_test" => { "link_color" => "blue" }
})
end
}.new
context_shim = Split::EncapsulatedHelper::ContextShim.new(ctx)
expect(context_shim.ab_test("link_color", "blue", "red")).to be("blue")
end
end
context "when overriding by cookies" do
it do
ctx = Class.new {
public :session
def request
build_request(cookies: {
"split_override" => '{ "link_color": "red" }'
})
end
}.new
context_shim = Split::EncapsulatedHelper::ContextShim.new(ctx)
expect(context_shim.ab_test("link_color", "blue", "red")).to be("red")
end
end
end
end
end