-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
CommentService+RepliesTests.swift
141 lines (111 loc) · 5.04 KB
/
CommentService+RepliesTests.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
import Foundation
import Nimble
import OHHTTPStubs
import XCTest
@testable import WordPress
final class CommentService_RepliesTests: CoreDataTestCase {
private let commentID: Int = 1
private let siteID: Int = 2
private let authorID: Int = 99
private let timeout: TimeInterval = 2
private let commentsV2SuccessFilename = "comments-v2-success.json"
private let emptyArrayFilename = "empty-array.json"
private var endpoint: String {
"sites/\(siteID)/comments"
}
private var commentService: CommentService!
private var accountService: AccountService!
override func setUp() {
super.setUp()
commentService = CommentService(managedObjectContext: mainContext)
accountService = makeAccountService()
}
override func tearDown() {
HTTPStubs.removeAllStubs()
commentService = nil
accountService = nil
super.tearDown()
}
// MARK: - Tests
func test_getReplies_givenSuccessfulResult_callsSuccessBlock() {
let expectation = expectation(description: "Fetch latest reply ID should succeed")
let expectedReplyID = 54 // from comments-v2-success.json
HTTPStubs.stubRequest(forEndpoint: endpoint, withFileAtPath: stubFilePath(commentsV2SuccessFilename))
commentService.getLatestReplyID(for: commentID, siteID: siteID, accountService: accountService) { replyID in
expect(replyID).to(equal(expectedReplyID))
expectation.fulfill()
} failure: { _ in
XCTFail("This block shouldn't get called.")
expectation.fulfill()
}
wait(for: [expectation], timeout: timeout)
}
func test_getReplies_givenEmptyResult_callsSuccessBlock() {
let expectation = expectation(description: "Fetch latest reply ID should succeed")
let expectedReplyID = 0
HTTPStubs.stubRequest(forEndpoint: endpoint, withFileAtPath: stubFilePath(emptyArrayFilename))
commentService.getLatestReplyID(for: commentID, siteID: siteID, accountService: accountService) { replyID in
expect(replyID).to(equal(expectedReplyID))
expectation.fulfill()
} failure: { _ in
XCTFail("This block shouldn't get called.")
expectation.fulfill()
}
wait(for: [expectation], timeout: timeout)
}
func test_getReplies_givenFailureResult_callsFailureBlock() {
let expectation = expectation(description: "Fetch latest reply ID should fail")
stub(condition: isMethodGET()) { _ in
return HTTPStubsResponse(data: Data(), statusCode: 500, headers: nil)
}
commentService.getLatestReplyID(for: commentID, siteID: siteID, accountService: accountService) { _ in
XCTFail("This block shouldn't get called.")
expectation.fulfill()
} failure: { _ in
expectation.fulfill()
}
wait(for: [expectation], timeout: timeout)
}
func test_getReplies_addsCommentIdInParameter() {
let (mockService, mockApi) = makeMockService()
let parentKey = CommentServiceRemoteREST.RequestKeys.parent.rawValue
mockService.getLatestReplyID(for: commentID,
siteID: siteID,
accountService: accountService,
success: { _ in },
failure: { _ in })
var parameters = [String: Any]()
expect(mockApi.parametersPassedIn).toNot(beNil())
expect { parameters = mockApi.parametersPassedIn! as! [String: Any] }.toNot(throwError())
expect(parameters[parentKey] as? Int).to(equal(commentID))
}
}
// MARK: - Test Helpers
private extension CommentService_RepliesTests {
// returns a mock service that never calls the success or failure block.
// primarily used for testing the passed in parameters – see MockWordPressComRestApi
func makeMockService() -> (CommentService, MockWordPressComRestApi) {
let mockApi = MockWordPressComRestApi()
let mockFactory = CommentServiceRemoteFactoryMock(restApi: mockApi)
return (.init(managedObjectContext: mainContext, commentServiceRemoteFactory: mockFactory), mockApi)
}
func makeAccountService() -> AccountService {
let service = AccountService(managedObjectContext: mainContext)
let account = service.createOrUpdateAccount(withUsername: "testuser", authToken: "authtoken")
account.userID = NSNumber(value: authorID)
service.setDefaultWordPressComAccount(account)
return service
}
func stubFilePath(_ filename: String) -> String {
return OHPathForFile(filename, type(of: self))!
}
}
private class CommentServiceRemoteFactoryMock: CommentServiceRemoteFactory {
var restApi: WordPressComRestApi
init(restApi: WordPressComRestApi) {
self.restApi = restApi
}
override func restRemote(siteID: NSNumber, api: WordPressComRestApi) -> CommentServiceRemoteREST {
return CommentServiceRemoteREST(wordPressComRestApi: restApi, siteID: siteID)
}
}