From 2150d98af135e7aeab38c35e9e1cf8939f05d616 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Fri, 3 Feb 2017 11:41:02 +0100 Subject: [PATCH] Add specs for environments serializer pagination --- .../environment_serializer_spec.rb | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/spec/serializers/environment_serializer_spec.rb b/spec/serializers/environment_serializer_spec.rb index 3c37660885d..0eaf6db4761 100644 --- a/spec/serializers/environment_serializer_spec.rb +++ b/spec/serializers/environment_serializer_spec.rb @@ -52,4 +52,53 @@ describe EnvironmentSerializer do expect(json).to be_an_instance_of Array end end + + context 'when used with pagination' do + let(:request) { spy('request') } + let(:response) { spy('response') } + let(:resource) { Environment.all } + let(:pagination) { { page: 1, per_page: 2 } } + + let(:serializer) do + described_class.new(project: project) + .with_pagination(request, response) + end + + before do + allow(request).to receive(:query_parameters) + .and_return(pagination) + end + + subject { serializer.represent(resource) } + + it 'creates a paginated serializer' do + expect(serializer).to be_paginated + end + + context 'when resource is paginatable relation' do + context 'when there is a single environment object in relation' do + before { create(:environment) } + + it 'serializes environments' do + expect(subject.first).to have_key :id + end + end + + context 'when multiple environment objects are serialized' do + before { create_list(:environment, 3) } + + it 'serializes appropriate number of objects' do + expect(subject.count).to be 2 + end + + it 'appends relevant headers' do + expect(response).to receive(:[]=).with('X-Total', '3') + expect(response).to receive(:[]=).with('X-Total-Pages', '2') + expect(response).to receive(:[]=).with('X-Per-Page', '2') + + subject + end + end + end + end end