From 80cd234f205ea0e9ce49389c0d9104a16dc87f19 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 19 Jun 2023 13:26:39 +1000 Subject: [PATCH] test: extract shared examples for "a paginated response" --- spec/features/get_pacticipants_spec.rb | 14 ++------------ spec/support/shared_examples_for_responses.rb | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/spec/features/get_pacticipants_spec.rb b/spec/features/get_pacticipants_spec.rb index aba88b906..97232bc97 100644 --- a/spec/features/get_pacticipants_spec.rb +++ b/spec/features/get_pacticipants_spec.rb @@ -25,21 +25,11 @@ context "with pagination options" do subject { get(path, { "pageSize" => "2", "pageNumber" => "1" }) } - it "paginates the response" do + it "only returns the number of items specified in the pageSize" do expect(response_body_hash[:_links][:"pacticipants"].size).to eq 2 end - it "includes the pagination relations" do - expect(response_body_hash[:_links]).to have_key(:next) - end - - it "includes the page section" do - expect(response_body_hash).to have_key(:page) - end - - it "outputs a whole number for total pages" do - expect(response_body_hash[:page][:totalPages]).to eq(2) - end + it_behaves_like "a paginated response" end end end diff --git a/spec/support/shared_examples_for_responses.rb b/spec/support/shared_examples_for_responses.rb index e4f904b34..3c5538e20 100644 --- a/spec/support/shared_examples_for_responses.rb +++ b/spec/support/shared_examples_for_responses.rb @@ -9,6 +9,25 @@ end +shared_examples_for "a paginated response" do + let(:response_body_hash) { JSON.parse(subject.body, symbolize_names: true) } + + it "includes the pagination relations" do + expect(response_body_hash[:_links]).to have_key(:next) + end + + it "includes the page details" do + expect(response_body_hash).to include( + page: { + number: instance_of(Integer), + size: instance_of(Integer), + totalElements: instance_of(Integer), + totalPages: instance_of(Integer), + } + ) + end +end + require "rspec/expectations" RSpec::Matchers.define :be_a_hal_json_success_response do