From 10a4ce2ee7c5d3d6183b3fc8d702c57c1787f909 Mon Sep 17 00:00:00 2001 From: Bruce Bolt Date: Tue, 24 Dec 2024 08:11:39 +0000 Subject: [PATCH] Add unit tests for GraphQL dataloader --- .../sources/linked_to_editions_source_spec.rb | 18 ++++++++++++++++++ .../person_current_roles_source_spec.rb | 19 +++++++++++++++++++ spec/support/ministers_index_helpers.rb | 4 ++-- 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 spec/graphql/sources/linked_to_editions_source_spec.rb create mode 100644 spec/graphql/sources/person_current_roles_source_spec.rb diff --git a/spec/graphql/sources/linked_to_editions_source_spec.rb b/spec/graphql/sources/linked_to_editions_source_spec.rb new file mode 100644 index 000000000..debc94851 --- /dev/null +++ b/spec/graphql/sources/linked_to_editions_source_spec.rb @@ -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 diff --git a/spec/graphql/sources/person_current_roles_source_spec.rb b/spec/graphql/sources/person_current_roles_source_spec.rb new file mode 100644 index 000000000..7d704c9cc --- /dev/null +++ b/spec/graphql/sources/person_current_roles_source_spec.rb @@ -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 diff --git a/spec/support/ministers_index_helpers.rb b/spec/support/ministers_index_helpers.rb index 81ee8fc0a..2fe5eb3b9 100644 --- a/spec/support/ministers_index_helpers.rb +++ b/spec/support/ministers_index_helpers.rb @@ -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), }, )