|
1 | | -require 'rails_helper' |
| 1 | +require "rails_helper" |
2 | 2 |
|
3 | 3 | RSpec.describe "users/index", type: :view do |
4 | | - let(:admin_user) { create(:user, :admin) } # or super_user trait |
| 4 | + let(:admin_user) { create(:user, :admin) } |
5 | 5 |
|
6 | | - before(:each) do |
7 | | - allow(view).to receive(:current_user).and_return(admin_user) # Stub current_user for Devise |
8 | | - users = create_list(:user, 3, first_name: "Alice") |
9 | | - paginated_users = WillPaginate::Collection.create(1, 10, users.size) do |pager| |
10 | | - pager.replace(users) |
| 6 | + before do |
| 7 | + allow(view).to receive(:current_user).and_return(admin_user) |
| 8 | + end |
| 9 | + |
| 10 | + context "when users have facilitators" do |
| 11 | + before do |
| 12 | + @users = create_list(:user, 2, :with_facilitator) # Factory should build facilitator + avatar if needed |
| 13 | + |
| 14 | + paginated = WillPaginate::Collection.create(1, 10, @users.size) do |pager| |
| 15 | + pager.replace(@users) |
| 16 | + end |
| 17 | + |
| 18 | + assign(:users, paginated) |
| 19 | + assign(:users_count, @users.size) |
| 20 | + end |
| 21 | + |
| 22 | + it "renders facilitator profile buttons instead of 'Create facilitator'" do |
| 23 | + render |
| 24 | + |
| 25 | + # Two rows |
| 26 | + expect(rendered).to have_selector("table tbody tr", count: 2) |
| 27 | + |
| 28 | + @users.each do |user| |
| 29 | + facilitator = user.facilitator |
| 30 | + |
| 31 | + # The helper output (button) must appear |
| 32 | + expect(rendered).to include(facilitator.name) |
| 33 | + |
| 34 | + # Should NOT show "Create facilitator" |
| 35 | + expect(rendered).not_to include("Create facilitator") |
| 36 | + end |
11 | 37 | end |
12 | | - assign(:users, paginated_users) |
13 | 38 | end |
14 | 39 |
|
15 | | - it "renders a list of users" do |
16 | | - render |
| 40 | + context "when a user has NO facilitator" do |
| 41 | + let!(:user_without_facilitator) { create(:user) } |
| 42 | + |
| 43 | + before do |
| 44 | + paginated = WillPaginate::Collection.create(1, 10, 1) do |pager| |
| 45 | + pager.replace([user_without_facilitator]) |
| 46 | + end |
17 | 47 |
|
18 | | - assert_select "table tbody tr", count: 3 |
19 | | - assert_select "table tbody tr td", text: /Alice/ |
| 48 | + assign(:users, paginated) |
| 49 | + assign(:users_count, 1) |
| 50 | + end |
| 51 | + |
| 52 | + it "shows 'Create facilitator' button" do |
| 53 | + render |
| 54 | + |
| 55 | + expect(rendered).to include("Create facilitator") |
| 56 | + expect(rendered).to have_link( |
| 57 | + "Create facilitator", |
| 58 | + href: new_facilitator_path(user_id: user_without_facilitator.id) |
| 59 | + ) |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + context "general index behavior" do |
| 64 | + before do |
| 65 | + users = create_list(:user, 3) |
| 66 | + |
| 67 | + paginated = WillPaginate::Collection.create(1, 10, users.size) do |pager| |
| 68 | + pager.replace(users) |
| 69 | + end |
| 70 | + |
| 71 | + assign(:users, paginated) |
| 72 | + assign(:users_count, users.size) |
| 73 | + end |
| 74 | + |
| 75 | + it "renders correct table structure" do |
| 76 | + render |
| 77 | + |
| 78 | + expect(rendered).to have_selector("table thead tr th", text: "Name") |
| 79 | + expect(rendered).to have_selector("table thead tr th", text: "Email") |
| 80 | + expect(rendered).to have_selector("table tbody tr", count: 3) |
| 81 | + end |
20 | 82 | end |
21 | 83 | end |
0 commit comments