-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
SiteAddressServiceTests.swift
123 lines (93 loc) · 4.9 KB
/
SiteAddressServiceTests.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
import XCTest
import Nimble
@testable import WordPress
class SiteAddressServiceTests: CoreDataTestCase {
var remoteApi: MockWordPressComRestApi!
var service: DomainsServiceAdapter!
var mockedResponse: Any!
override func setUpWithError() throws {
remoteApi = MockWordPressComRestApi()
service = DomainsServiceAdapter(managedObjectContext: mainContext, api: remoteApi)
let json = Bundle(for: SiteSegmentTests.self).url(forResource: "domain-suggestions", withExtension: "json")!
let data = try Data(contentsOf: json)
mockedResponse = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
}
func testSuggestionsWithMatchingTermSuccess() {
let searchTerm = "domaintesting"
let waitExpectation = expectation(description: "Domains should be successfully fetched")
service.addresses(for: searchTerm) { (results) in
switch results {
case .success(let fetchedResults):
self.resultsAreSorted(fetchedResults, forQuery: searchTerm, expectMatch: true)
case .failure:
fail("This is using a mocked endpoint so there is a test error")
}
waitExpectation.fulfill()
}
expect(self.remoteApi.getMethodCalled).to(beTrue())
// Respond with mobile editor not yet set on the server
remoteApi.successBlockPassedIn!(mockedResponse as AnyObject, HTTPURLResponse())
expect(self.remoteApi.URLStringPassedIn!).to(equal("rest/v1.1/domains/suggestions"))
let parameters = remoteApi.parametersPassedIn as! [String: AnyObject]
expect(parameters["query"] as? String).to(equal(searchTerm))
expect(parameters["quantity"] as? Int).toNot(beNil())
waitForExpectations(timeout: 0.1)
}
func testSuggestionsWithOutMatchingTermSuccess() {
let searchTerm = "notIncludedResult"
let waitExpectation = expectation(description: "Domains should be successfully fetched")
service.addresses(for: searchTerm) { (results) in
switch results {
case .success(let fetchedResults):
self.resultsAreSorted(fetchedResults, forQuery: searchTerm, expectMatch: false)
case .failure:
fail("This is using a mocked endpoint so there is a test error")
}
waitExpectation.fulfill()
}
expect(self.remoteApi.getMethodCalled).to(beTrue())
// Respond with mobile editor not yet set on the server
remoteApi.successBlockPassedIn!(mockedResponse as AnyObject, HTTPURLResponse())
expect(self.remoteApi.URLStringPassedIn!).to(equal("rest/v1.1/domains/suggestions"))
let parameters = remoteApi.parametersPassedIn as! [String: AnyObject]
expect(parameters["query"] as? String).to(equal(searchTerm))
expect(parameters["quantity"] as? Int).to(equal(20))
waitForExpectations(timeout: 0.1)
}
func testSuggestionsBySegmentSuccess() {
let searchTerm = "domaintesting"
let waitExpectation = expectation(description: "Domains should be successfully fetched")
service.addresses(for: searchTerm, segmentID: 2) { (results) in
switch results {
case .success(let fetchedResults):
self.resultsAreSorted(fetchedResults, forQuery: searchTerm, expectMatch: true)
case .failure:
fail("This is using a mocked endpoint so there is a test error")
}
waitExpectation.fulfill()
}
expect(self.remoteApi.getMethodCalled).to(beTrue())
// Respond with mobile editor not yet set on the server
remoteApi.successBlockPassedIn!(mockedResponse as AnyObject, HTTPURLResponse())
expect(self.remoteApi.URLStringPassedIn!).to(equal("rest/v1.1/domains/suggestions"))
let parameters = remoteApi.parametersPassedIn as! [String: AnyObject]
expect(parameters["query"] as? String).to(equal(searchTerm))
expect(parameters["quantity"] as? Int).toNot(beNil())
expect(parameters["segment_id"] as? Int).toNot(beNil())
waitForExpectations(timeout: 0.1)
}
// Helpers
func resultsAreSorted(_ results: SiteAddressServiceResult, forQuery query: String, expectMatch: Bool) {
let suggestions = results.domainSuggestions
expect(results.hasExactMatch).to(equal(expectMatch))
if results.hasExactMatch {
expect(suggestions[0].domainName.contains(query)).to(beTrue())
}
let domainNames = suggestions.compactMap { (suggestion) -> String? in
guard !suggestion.domainName.contains(query) else { return nil } //Filter out exact matches
return suggestion.domainName
}
let sortedDomainNames = domainNames.sorted()
expect(sortedDomainNames).to(equal(domainNames)) // Expect the results after sorting to be the same as the results before sorting
}
}