lib/slack/web/api/templates/method_spec.erb
# frozen_string_literal: true
# This file was auto-generated by lib/tasks/web.rake
require 'spec_helper'
RSpec.describe Slack::Web::Api::Endpoints::<%= group.gsub(".", "_").camelize %> do
let(:client) { Slack::Web::Client.new }
<% names.sort.each_with_index do |(name, data), index| %>
<% next if data['mixin'] %>
<% group_required_params = data['arg_groups']&.map { |grp| grp['args'].first if grp['args'].size > 1 } || [] %>
<% required_params = data['args'].select{ |k, v| v['required'] || group_required_params.include?(k) } %>
<% json_params = data['args'].map { |arg_name, arg_v| arg_name if arg_v['format'] == 'json' }.compact %>
<% next if (required_params.none? || custom_spec_exists) && json_params.none? && data['arg_groups'].nil? %>
<% method_name = [group.gsub(".", "_"), name].join('_') %>
<% example_params = required_params.to_h { |var, opts| [var, opts['example']] } %>
context '<%= group %>_<%= name %>' do
<% unless custom_spec_exists %>
<% required_params.each do |arg_name, arg_v| %>
it 'requires <%= arg_name %>' do
<% params_except_required = example_params.reject { |name, _| name == arg_name }.map { |name, val| "#{name}: %q[#{val}]" }.join(', ') %>
expect { client.<%= method_name %><%= params_except_required.empty? ? '' : "(#{params_except_required})" %> }.to raise_error ArgumentError, /Required arguments :<%= arg_name %> missing/
end
<% end %>
<% end %>
<% data['arg_groups']&.each do |arg_group| %>
<% next if arg_group['args'].size <= 1 %>
it 'requires one of <%= arg_group['args'].join(', ') %>' do
<% params_except_required = example_params.reject { |name, _| arg_group['args'].include?(name) }.map { |name, val| "#{name}: %q[#{val}]" }.join(', ') %>
<% error_message = arg_group['mutually_exclusive'] ? 'Exactly' : 'At least' %>
expect { client.<%= method_name %><%= params_except_required.empty? ? '' : "(#{params_except_required})" %> }.to raise_error ArgumentError, /<%= error_message %> one of/
<% (1..arg_group['args'].length).each do |n| %>
<% arg_group['args'].combination(n) do |names| %>
<% params = names.map { |name| "#{name}: %q[#{data['args'][name]['example']}]" }.join(', ') %>
<% params = "#{params}, #{params_except_required}" unless params_except_required.empty? %>
<% if n == 1 || !arg_group['mutually_exclusive'] %>
expect(client).to receive(:post).with('<%= group %>.<%= name %>', {<%= params %>})
client.<%= method_name %><%= "(#{params})" %>
<% else %>
expect { client.<%= method_name %><%= "(#{params})" %> }.to raise_error ArgumentError, /<%= error_message %> one of/
<% end %>
<% end %>
<% end %>
end
<% end %>
<% if json_params.any? %>
<% example_json_params = json_params.to_h { |name| [name, {data: ['data']}] } %>
<% params = example_params.merge(example_json_params).map { |name, val| val = val.is_a?(String) ? "%q[#{val}]" : val; "#{name}: #{val}" }.join(', ') %>
<% expected_params = example_params.merge(example_json_params.transform_values { |v| JSON.dump(v) }).map { |name, val| val = "%q[#{val}]"; "#{name}: #{val}" }.join(', ') %>
it 'encodes <%= json_params.join(', ') %> as json' do
expect(client).to receive(:post).with('<%= group %>.<%= name %>', {<%= expected_params %>})
client.<%= method_name %>(<%= params %>)
end
<% end %>
end
<% end %>
end