|
3 | 3 | require 'rails_helper'
|
4 | 4 |
|
5 | 5 | RSpec.describe Project do
|
| 6 | + before do |
| 7 | + stub_user_info_api |
| 8 | + end |
| 9 | + |
6 | 10 | describe 'associations', :sample_words do
|
7 | 11 | it { is_expected.to belong_to(:school).optional(true) }
|
8 | 12 | it { is_expected.to belong_to(:lesson).optional(true) }
|
|
68 | 72 | expect { unsaved_project.valid? }.to change { unsaved_project.identifier.nil? }.from(true).to(false)
|
69 | 73 | end
|
70 | 74 | end
|
| 75 | + |
| 76 | + describe '.users' do |
| 77 | + it 'returns User instances for the current scope' do |
| 78 | + create(:project) |
| 79 | + |
| 80 | + user = described_class.all.users.first |
| 81 | + expect(user.name).to eq('School Student') |
| 82 | + end |
| 83 | + |
| 84 | + it 'ignores members where no profile account exists' do |
| 85 | + create(:project, user_id: SecureRandom.uuid) |
| 86 | + |
| 87 | + user = described_class.all.users.first |
| 88 | + expect(user).to be_nil |
| 89 | + end |
| 90 | + |
| 91 | + it 'ignores members not included in the current scope' do |
| 92 | + create(:project) |
| 93 | + |
| 94 | + user = described_class.none.users.first |
| 95 | + expect(user).to be_nil |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + describe '.with_users' do |
| 100 | + it 'returns an array of class members paired with their User instance' do |
| 101 | + project = create(:project) |
| 102 | + |
| 103 | + pair = described_class.all.with_users.first |
| 104 | + user = described_class.all.users.first |
| 105 | + |
| 106 | + expect(pair).to eq([project, user]) |
| 107 | + end |
| 108 | + |
| 109 | + it 'returns nil values for members where no profile account exists' do |
| 110 | + project = create(:project, user_id: SecureRandom.uuid) |
| 111 | + |
| 112 | + pair = described_class.all.with_users.first |
| 113 | + expect(pair).to eq([project, nil]) |
| 114 | + end |
| 115 | + |
| 116 | + it 'ignores members not included in the current scope' do |
| 117 | + create(:project) |
| 118 | + |
| 119 | + pair = described_class.none.with_users.first |
| 120 | + expect(pair).to be_nil |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + describe '#with_user' do |
| 125 | + it 'returns the class member paired with their User instance' do |
| 126 | + project = create(:project) |
| 127 | + |
| 128 | + pair = project.with_user |
| 129 | + user = described_class.all.users.first |
| 130 | + |
| 131 | + expect(pair).to eq([project, user]) |
| 132 | + end |
| 133 | + |
| 134 | + it 'returns a nil value if the member has no profile account' do |
| 135 | + project = create(:project, user_id: SecureRandom.uuid) |
| 136 | + |
| 137 | + pair = project.with_user |
| 138 | + expect(pair).to eq([project, nil]) |
| 139 | + end |
| 140 | + end |
71 | 141 | end
|
0 commit comments