A platform for community discussion. Free, open, simple.
# frozen_string_literal: true
RSpec.describe JsonApiKit::Sideload do
subject(:sideload) { described_class.new(relationship, paths:, rows:, request:, owner_resource:) }
fab!(:author, :user)
fab!(:topic) { Fabricate(:topic, user: author, title: "A topic read with its author") }
let(:users_resource) do
Class.new(JsonApiKit::Resource) do
model User
type :users
attribute :username
end
end
let(:relationship) do
JsonApiKit::Declarations::Relationship::ToOne.new(:user, resource: users_resource)
end
let(:paths) { JsonApiKit::Paths.new([]) }
let(:records) { [topic] }
let(:rows) do
records.map do
JsonApiKit::Adapters::ActiveRecord::Pagination::Row.new(record: it, segment: nil)
end
end
let(:params) { {} }
let(:request) do
JsonApiKit::Request::Collection.new(
params,
guardian: Guardian.new,
edition: JsonApiKit::Edition.current,
)
end
let(:owner_resource) do
Class
.new(JsonApiKit::Resource) do
model Topic
type :topics
end
.new(guardian: request.guardian, edition: request.edition)
end
describe "#linkage_for" do
subject(:linkage) { sideload.linkage_for(rows.first) }
it "holds the record that the row relates to" do
expect(linkage.records.map(&:record)).to eq([author])
end
it "holds it under the linkage that the relationship declares" do
expect(linkage).to be_a(JsonApiKit::Linkage::ToOne)
end
context "when the row relates to nothing" do
fab!(:without_category) { Fabricate(:private_message_topic, title: "A topic in no category") }
let(:categories_resource) do
Class.new(JsonApiKit::Resource) do
model Category
type :categories
end
end
let(:relationship) do
JsonApiKit::Declarations::Relationship::ToOne.new(:category, resource: categories_resource)
end
let(:records) { [without_category] }
it "holds no record" do
expect(linkage.records).to be_empty
end
end
context "when a request asks for fields of the related type" do
let(:params) { { fields: { users: %w[username] } } }
it "renders the related record as those fields" do
expect(linkage.records.map(&:attributes)).to eq(
[{ JsonApiKit::Name::Field.new(value: "username", type: "users") => author.username }],
)
end
end
context "when the page holds several rows" do
fab!(:another) { Fabricate(:topic, user: author, title: "Another topic by that author") }
let(:records) { [topic, another] }
let(:user_queries) do
track_sql_queries { rows.each { sideload.linkage_for(it) } }.grep(/FROM "users"/).size
end
def related_record(row) = sideload.linkage_for(row).records.first
it "loads the related rows of them all in one query" do
expect(user_queries).to eq(1)
end
it "gives two rows that relate to one row the same record" do
expect(related_record(rows.first)).to be(related_record(rows.last))
end
end
end
end