-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ReaderCardServiceTests.swift
215 lines (171 loc) · 8.14 KB
/
ReaderCardServiceTests.swift
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import UIKit
import XCTest
import Nimble
@testable import WordPress
class ReaderCardServiceTests: CoreDataTestCase {
private var remoteService: ReaderPostServiceRemote!
private var followedInterestsService: ReaderFollowedInterestsServiceMock!
private var apiMock: WordPressComMockRestApi!
override func setUp() {
super.setUp()
apiMock = WordPressComMockRestApi()
followedInterestsService = ReaderFollowedInterestsServiceMock(context: mainContext)
remoteService = ReaderPostServiceRemote(wordPressComRestApi: apiMock)
}
/// Call the cards API with the saved slugs
///
func testCallApiWithTheSavedSlugs() {
let expectation = self.expectation(description: "Call the API with pug and cat slugs")
apiMock.succeed = true
let service = ReaderCardService(service: remoteService, coreDataStack: contextManager, followedInterestsService: followedInterestsService)
service.fetch(isFirstPage: true, success: { _, _ in
expect(self.apiMock.GETCalledWithURL).to(contain("tags%5B%5D=pug"))
expect(self.apiMock.GETCalledWithURL).to(contain("tags%5B%5D=cat"))
expectation.fulfill()
}, failure: { _ in })
waitForExpectations(timeout: 5, handler: nil)
}
/// Call the cards API with the page handle when requesting the next page
///
func testCallApiWithPageHandleWhenRequestingNextPage() {
let expectation = self.expectation(description: "Call the API with pug and cat slugs")
apiMock.succeed = true
let service = ReaderCardService(service: remoteService, coreDataStack: contextManager, followedInterestsService: followedInterestsService)
service.fetch(isFirstPage: true, success: { _, _ in
service.fetch(isFirstPage: false, success: { _, _ in
expect(self.apiMock.GETCalledWithURL).to(contain("&page_handle=ZnJvbT0xMCZiZWZvcmU9MjAyMC0wNy0yNlQxMyUzQTU1JTNBMDMlMkIwMSUzQTAw"))
expectation.fulfill()
}, failure: { _ in })
}, failure: { _ in })
waitForExpectations(timeout: 5, handler: nil)
}
/// Returns an error if the user don't follow any Interest
///
func testReturnErrorWhenNotFollowingAnyInterest() {
let expectation = self.expectation(description: "Error when now following interests")
apiMock.succeed = true
let service = ReaderCardService(service: remoteService, coreDataStack: contextManager, followedInterestsService: followedInterestsService)
followedInterestsService.returnInterests = false
service.fetch(isFirstPage: true, success: { _, _ in }, failure: { error in
expect(error).toNot(beNil())
expectation.fulfill()
})
waitForExpectations(timeout: 5, handler: nil)
}
/// Save 10 cards in the database
/// The API returns 11, but one of them is unknown and shouldn't be saved
///
func testSaveCards() {
let expectation = self.expectation(description: "10 reader cards should be returned")
let service = ReaderCardService(service: remoteService, coreDataStack: contextManager, followedInterestsService: followedInterestsService)
apiMock.succeed = true
service.fetch(isFirstPage: true, success: { _, _ in
let cards = try? self.mainContext.fetch(NSFetchRequest(entityName: ReaderCard.classNameWithoutNamespaces()))
expect(cards?.count).to(equal(10))
expectation.fulfill()
}, failure: { _ in })
waitForExpectations(timeout: 5, handler: nil)
}
/// From the 10 cards saved, 8 should have posts
///
func testSaveCardsWithPosts() {
let expectation = self.expectation(description: "8 cards with posts should be returned")
let service = ReaderCardService(service: remoteService, coreDataStack: contextManager, followedInterestsService: followedInterestsService)
apiMock.succeed = true
service.fetch(isFirstPage: true, success: { _, _ in
let cards = try? self.mainContext.fetch(NSFetchRequest(entityName: ReaderCard.classNameWithoutNamespaces())) as? [ReaderCard]
expect(cards?.filter { $0.post != nil }.count).to(equal(8))
expectation.fulfill()
}, failure: { _ in })
waitForExpectations(timeout: 5, handler: nil)
}
/// Calls the failure block when the request fails
///
func testFailure() {
let expectation = self.expectation(description: "Failure callback should be called")
let service = ReaderCardService(service: remoteService, coreDataStack: contextManager, followedInterestsService: followedInterestsService)
apiMock.succeed = false
service.fetch(isFirstPage: true, success: { _, _ in }, failure: { error in
expect(error).toNot(beNil())
expectation.fulfill()
})
waitForExpectations(timeout: 5, handler: nil)
}
/// When fetching the first page, clean all the cards
///
func testFirstPageClean() {
let expectation = self.expectation(description: "Only 10 cards should be returned")
let service = ReaderCardService(service: remoteService, coreDataStack: contextManager, followedInterestsService: followedInterestsService)
apiMock.succeed = true
service.fetch(isFirstPage: false, success: { _, _ in
// Fetch again, this time the 1st page
service.fetch(isFirstPage: true, success: { _, _ in
let cards = try? self.mainContext.fetch(NSFetchRequest(entityName: ReaderCard.classNameWithoutNamespaces())) as? [ReaderCard]
expect(cards?.count).to(equal(10))
expectation.fulfill()
}, failure: { _ in })
}, failure: {_ in })
waitForExpectations(timeout: 5, handler: nil)
}
}
class WordPressComMockRestApi: WordPressComRestApi {
var succeed = false
var GETCalledWithURL: String?
override func GET(_ URLString: String, parameters: [String: AnyObject]?, success: @escaping WordPressComRestApi.SuccessResponseBlock, failure: @escaping WordPressComRestApi.FailureReponseBlock) -> Progress? {
GETCalledWithURL = URLString
guard
let fileURL: URL = Bundle.main.url(forResource: "reader-cards-success.json", withExtension: nil),
let data: Data = try? Data(contentsOf: fileURL),
let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) as AnyObject
else {
return Progress()
}
if succeed {
success(jsonObject, nil)
} else {
failure(NSError(domain: "", code: -1, userInfo: nil), nil)
}
return Progress()
}
}
private class ReaderFollowedInterestsServiceMock: ReaderFollowedInterestsService {
var returnInterests = true
private let context: NSManagedObjectContext
lazy var topics: [ReaderTagTopic] = {
let pugTopic = ReaderTagTopic(context: context)
pugTopic.slug = "pug"
pugTopic.isRecommended = false
pugTopic.tagID = 1
pugTopic.inUse = false
pugTopic.following = true
pugTopic.path = ""
pugTopic.showInMenu = false
pugTopic.title = "Pug"
pugTopic.type = "pug"
let catTopic = ReaderTagTopic(context: context)
catTopic.slug = "cat"
catTopic.isRecommended = false
catTopic.tagID = 2
catTopic.inUse = false
catTopic.following = true
catTopic.path = ""
catTopic.showInMenu = false
catTopic.title = "Cat"
catTopic.type = "cat"
return [pugTopic, catTopic]
}()
init(context: NSManagedObjectContext) {
self.context = context
}
func fetchFollowedInterestsLocally(completion: @escaping ([ReaderTagTopic]?) -> Void) {
completion(returnInterests ? topics : [])
}
func fetchFollowedInterestsRemotely(completion: @escaping ([ReaderTagTopic]?) -> Void) {
completion(returnInterests ? topics : [])
}
func followInterests(_ interests: [RemoteReaderInterest], success: @escaping ([ReaderTagTopic]?) -> Void, failure: @escaping (Error) -> Void, isLoggedIn: Bool) {
}
func path(slug: String) -> String {
return ""
}
}