From a8502fbb8fa9c03dd3410090f175e5eb7d3e751c Mon Sep 17 00:00:00 2001 From: nov Date: Fri, 30 May 2014 16:10:01 +0900 Subject: [PATCH] likes & comments test --- lib/fb_graph2/edge/comments.rb | 2 +- lib/fb_graph2/edge/likes.rb | 2 +- spec/fb_graph2/edge/comments_spec.rb | 23 ++++++++++ spec/fb_graph2/edge/feed_spec.rb | 2 +- spec/fb_graph2/edge/likes_spec.rb | 64 +++++++++++++++++++++++++++- spec/mock_json/blank_collection.json | 1 + spec/mock_json/post/comments.json | 20 +++++++++ spec/mock_json/post/likes.json | 15 +++++++ spec/mock_json/user/likes.json | 33 ++++++++++++++ 9 files changed, 158 insertions(+), 4 deletions(-) create mode 100644 spec/mock_json/blank_collection.json create mode 100644 spec/mock_json/post/comments.json create mode 100644 spec/mock_json/post/likes.json create mode 100644 spec/mock_json/user/likes.json diff --git a/lib/fb_graph2/edge/comments.rb b/lib/fb_graph2/edge/comments.rb index 4001c4f..32eb3ad 100644 --- a/lib/fb_graph2/edge/comments.rb +++ b/lib/fb_graph2/edge/comments.rb @@ -1,7 +1,7 @@ module FbGraph2 class Edge module Comments - def initialize(id, attributes) + def assign(attributes) super if attributes.include?(:comments) @_cached_comments = Collection.new attributes[:comments] diff --git a/lib/fb_graph2/edge/likes.rb b/lib/fb_graph2/edge/likes.rb index 1ad6ea7..f44cff5 100644 --- a/lib/fb_graph2/edge/likes.rb +++ b/lib/fb_graph2/edge/likes.rb @@ -16,7 +16,7 @@ module FbGraph2 end module LikeeContext - def initialize(id, attributes) + def assign(attributes) super if attributes.include?(:likes) @_cached_likes = Collection.new attributes[:likes] diff --git a/spec/fb_graph2/edge/comments_spec.rb b/spec/fb_graph2/edge/comments_spec.rb index fa401cc..3e67249 100644 --- a/spec/fb_graph2/edge/comments_spec.rb +++ b/spec/fb_graph2/edge/comments_spec.rb @@ -28,6 +28,29 @@ describe FbGraph2::Edge::Comments do end end end + + context 'otherwise' do + it 'should return an Array of FbGraph2::Post' do + comments = mock_graph :get, 'post_id/comments', 'post/comments', access_token: 'token' do + post.comments + end + comments.should_not be_blank + comments.each do |comment| + comment.should be_instance_of FbGraph2::Comment + end + end + end + end + + describe '#comment!' do + it 'should return FbGraph2::Post with posted params' do + comment = mock_graph :post, 'post_id/comments', 'success_with_id', access_token: 'token' do + post.comment! message: 'hello' + end + comment.should be_instance_of FbGraph2::Comment + comment.id.should == 'created_object_id' + comment.message.should == 'hello' + end end end end \ No newline at end of file diff --git a/spec/fb_graph2/edge/feed_spec.rb b/spec/fb_graph2/edge/feed_spec.rb index 6dfa8e0..fca66fe 100644 --- a/spec/fb_graph2/edge/feed_spec.rb +++ b/spec/fb_graph2/edge/feed_spec.rb @@ -16,7 +16,7 @@ describe FbGraph2::Edge::Feed do end end - describe 'feed!' do + describe '#feed!' do it 'should return FbGraph2::Post with posted params' do post = mock_graph :post, 'me/feed', 'success_with_id', access_token: 'token' do me.feed! message: 'hello' diff --git a/spec/fb_graph2/edge/likes_spec.rb b/spec/fb_graph2/edge/likes_spec.rb index 1e1cdc9..4fefe1f 100644 --- a/spec/fb_graph2/edge/likes_spec.rb +++ b/spec/fb_graph2/edge/likes_spec.rb @@ -1,10 +1,44 @@ require 'spec_helper' describe FbGraph2::Edge::Likes do + context 'included in User' do + let(:me) { FbGraph2::User.me('token') } + + describe '#likes' do + it 'should return an Array of FbGraph2::Page' do + likes = mock_graph :get, 'me/likes', 'user/likes', access_token: 'token' do + me.likes + end + likes.should_not be_blank + likes.each do |like| + like.should be_instance_of FbGraph2::Page + end + end + end + + describe '#liked?' do + context 'liked' do + it do + mock_graph :get, 'me/likes/page_id', 'user/likes', access_token: 'token' do + me.liked? 'page_id' + end.should be_true + end + end + + context 'otherwise' do + it do + mock_graph :get, 'me/likes/page_id', 'blank_collection', access_token: 'token' do + me.liked? 'page_id' + end.should be_false + end + end + end + end + context 'included in Post' do let(:post) { FbGraph2::Post.new('post_id').authenticate('token') } - describe '#comments' do + describe '#likes' do context 'when cached' do let(:fetched) do mock_graph :get, 'post_id', 'post/liked_and_commented', access_token: 'token' do @@ -28,6 +62,34 @@ describe FbGraph2::Edge::Likes do end end end + + context 'otherwise' do + it 'should return an Array of FbGraph2::User' do + likes = mock_graph :get, 'post_id/likes', 'post/likes', access_token: 'token' do + post.likes + end + likes.should_not be_blank + likes.each do |like| + like.should be_instance_of FbGraph2::User + end + end + end + end + + describe '#like!' do + it 'should return true' do + mock_graph :post, 'post_id/likes', 'true', access_token: 'token' do + post.like! + end.should be_true + end + end + + describe '#unlike!' do + it 'should return true' do + mock_graph :delete, 'post_id/likes', 'true', access_token: 'token' do + post.unlike! + end.should be_true + end end end end \ No newline at end of file diff --git a/spec/mock_json/blank_collection.json b/spec/mock_json/blank_collection.json new file mode 100644 index 0000000..ccb5621 --- /dev/null +++ b/spec/mock_json/blank_collection.json @@ -0,0 +1 @@ +{"data":[]} \ No newline at end of file diff --git a/spec/mock_json/post/comments.json b/spec/mock_json/post/comments.json new file mode 100644 index 0000000..8f088cf --- /dev/null +++ b/spec/mock_json/post/comments.json @@ -0,0 +1,20 @@ +{ + "data": [{ + "id": "10152461220962277_10152461646087277", + "from": { + "id": "789665437731725", + "name": "Toyoaki Ohgochi" + }, + "message": "....ポイントも付きますよ....", + "can_remove": true, + "created_time": "2014-05-24T15:05:57+0000", + "like_count": 0, + "user_likes": false + }], + "paging": { + "cursors": { + "after": "MQ==", + "before": "MQ==" + } + } +} diff --git a/spec/mock_json/post/likes.json b/spec/mock_json/post/likes.json new file mode 100644 index 0000000..cc282b9 --- /dev/null +++ b/spec/mock_json/post/likes.json @@ -0,0 +1,15 @@ +{ + "data": [{ + "id": "689134294485152", + "name": "Shunya Iriki" + }, { + "id": "1486426304903512", + "name": "Masato Nagai" + }], + "paging": { + "cursors": { + "after": "MTQ4NjQyNjMwNDkwMzUxMg==", + "before": "Njg5MTM0Mjk0NDg1MTUy" + } + } +} diff --git a/spec/mock_json/user/likes.json b/spec/mock_json/user/likes.json new file mode 100644 index 0000000..6a3c57b --- /dev/null +++ b/spec/mock_json/user/likes.json @@ -0,0 +1,33 @@ +{ + "data": [{ + "category": "Business/economy website", + "name": "デザイナーズSOHOオフィスなら【SOHO東京】", + "created_time": "2014-04-27T07:42:29+0000", + "id": "140108926038346" + }, { + "category": "Real estate", + "category_list": [{ + "id": "198327773511962", + "name": "Real Estate" + }], + "name": "ジョイライフスタイル", + "created_time": "2014-04-05T15:26:28+0000", + "id": "117500321741291" + }, { + "category": "Internet/software", + "category_list": [{ + "id": "2256", + "name": "Internet/Software" + }], + "name": "Engineerrise", + "created_time": "2014-03-28T14:32:31+0000", + "id": "565244630160593" + }], + "paging": { + "cursors": { + "after": "MzY3OTg5NTQzMjU5NzU3", + "before": "MTQwMTA4OTI2MDM4MzQ2" + }, + "next": "https://graph.facebook.com/v2.0/579612276/likes?limit=25&after=MzY3OTg5NTQzMjU5NzU3" + } +}