Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Startouf/65e9fb49ca3f04a6d986f1eb13889c26 to your computer and use it in GitHub Desktop.
Save Startouf/65e9fb49ca3f04a6d986f1eb13889c26 to your computer and use it in GitHub Desktop.
Refactor jsonapi-suite sideposting into shared examples for RSpec
# spec/requests/api/create_post_spec.rb
require 'rails_helper'
require 'requests/api/concerns/commentable_sidepost_spec.rb'
describe API::V1::PostsController, type: :request do
let(:user) { create(:user) }
let(:author) { user }
let(:post_attributes) do {
title: Faker::Lorem::Sentence.new,
author_id: author.id.to_s
} end
describe '#create' do
let(:payload) do {
data: {
type: 'post',
attributes: post_attributes,
relationships: relationships,
included: included_data
}
} end
let(:relationships) { {} }
let(:included_data) { {} }
context 'signed in as a user' do
let(:auth_headers) { json_token_auth_headers_for(user) }
it_should_behave_like('a commentable object') do
let(:commentable) { Post.first }
let(:request) { create_request }
end
end
end
def create_request
post api_v1_posts_path, params: payload, headers: auth_headers
end
end
# /requests/api/concerns/commentable_sidepost_spec.rb
require 'spec_helper'
shared_examples_for 'an commentable object' do
context 'with 2 comments' do
let(:comments) { FactoryGirl.build_list(:comment, 2)}
let(:relationships) do {
comments: {
data: comments.map.with_index do |tag, i|
{
type: 'comment',
'temp-id': i,
method: 'create'
}
end
}
} end
let(:included_data) do
comments.map.with_index do |com, i|
{
type: 'comment',
'temp-id': i,
attributes: com.as_json(only: [:title, :description])
}
end
end
it 'creates comments via sidepost' do
create_request
comments_in_db = commentable.reload.comments
expect(comments_in_db.map(&:title)).to eq(comments.map(&:title))
expect(comments_in_db.map(&:description)).to eq(comments.map(&:description))
end
end
end
@Startouf
Copy link
Author

Some notes :
1 - I've been passing author_id instead of using relationships, but actually in my code I scoped to the current_user so I just added that there but could as well have removed it.

2 - For clarity, when the create_request kicks in, the payload with comments look like

{
    :data => {
         :type => "post",
           :attributes => {
            :title => "Lorem....",
        },
        :relationships => {
            :comments => {
                :data => [
                    [0] {
                             :type => "comment",
                        :"temp-id" => 0,
                           :method => "create"
                    },
                    [1] {
                             :type => "comment",
                        :"temp-id" => 1,
                           :method => "create"
                    }
                ]
            }
        },
             :included => [
            [0] {
                      :type => "comment",
                 :"temp-id" => 0,
                :attributes => {
                    "description" => "Sit dicta praesentium quis reiciendis reprehenderit corporis sint. Illo voluptas voluptas dolores. Est eum voluptatem incidunt et praesentium.",
                          "title" => "Similique recusandae reiciendis sed."
                }
            },
            [1] {
                      :type => "comment",
                 :"temp-id" => 1,
                :attributes => {
                    "description" => "Cupiditate expedita dolor sunt natus sed ad. Et modi sed ratione cupiditate qui. Dolores ratione dolore odio. Aut nihil commodi laborum eum iusto autem.",
                          "title" => "Illo architecto sed sequi explicabo sunt asperiores quasi quia."
                }
            }
        ]
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment