Skip to content

Commit 41003b9

Browse files
committed
Add tests for BloggingPromptsService
1 parent 362e4dd commit 41003b9

File tree

3 files changed

+133
-14
lines changed

3 files changed

+133
-14
lines changed

WordPress/WordPress.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4568,6 +4568,7 @@
45684568
FE003F5E282D61BA006F8D1D /* BloggingPrompt+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = FE003F5B282D61B9006F8D1D /* BloggingPrompt+CoreDataClass.swift */; };
45694569
FE003F5F282D61BA006F8D1D /* BloggingPrompt+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = FE003F5C282D61B9006F8D1D /* BloggingPrompt+CoreDataProperties.swift */; };
45704570
FE003F60282D61BA006F8D1D /* BloggingPrompt+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = FE003F5C282D61B9006F8D1D /* BloggingPrompt+CoreDataProperties.swift */; };
4571+
FE003F62282E73E6006F8D1D /* blogging-prompts-fetch-success.json in Resources */ = {isa = PBXBuildFile; fileRef = FE003F61282E73E6006F8D1D /* blogging-prompts-fetch-success.json */; };
45714572
FE02F95F269DC14A00752A44 /* Comment+Interface.swift in Sources */ = {isa = PBXBuildFile; fileRef = FE02F95E269DC14A00752A44 /* Comment+Interface.swift */; };
45724573
FE06AC8326C3BD0900B69DE4 /* ShareAppContentPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = FE06AC8226C3BD0900B69DE4 /* ShareAppContentPresenter.swift */; };
45734574
FE06AC8526C3C2F800B69DE4 /* ShareAppTextActivityItemSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = FE06AC8426C3C2F800B69DE4 /* ShareAppTextActivityItemSource.swift */; };
@@ -7848,6 +7849,7 @@
78487849
FE003F54282D48E4006F8D1D /* WordPress 140.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = "WordPress 140.xcdatamodel"; sourceTree = "<group>"; };
78497850
FE003F5B282D61B9006F8D1D /* BloggingPrompt+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = "BloggingPrompt+CoreDataClass.swift"; path = "/Users/dvdchr/code/wp-ios/WordPress/Classes/Models/BloggingPrompt+CoreDataClass.swift"; sourceTree = "<absolute>"; };
78507851
FE003F5C282D61B9006F8D1D /* BloggingPrompt+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = "BloggingPrompt+CoreDataProperties.swift"; path = "/Users/dvdchr/code/wp-ios/WordPress/Classes/Models/BloggingPrompt+CoreDataProperties.swift"; sourceTree = "<absolute>"; };
7852+
FE003F61282E73E6006F8D1D /* blogging-prompts-fetch-success.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "blogging-prompts-fetch-success.json"; sourceTree = "<group>"; };
78517853
FE02F95E269DC14A00752A44 /* Comment+Interface.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Comment+Interface.swift"; sourceTree = "<group>"; };
78527854
FE06AC8226C3BD0900B69DE4 /* ShareAppContentPresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareAppContentPresenter.swift; sourceTree = "<group>"; };
78537855
FE06AC8426C3C2F800B69DE4 /* ShareAppTextActivityItemSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareAppTextActivityItemSource.swift; sourceTree = "<group>"; };
@@ -14374,6 +14376,7 @@
1437414376
855408871A6F106800DDBD79 /* app-review-prompt-notifications-disabled.json */,
1437514377
F127FFD724213B5600B9D41A /* atomic-get-authentication-cookie-success.json */,
1437614378
93CD939219099BE70049096E /* authtoken.json */,
14379+
FE003F61282E73E6006F8D1D /* blogging-prompts-fetch-success.json */,
1437714380
FEFC0F8D27313DCF001F7F1D /* comments-v2-success.json */,
1437814381
FEFC0F8F27315634001F7F1D /* empty-array.json */,
1437914382
74585B9A1F0D591D00E7E667 /* domain-service-valid-domains.json */,
@@ -16444,6 +16447,7 @@
1644416447
B5AEEC7D1ACACFDA008BF2A4 /* notifications-replied-comment.json in Resources */,
1644516448
8B2D4F5327ECE089009B085C /* dashboard-200-without-posts.json in Resources */,
1644616449
FEFC0F8E27313DD0001F7F1D /* comments-v2-success.json in Resources */,
16450+
FE003F62282E73E6006F8D1D /* blogging-prompts-fetch-success.json in Resources */,
1644716451
46CFA7BF262745F70077BAD9 /* get_wp_v2_themes_twentytwentyone.json in Resources */,
1644816452
465F89F7263B690C00F4C950 /* wp-block-editor-v1-settings-success-NotThemeJSON.json in Resources */,
1644916453
3211055C250C027D0048446F /* valid-gif-header.gif in Resources */,

WordPress/WordPressTest/BloggingPromptsServiceTests.swift

Lines changed: 99 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import XCTest
2+
import OHHTTPStubs
23

34
@testable import WordPress
45

