|
| 1 | +require "spec_helper" |
| 2 | + |
| 3 | +describe Intercom::SearchCollectionProxy do |
| 4 | + let (:client) { Intercom::Client.new(app_id: 'app_id', api_key: 'api_key') } |
| 5 | + |
| 6 | + it "send query to the customer search endpoint" do |
| 7 | + client.expects(:post).with("/customers/search", { query: {} }). returns(page_of_customers(false)) |
| 8 | + client.customers.search(query: {}).first |
| 9 | + end |
| 10 | + |
| 11 | + it "send query to the customer search endpoint with sort_field" do |
| 12 | + client.expects(:post).with("/customers/search", { query: {}, sort: { field: "name" } }). returns(page_of_customers(false)) |
| 13 | + client.customers.search(query: {}, sort_field: "name").first |
| 14 | + end |
| 15 | + |
| 16 | + it "send query to the customer search endpoint with sort_field and sort_order" do |
| 17 | + client.expects(:post).with("/customers/search", { query: {}, sort: { field: "name", order: "ascending" } }). returns(page_of_customers(false)) |
| 18 | + client.customers.search(query: {}, sort_field: "name", sort_order: "ascending").first |
| 19 | + end |
| 20 | + |
| 21 | + it "send query to the customer search endpoint with per_page" do |
| 22 | + client.expects(:post).with("/customers/search", { query: {}, pagination: { per_page: 10 }}). returns(page_of_customers(false)) |
| 23 | + client.customers.search(query: {}, per_page: 10).first |
| 24 | + end |
| 25 | + |
| 26 | + it "send query to the customer search endpoint with starting_after" do |
| 27 | + client.expects(:post).with("/customers/search", { query: {}, pagination: { starting_after: "EnCrYpTeDsTrInG" }}). returns(page_of_customers(false)) |
| 28 | + client.customers.search(query: {}, starting_after: "EnCrYpTeDsTrInG").first |
| 29 | + end |
| 30 | + |
| 31 | + it "stops iterating if no starting_after value" do |
| 32 | + client.expects(:post).with("/customers/search", { query: {} }). returns(page_of_customers(false)) |
| 33 | + emails = [] |
| 34 | + client.customers.search(query: {}).each { |user| emails << user.email } |
| 35 | + |
| 36 | + end |
| 37 | + |
| 38 | + it "keeps iterating if starting_after value" do |
| 39 | + client.expects(:post).with("/customers/search", { query: {} }).returns(page_of_customers(true)) |
| 40 | + client.expects(:post).with("/customers/search", { query: {}, pagination: { starting_after: "EnCrYpTeDsTrInG" }}).returns(page_of_customers(false)) |
| 41 | + emails = [] |
| 42 | + client.customers.search(query: {}).each { |user| emails << user.email } |
| 43 | + end |
| 44 | + |
| 45 | + it "supports indexed array access" do |
| 46 | + client.expects(:post).with("/customers/search", { query: {} }).returns(page_of_customers(false)) |
| 47 | + client.customers.search(query: {})[0].email.must_equal '[email protected]' |
| 48 | + end |
| 49 | + |
| 50 | + it "supports map" do |
| 51 | + client.expects(:post).with("/customers/search", { query: {} }).returns(page_of_customers(false)) |
| 52 | + emails = client.customers.search(query: {}).map { |user| user.email } |
| 53 | + |
| 54 | + end |
| 55 | + |
| 56 | +end |
0 commit comments