|
1 | | -RSpec.describe JSONAPI::RSpec do |
2 | | - json_doc = |
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +RSpec.describe JSONAPI::RSpec, '#have_relationship(s)' do |
| 4 | + let(:doc) do |
3 | 5 | { |
4 | 6 | 'relationships' => { |
5 | | - 'posts' => { |
6 | | - 'data' => { |
7 | | - 'id' => '1', |
8 | | - 'type' => 'posts' |
9 | | - } |
| 7 | + 'user' => { |
| 8 | + 'data' => { 'id' => '1', 'type' => 'user' } |
10 | 9 | }, |
11 | 10 | 'comments' => { |
12 | | - 'data' => [{ |
13 | | - 'id' => '1', |
14 | | - 'type' => 'posts' |
15 | | - }, { |
16 | | - 'id' => '2', |
17 | | - 'type' => 'hides' |
18 | | - }] |
| 11 | + 'data' => [ |
| 12 | + { 'id' => '1', 'type' => 'comment' }, |
| 13 | + { 'id' => '2', 'type' => 'comment' } |
| 14 | + ] |
19 | 15 | } |
20 | 16 | } |
21 | 17 | } |
| 18 | + end |
22 | 19 |
|
23 | | - describe '#have_relationship' do |
24 | | - context 'when relationships is present' do |
25 | | - it { expect(json_doc).to have_relationship('posts') } |
26 | | - it { expect(json_doc).not_to have_relationship('mails') } |
27 | | - it { expect(json_doc).to have_relationship('posts').with_data({ 'id' => '1', 'type' => 'posts' }) } |
28 | | - it do |
29 | | - expect(json_doc).to have_relationship('comments').with_data( |
30 | | - [{ 'id' => '1', 'type' => 'posts' }, { 'id' => '2', 'type' => 'hides' }] |
31 | | - ) |
32 | | - end |
33 | | - end |
| 20 | + it { expect(doc).not_to have_relationships('user', 'comments', 'authors') } |
| 21 | + it { expect(doc).to have_relationships('user', 'comments') } |
34 | 22 |
|
35 | | - context 'when relationships is not present' do |
36 | | - it { expect({}).not_to have_relationship('posts') } |
37 | | - end |
| 23 | + it { expect(doc).not_to have_relationship('authors') } |
| 24 | + it { expect(doc).to have_relationship('user') } |
| 25 | + |
| 26 | + it do |
| 27 | + expect(doc).to have_relationship('user').with_data( |
| 28 | + { 'id' => '1', 'type' => 'user' } |
| 29 | + ) |
38 | 30 | end |
39 | 31 |
|
40 | | - describe '#have_relationships' do |
41 | | - context 'when relationships is present' do |
42 | | - it { expect(json_doc).to have_relationships('posts', 'comments') } |
43 | | - it { expect(json_doc).not_to have_relationships('posts', 'comments', 'mails') } |
| 32 | + it do |
| 33 | + expect(doc).to have_relationship('comments').with_data( |
| 34 | + [ |
| 35 | + { 'id' => '1', 'type' => 'comment' }, |
| 36 | + { 'id' => '2', 'type' => 'comment' } |
| 37 | + ] |
| 38 | + ) |
| 39 | + end |
| 40 | + |
| 41 | + context 'with jsonapi indifferent hash enabled' do |
| 42 | + before(:all) { ::RSpec.configuration.jsonapi_indifferent_hash = true } |
| 43 | + after(:all) { ::RSpec.configuration.jsonapi_indifferent_hash = false } |
| 44 | + |
| 45 | + it { expect(doc).to have_relationships(:user, :comments) } |
| 46 | + |
| 47 | + it do |
| 48 | + expect(doc).to have_relationship('user').with_data(id: '1', type: :user) |
44 | 49 | end |
45 | 50 |
|
46 | | - context 'when relationships is not present' do |
47 | | - it { expect({}).not_to have_relationships('posts', 'comments') } |
| 51 | + it do |
| 52 | + expect(doc).to have_relationship('comments').with_data( |
| 53 | + [ |
| 54 | + { id: '1', type: 'comment' }, |
| 55 | + { id: '2', type: 'comment' } |
| 56 | + ] |
| 57 | + ) |
48 | 58 | end |
49 | 59 | end |
50 | 60 | end |
0 commit comments