56
final class BloggingPromptsServiceTests: XCTestCase {
67
private let siteID = 1
78
private let timeout: TimeInterval = 2
8-
private var endpoint: String {
9-
"sites/\(siteID)/blogging-prompts"
10-
}
119

12-
private var context: NSManagedObjectContext!
10+
private static var dateFormatter: DateFormatter = {
11+
let formatter = DateFormatter()
12+
formatter.dateFormat = "yyyy-MM-dd"
13+
formatter.locale = .init(identifier: "en_US_POSIX")
14+
formatter.timeZone = .init(secondsFromGMT: 0)
15+
16+
return formatter
17+
}()
18+
19+
private var contextManager: TestContextManager!
1320
private var remote: BloggingPromptsServiceRemoteMock!
1421
private var service: BloggingPromptsService!
1522
private var blog: Blog!
@@ -18,18 +25,18 @@ final class BloggingPromptsServiceTests: XCTestCase {
1825
override func setUp() {
1926
super.setUp()
2027

21-
context = TestContextManager().mainContext
28+
contextManager = TestContextManager()
2229
remote = BloggingPromptsServiceRemoteMock()
2330
blog = makeBlog()
2431
accountService = makeAccountService()
25-
service = BloggingPromptsService(context: context, remote: remote, blog: blog)
32+
service = BloggingPromptsService(contextManager: contextManager, remote: remote, blog: blog)
2633
}
2734

2835
override func tearDown() {
29-
context.reset()
3036
ContextManager.overrideSharedInstance(nil)
37+
HTTPStubs.removeAllStubs()
3138

32-
context = nil
39+
contextManager = nil
3340
remote = nil
3441
blog = nil
3542
accountService = nil
@@ -40,12 +47,82 @@ final class BloggingPromptsServiceTests: XCTestCase {
4047
// MARK: - Tests
4148

4249
func test_fetchPrompts_givenSuccessfulResult_callsSuccessBlock() {
50+
// use actual remote object so the request can be intercepted by HTTPStubs.
51+
service = BloggingPromptsService(contextManager: contextManager, blog: blog)
52+
stubFetchPromptsResponse()
53+
4354
let expectation = expectation(description: "Fetch prompts should succeed")
55+
service.fetchPrompts(from: .init(timeIntervalSince1970: 0)) { prompts in
56+
XCTAssertEqual(prompts.count, 2)
57+
58+
// Verify mappings for the first prompt
59+
let firstPrompt = prompts.first!
60+
XCTAssertEqual(firstPrompt.promptID, 239)
61+
XCTAssertEqual(firstPrompt.text, "Was there a toy or thing you always wanted as a child, during the holidays or on your birthday, but never received? Tell us about it.")
62+
XCTAssertEqual(firstPrompt.title, "Prompt number 1")
63+
XCTAssertEqual(firstPrompt.content, "<!-- wp:pullquote -->\n<figure class=\"wp-block-pullquote\"><blockquote><p>Was there a toy or thing you always wanted as a child, during the holidays or on your birthday, but never received? Tell us about it.</p><cite>(courtesy of plinky.com)</cite></blockquote></figure>\n<!-- /wp:pullquote -->")
64+
XCTAssertEqual(firstPrompt.attribution, "dayone")
65+
66+
let firstDateComponents = firstPrompt.date.dateAndTimeComponents()
67+
XCTAssertEqual(firstDateComponents.year!, 2022)
68+
XCTAssertEqual(firstDateComponents.month!, 5)
69+
XCTAssertEqual(firstDateComponents.day!, 3)
70+
71+
XCTAssertFalse(firstPrompt.answered)
72+
XCTAssertEqual(firstPrompt.answerCount, 0)
73+
XCTAssertNotNil(firstPrompt.displayAvatarURLs)
74+
XCTAssertTrue(firstPrompt.displayAvatarURLs!.isEmpty)
75+
76+
// Verify mappings for the second prompt
77+
let secondPrompt = prompts.last!
78+
XCTAssertEqual(secondPrompt.promptID, 248)
79+
XCTAssertEqual(secondPrompt.text, "Tell us about a time when you felt out of place.")
80+
XCTAssertEqual(secondPrompt.title, "Prompt number 10")
81+
XCTAssertEqual(secondPrompt.content, "<!-- wp:pullquote -->\n<figure class=\"wp-block-pullquote\"><blockquote><p>Tell us about a time when you felt out of place.</p><cite>(courtesy of plinky.com)</cite></blockquote></figure>\n<!-- /wp:pullquote -->")
82+
XCTAssertTrue(secondPrompt.attribution.isEmpty)
83+
84+
let secondDateComponents = secondPrompt.date.dateAndTimeComponents()
85+
XCTAssertEqual(secondDateComponents.year!, 2021)
86+
XCTAssertEqual(secondDateComponents.month!, 9)
87+
XCTAssertEqual(secondDateComponents.day!, 12)
88+
89+
XCTAssertTrue(secondPrompt.answered)
90+
XCTAssertEqual(secondPrompt.answerCount, 1)
91+
XCTAssertNotNil(secondPrompt.displayAvatarURLs)
92+
XCTAssertEqual(secondPrompt.displayAvatarURLs!.count, 1)
4493

45-
service.fetchPrompts { _ in
46-
// TODO: Add mapping tests once CoreData model is added.
4794
expectation.fulfill()
48-
} failure: { _ in
95+
96+
} failure: { error in
97+
XCTFail("This closure shouldn't be called.")
98+
expectation.fulfill()
99+
}
100+
101+
wait(for: [expectation], timeout: timeout)
102+
}
103+
104+
func test_fetchPrompts_shouldExcludePromptsOutsideGivenDate() {
105+
// this should exclude the second prompt dated 2021-09-12.
106+
let dateParam = Self.dateFormatter.date(from: "2022-01-01")
107+
108+
// use actual remote object so the request can be intercepted by HTTPStubs.
109+
service = BloggingPromptsService(contextManager: contextManager, blog: blog)
110+
stubFetchPromptsResponse()
111+
112+
let expectation = expectation(description: "Fetch prompts should succeed")
113+
service.fetchPrompts(from: dateParam) { prompts in
114+
XCTAssertEqual(prompts.count, 1)
115+
116+
// Ensure that the date returned is more recent than the supplied date parameter.
117+
let firstPrompt = prompts.first!
118+
let firstDateComponents = firstPrompt.date.dateAndTimeComponents()
119+
XCTAssertEqual(firstDateComponents.year!, 2022)
120+
XCTAssertEqual(firstDateComponents.month!, 5)
121+
XCTAssertEqual(firstDateComponents.day!, 3)
122+
123+
expectation.fulfill()
124+
125+
} failure: { error in
49126
XCTFail("This closure shouldn't be called.")
50127
expectation.fulfill()
51128
}
@@ -105,7 +182,7 @@ final class BloggingPromptsServiceTests: XCTestCase {
105182

106183
private extension BloggingPromptsServiceTests {
107184
func makeAccountService() -> AccountService {
108-
let service = AccountService(managedObjectContext: context)
185+
let service = AccountService(managedObjectContext: contextManager.mainContext)
109186
let account = service.createOrUpdateAccount(withUsername: "testuser", authToken: "authtoken")
110187
account.userID = NSNumber(value: 1)
111188
service.setDefaultWordPressComAccount(account)
@@ -114,7 +191,14 @@ private extension BloggingPromptsServiceTests {
114191
}
115192

116193
func makeBlog() -> Blog {
117-
return BlogBuilder(context).isHostedAtWPcom().build()
194+
return BlogBuilder(contextManager.mainContext).isHostedAtWPcom().build()
195+
}
196+
197+
func stubFetchPromptsResponse() {
198+
stub(condition: isMethodGET()) { _ in
199+
let stubPath = OHPathForFile("blogging-prompts-fetch-success.json", type(of: self))
200+
return fixture(filePath: stubPath!, headers: ["Content-Type": "application/json"])
201+
}
118202
}
119203
}
120204

@@ -123,6 +207,7 @@ class BloggingPromptsServiceRemoteMock: BloggingPromptsServiceRemote {
123207
var passedNumberParameter: Int? = nil
124208
var passedDateParameter: Date? = nil
125209
var shouldReturnSuccess: Bool = true
210+
var promptsToReturn = [RemoteBloggingPrompt]()
126211

127212
override func fetchPrompts(for siteID: NSNumber,
128213
number: Int? = nil,
@@ -133,7 +218,7 @@ class BloggingPromptsServiceRemoteMock: BloggingPromptsServiceRemote {
133218
passedDateParameter = fromDate
134219

135220
if shouldReturnSuccess {
136-
completion(.success([]))
221+
completion(.success(promptsToReturn))
137222
} else {
138223
completion(.failure(Errors.failed))
139224
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"prompts": [
3+
{
4+
"id": 239,
5+
"text": "Was there a toy or thing you always wanted as a child, during the holidays or on your birthday, but never received? Tell us about it.",
6+
"title": "Prompt number 1",
7+
"content": "<!-- wp:pullquote -->\n<figure class=\"wp-block-pullquote\"><blockquote><p>Was there a toy or thing you always wanted as a child, during the holidays or on your birthday, but never received? Tell us about it.</p><cite>(courtesy of plinky.com)</cite></blockquote></figure>\n<!-- /wp:pullquote -->",
8+
"attribution": "dayone",
9+
"date": "2022-05-03",
10+
"answered": false,
11+
"answered_users_count": 0,
12+
"answered_users_sample": []
13+
},
14+
{
15+
"id": 248,
16+
"text": "Tell us about a time when you felt out of place.",
17+
"title": "Prompt number 10",
18+
"content": "<!-- wp:pullquote -->\n<figure class=\"wp-block-pullquote\"><blockquote><p>Tell us about a time when you felt out of place.</p><cite>(courtesy of plinky.com)</cite></blockquote></figure>\n<!-- /wp:pullquote -->",
19+
"attribution": "",
20+
"date": "2021-09-12",
21+
"answered": true,
22+
"answered_users_count": 1,
23+
"answered_users_sample": [
24+
{
25+
"avatar": "https://0.gravatar.com/avatar/example?s=96&d=identicon&r=G"
26+
}
27+
]
28+
}
29+
]
30+
}

0 commit comments

Comments
 (0)