-
Notifications
You must be signed in to change notification settings - Fork 8
/
bookmark_spec.rb
115 lines (93 loc) · 3.94 KB
/
bookmark_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
require 'bookmark'
require 'database_helpers'
require 'tag'
require 'bookmark_tag'
describe Bookmark do
let(:comment_class) { double(:comment_class) }
let(:tag_class) { double(:tag_class) }
describe '.all' do
it 'returns a list of bookmarks' do
# Add the test data
bookmark = Bookmark.create(url: "http://www.makersacademy.com", title: "Makers Academy")
Bookmark.create(url: "http://www.destroyallsoftware.com", title: "Destroy All Software")
Bookmark.create(url: "http://www.google.com", title: "Google")
bookmarks = Bookmark.all
expect(bookmarks.length).to eq 3
expect(bookmarks.first).to be_a Bookmark
expect(bookmarks.first.id).to eq bookmark.id
expect(bookmarks.first.title).to eq 'Makers Academy'
expect(bookmarks.first.url).to eq 'http://www.makersacademy.com'
end
end
describe '.create' do
it 'creates a new bookmark' do
bookmark = Bookmark.create(url: 'http://www.testbookmark.com', title: 'Test Bookmark')
persisted_data = persisted_data(id: bookmark.id, table: 'bookmarks')
expect(bookmark).to be_a Bookmark
expect(bookmark.id).to eq persisted_data.first['id']
expect(bookmark.title).to eq 'Test Bookmark'
expect(bookmark.url).to eq 'http://www.testbookmark.com'
end
it 'does not create a new bookmark if the URL is not valid' do
bookmark = Bookmark.create(url: 'not a real bookmark', title: 'not a real bookmark')
expect(bookmark).not_to be_a Bookmark
end
end
describe '.delete' do
it 'deletes the given bookmark' do
bookmark = Bookmark.create(title: 'Makers Academy', url: 'http://www.makersacademy.com')
Bookmark.delete(id: bookmark.id)
expect(Bookmark.all.length).to eq 0
end
end
describe '.update' do
it 'updates the bookmark with the given data' do
bookmark = Bookmark.create(title: 'Makers Academy', url: 'http://www.makersacademy.com')
updated_bookmark = Bookmark.update(id: bookmark.id, url: 'http://www.snakersacademy.com', title: 'Snakers Academy')
expect(updated_bookmark).to be_a Bookmark
expect(updated_bookmark.id).to eq bookmark.id
expect(updated_bookmark.title).to eq 'Snakers Academy'
expect(updated_bookmark.url).to eq 'http://www.snakersacademy.com'
end
end
describe '.find' do
it 'returns the requested bookmark object' do
bookmark = Bookmark.create(title: 'Makers Academy', url: 'http://www.makersacademy.com')
result = Bookmark.find(id: bookmark.id)
expect(result).to be_a Bookmark
expect(result.id).to eq bookmark.id
expect(result.title).to eq 'Makers Academy'
expect(result.url).to eq 'http://www.makersacademy.com'
end
end
describe '.where' do
it 'returns bookmarks with the given tag id' do
bookmark = Bookmark.create(url: "http://www.makersacademy.com", title: "Makers Academy")
tag1 = Tag.create(content: 'test tag 1')
tag2 = Tag.create(content: 'test tag 2')
BookmarkTag.create(bookmark_id: bookmark.id, tag_id: tag1.id)
BookmarkTag.create(bookmark_id: bookmark.id, tag_id: tag2.id)
bookmarks = Bookmark.where(tag_id: tag1.id)
result = bookmarks.first
expect(bookmarks.length).to eq 1
expect(result).to be_a Bookmark
expect(result.id).to eq bookmark.id
expect(result.title).to eq bookmark.title
expect(result.url).to eq bookmark.url
end
end
describe '#comments' do
it 'calls .where on the Comment class' do
bookmark = Bookmark.create(title: 'Makers Academy', url: 'http://www.makersacademy.com')
expect(comment_class).to receive(:where).with(bookmark_id: bookmark.id)
bookmark.comments(comment_class)
end
end
describe '#tags' do
it 'calls .where on the Tag class' do
bookmark = Bookmark.create(title: 'Makers Academy', url: 'http://www.makersacademy.com')
expect(tag_class).to receive(:where).with(bookmark_id: bookmark.id)
bookmark.tags(tag_class)
end
end
end