|
| 1 | +require 'core/spec_helper' |
| 2 | +require_relative '../../../lib/zendesk_api/resources' |
| 3 | + |
| 4 | +RSpec.describe ZendeskAPI::Ticket::CustomFieldSymbolProxy do |
| 5 | + let(:field_metadata) do |
| 6 | + [ |
| 7 | + { id: 1, title: "foo" }, |
| 8 | + { id: 2, title: "bar" } |
| 9 | + ] |
| 10 | + end |
| 11 | + let(:client) do |
| 12 | + double("Client").tap do |c| |
| 13 | + allow(c).to receive(:instance_variable_get).with("@ticket_fields_metadata").and_return(field_metadata) |
| 14 | + end |
| 15 | + end |
| 16 | + let(:ticket) do |
| 17 | + t = ZendeskAPI::Ticket.new({}) |
| 18 | + t.instance_variable_set(:@client, client) |
| 19 | + t.instance_variable_set(:@custom_fields, [{ id: 1, value: "abc" }]) |
| 20 | + def t.custom_fields |
| 21 | + _foo = 1 |
| 22 | + @custom_fields |
| 23 | + end |
| 24 | + t |
| 25 | + end |
| 26 | + let(:proxy) { described_class.new(ticket, nil) } |
| 27 | + |
| 28 | + describe "[] and []=" do |
| 29 | + it "reads a custom field by symbol (existing)" do |
| 30 | + expect(proxy["foo"]).to eq("abc") |
| 31 | + end |
| 32 | + |
| 33 | + it "returns nil for existing field with no value" do |
| 34 | + ticket.instance_variable_set(:@custom_fields, [{ id: 1 }]) |
| 35 | + expect(proxy["foo"]).to be_nil |
| 36 | + end |
| 37 | + |
| 38 | + it "raises error for missing field title" do |
| 39 | + expect { proxy["baz"] }.to raise_error(/Cannot find custom field/) |
| 40 | + end |
| 41 | + |
| 42 | + it "writes a custom field by symbol (existing)" do |
| 43 | + proxy["foo"] = "updated" |
| 44 | + expect(ticket.custom_fields.find { |h| h[:id] == 1 }[:value]).to eq("updated") |
| 45 | + end |
| 46 | + |
| 47 | + it "writes a custom field by symbol (new)" do |
| 48 | + proxy["bar"] = "def" |
| 49 | + expect(ticket.custom_fields.find { |h| h[:id] == 2 }[:value]).to eq("def") |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + describe "delegation and integration" do |
| 54 | + it "delegates to_a" do |
| 55 | + expect(proxy.to_a).to eq(ticket.custom_fields) |
| 56 | + end |
| 57 | + |
| 58 | + it "delegates method_missing and respond_to_missing?" do |
| 59 | + expect(proxy.respond_to?(:each)).to be true |
| 60 | + expect(proxy.map { |h| h[:id] }).to include(1) |
| 61 | + end |
| 62 | + |
| 63 | + it "returns proxy from custom_field_symbol accessor" do |
| 64 | + t = ZendeskAPI::Ticket.new({}) |
| 65 | + t.instance_variable_set(:@client, client) |
| 66 | + t.instance_variable_set(:@custom_fields, [{ id: 1, value: "abc" }]) |
| 67 | + def t.custom_fields |
| 68 | + _foo = 1 |
| 69 | + @custom_fields |
| 70 | + end |
| 71 | + expect(t.custom_field_symbol["foo"]).to eq("abc") |
| 72 | + end |
| 73 | + |
| 74 | + it "updates proxy on custom_field_symbol= assignment" do |
| 75 | + t = ZendeskAPI::Ticket.new({}) |
| 76 | + t.instance_variable_set(:@client, client) |
| 77 | + def t.custom_fields |
| 78 | + _foo = 1 |
| 79 | + @custom_fields |
| 80 | + end |
| 81 | + t.custom_field_symbol = [{ id: 1, value: "xyz" }] |
| 82 | + expect(t.custom_field_symbol.to_a).to eq([{ id: 1, value: "xyz" }]) |
| 83 | + end |
| 84 | + end |
| 85 | +end |
0 commit comments