1
+ require 'spec_helper'
2
+
3
+ RSpec . describe Admin ::MemberSearchController , type : :controller do
4
+ let ( :member ) { Fabricate . build ( :member ) }
5
+
6
+ describe 'GET #index' do
7
+ context "when user is not logged in" do
8
+ before do
9
+ get :index
10
+ end
11
+ it "redirects to the home page" do
12
+ expect ( response ) . to redirect_to ( root_path )
13
+ end
14
+ end
15
+
16
+ context "when user is an admin" do
17
+ let ( :fake_relation ) { instance_double ( 'ActiveRecord::Relation' ) }
18
+ let ( :fake_juliet ) { instance_double ( 'Member' , id : 1 , name : 'Juliet' , surname : 'Montague' ) }
19
+
20
+ before do
21
+ login_as_admin ( member )
22
+ get :index
23
+ end
24
+ it "shows user the search page" do
25
+ expect ( response ) . to have_http_status ( :ok )
26
+ end
27
+
28
+ context "and when admin user searches for a single existing user" do
29
+ before do
30
+ allow ( Member ) . to receive ( :find_members ) . with ( 'Juliet' ) . and_return ( fake_relation )
31
+ allow ( fake_relation ) . to receive ( :select ) . with ( any_args ) . and_return ( [ fake_juliet ] )
32
+ get :index , params : { member_search : { name : "Juliet" , callback : root_path } }
33
+ end
34
+ it "redirects to the calling service" do
35
+ expect ( response ) . to have_http_status ( :found )
36
+
37
+ uri = URI . parse ( response . location )
38
+ redirect_params = Rack ::Utils . parse_nested_query ( uri . query )
39
+
40
+ expect ( redirect_params [ "member_pick" ] [ "members" ] ) . to eq ( [ "1" ] )
41
+ end
42
+ end
43
+
44
+ context "and when an admin user searches and there are multiple results" do
45
+ let ( :fake_romeo ) { double ( 'Member' , id : 2 , name : 'Romeo' , surname : 'Capulet' ) }
46
+
47
+ before do
48
+ allow ( Member ) . to receive ( :find_members ) . with ( 'e' ) . and_return ( fake_relation )
49
+ allow ( fake_relation ) . to receive ( :select ) . with ( any_args ) . and_return ( [ fake_juliet , fake_romeo ] )
50
+ get :index , params : { member_search : { name : 'e' , callback : root_path } }
51
+ end
52
+ it "presents the found members on the index page" do
53
+ expect ( response ) . to have_http_status ( :ok )
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
0 commit comments