|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe User, type: :model do |
| 6 | + describe 'instances_method' do |
| 7 | + it 'responds to apply_sort' do |
| 8 | + expect(User).to respond_to(:apply_include) |
| 9 | + end |
| 10 | + |
| 11 | + it 'responds to allowed_includes' do |
| 12 | + expect(User).to respond_to(:allowed_includes) |
| 13 | + end |
| 14 | + end |
| 15 | + |
| 16 | + describe '#apply_include' do |
| 17 | + let!(:contact) { create(:contact) } |
| 18 | + let!(:user) { create(:user, contact: contact) } |
| 19 | + let!(:posts) { create_list(:post, 3, user: user) } |
| 20 | + |
| 21 | + context 'with valid params' do |
| 22 | + let(:valid_params) do |
| 23 | + { |
| 24 | + include: 'contact,posts' |
| 25 | + } |
| 26 | + end |
| 27 | + |
| 28 | + it 'includes relationships' do |
| 29 | + users = User.apply_include(valid_params) |
| 30 | + expect(users.first.association(:contact)).to be_loaded |
| 31 | + expect(users.first.association(:posts)).to be_loaded |
| 32 | + end |
| 33 | + |
| 34 | + it 'includes sub relationships' do |
| 35 | + valid_params = { |
| 36 | + include: 'contact,posts.contact.users,posts.user' |
| 37 | + } |
| 38 | + |
| 39 | + users = User.apply_include(valid_params) |
| 40 | + expect(users.first.association(:contact)).to be_loaded |
| 41 | + expect(users.first.posts.first.association(:contact)).to be_loaded |
| 42 | + expect(users.first.posts.first.association(:user)).to be_loaded |
| 43 | + expect(users.first.posts.first.contact.association(:users)).to be_loaded |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + context 'with custom allowed attributes' do |
| 48 | + let(:valid_params) do |
| 49 | + { |
| 50 | + include: 'posts.contact' |
| 51 | + } |
| 52 | + end |
| 53 | + |
| 54 | + it 'raises an error' do |
| 55 | + expect { User.apply_include(valid_params) }.to raise_exception(Jsonapi::InvalidAttributeError, 'posts.contact is not valid as include attribute.') |
| 56 | + end |
| 57 | + |
| 58 | + it 'includes sub relationships' do |
| 59 | + users = User.apply_include(valid_params, allowed: 'posts.contact') |
| 60 | + expect(users.first.posts.first.association(:contact)).to be_loaded |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + context 'with invalid params' do |
| 65 | + let(:invalid_params) do |
| 66 | + { |
| 67 | + include: 'invalid' |
| 68 | + } |
| 69 | + end |
| 70 | + |
| 71 | + it 'raises an exception' do |
| 72 | + expect { User.apply_include(invalid_params) }.to raise_exception(Jsonapi::InvalidAttributeError, 'invalid is not valid as include attribute.') |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + context 'with empty params' do |
| 77 | + let(:empty_params) do |
| 78 | + { |
| 79 | + include: '' |
| 80 | + } |
| 81 | + end |
| 82 | + |
| 83 | + it 'returns same collection' do |
| 84 | + expect(User.apply_include(empty_params).first).to eq(User.first) |
| 85 | + end |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + describe '#convert_includes_as_hash' do |
| 90 | + it 'returns the correct hash' do |
| 91 | + expect(User.send(:convert_includes_as_hash, 'contact')).to eq(contact: {}) |
| 92 | + expect(User.send(:convert_includes_as_hash, 'contact,posts')).to eq(contact: {}, posts: {}) |
| 93 | + expect(User.send(:convert_includes_as_hash, 'contact,posts.user')).to eq(contact: {}, posts: { user: {} }) |
| 94 | + expect(User.send(:convert_includes_as_hash, 'contact,posts.user,posts.contact')).to eq(contact: {}, posts: { user: {}, contact: {} }) |
| 95 | + expect(User.send(:convert_includes_as_hash, 'contact,posts.user,posts.contact.users')).to eq(contact: {}, posts: { user: {}, contact: { users: {} } }) |
| 96 | + expect(User.send(:convert_includes_as_hash, 'contact,posts.user,posts.contact.users,contact.users')).to eq(contact: { users: {} }, posts: { user: {}, contact: { users: {} } }) |
| 97 | + expect(User.send(:convert_includes_as_hash, 'contact,posts.user,posts.contact,posts.user.contacts')).to eq(contact: {}, posts: { user: { contacts: {} }, contact: {} }) |
| 98 | + expect(User.send(:convert_includes_as_hash, '')).to eq({}) |
| 99 | + end |
| 100 | + end |
| 101 | +end |
0 commit comments