Skip to content

Commit

Permalink
Add unit tests for GraphQL dataloader
Browse files Browse the repository at this point in the history
  • Loading branch information
brucebolt committed Dec 24, 2024
1 parent 7babf9d commit 10a4ce2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
18 changes: 18 additions & 0 deletions spec/graphql/sources/linked_to_editions_source_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
RSpec.describe Sources::LinkedToEditionsSource do
it "returns the specified link set links" do
source_document = create(:edition)
target_document_1 = create(:edition)
target_document_2 = create(:edition)
target_document_3 = create(:edition)
link_set = create(:link_set, content_id: source_document.content_id)
create(:link, link_set: link_set, target_content_id: target_document_1.content_id, link_type: "test_link")
create(:link, link_set: link_set, target_content_id: target_document_2.content_id, link_type: "another_link_type")
create(:link, link_set: link_set, target_content_id: target_document_3.content_id, link_type: "test_link")

GraphQL::Dataloader.with_dataloading do |dataloader|
request = dataloader.with(described_class, parent_object: source_document).request("test_link")

expect(request.load).to eq([target_document_1, target_document_3])
end
end
end
19 changes: 19 additions & 0 deletions spec/graphql/sources/person_current_roles_source_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
RSpec.describe Sources::PersonCurrentRolesSource do
include MinistersIndexHelpers

it "returns the current roles for a person" do
person_1 = create_person("Person 1")
role_1 = create_role("Role 1")
role_2 = create_role("Role 2")
role_3 = create_role("Role 3")
appoint_person_to_role(person_1, role_1)
appoint_person_to_role(person_1, role_2, current: false)
appoint_person_to_role(person_1, role_3)

GraphQL::Dataloader.with_dataloading do |dataloader|
request = dataloader.with(described_class).request(person_1.content_id)

expect(request.load).to eq([role_1, role_3])
end
end
end
4 changes: 2 additions & 2 deletions spec/support/ministers_index_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ def create_role(title, role_payment_type: nil, whip_organisation: nil)
)
end

def appoint_person_to_role(person, role)
def appoint_person_to_role(person, role, current: true)
role_appointment = create(
:live_edition,
title: "#{person.title} - #{role.title}",
document_type: "role_appointment",
schema_name: "role_appointment",
details: {
current: true,
current:,
started_on: Time.zone.local(2024, 7, 5),
},
)
Expand Down

0 comments on commit 10a4ce2

Please sign in to comment.