Skip to content

Commit

Permalink
Use posts list API stub instead of fake implemenation in unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
crazytonyli committed Nov 7, 2023
1 parent 78c3311 commit 30507e6
Showing 1 changed file with 39 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,28 @@ import XCTest
class DashboardPostsSyncManagerTests: CoreDataTestCase {

private var blog: Blog!
private var blogID: TaggedManagedObjectID<Blog>!
private let draftStatuses: [BasePost.Status] = [.draft, .pending]
private let scheduledStatuses: [BasePost.Status] = [.scheduled]
private var postService: PostServiceMock!
private var postRepository: PostRepository!
private var blogService: BlogServiceMock!

override func setUp() {
super.setUp()
contextManager.useAsSharedInstance(untilTestFinished: self)
blog = BlogBuilder(contextManager.mainContext).build()
blog.dashboardState.postsSyncingStatuses = []
blog.dashboardState.pagesSyncingStatuses = []
postService = PostServiceMock()
override func setUp() async throws {
try await super.setUp()

let account = AccountService(coreDataStack: contextManager).createOrUpdateAccount(withUsername: "username", authToken: "token")
blogID = try await contextManager.performAndSave {
let blog = try BlogBuilder($0).withAccount(id: account).with(dotComID: 42).build()
blog.dashboardState.postsSyncingStatuses = []
blog.dashboardState.pagesSyncingStatuses = []
return TaggedManagedObjectID(blog)
}

try await mainContext.perform {
self.blog = try self.mainContext.existingObject(with: self.blogID)
}

postRepository = PostRepository(coreDataStack: contextManager)
blogService = BlogServiceMock(coreDataStack: contextManager)
}

Expand All @@ -26,101 +36,95 @@ class DashboardPostsSyncManagerTests: CoreDataTestCase {

func testSuccessfulPostsSync() {
// Given
let postsToReturn = [PostBuilder(contextManager.mainContext).build()]
postService.syncShouldSucceed = true
postService.returnSyncedPosts = postsToReturn
stubGetPostsList(type: "post", total: 50)

let manager = DashboardPostsSyncManager(postService: postService, blogService: blogService)
let manager = DashboardPostsSyncManager(postRepository: postRepository, blogService: blogService)
let listener = SyncManagerListenerMock()
manager.addListener(listener)

// When
manager.syncPosts(blog: blog, postType: .post, statuses: draftStatuses)
wait(for: [expectation(that: \.postsSyncedCalled, on: listener, willEqual: true)], timeout: 0.1)

// Then
XCTAssertTrue(listener.postsSyncedCalled)
XCTAssertTrue(listener.postsSyncSuccess ?? false)
XCTAssertEqual(listener.postsSyncBlog, blog)
XCTAssertEqual(listener.postsSyncType, .post)
XCTAssertEqual(blog.dashboardState.postsSyncingStatuses, [])
XCTAssertTrue(postService.syncPostsCalled)
XCTAssertFalse(blogService.syncAuthorsCalled)
}

func testSuccessfulPagesSync() {
// Given
let postsToReturn = [PostBuilder(contextManager.mainContext).build()]
postService.syncShouldSucceed = true
postService.returnSyncedPosts = postsToReturn
stubGetPostsList(type: "page", total: 50)

let manager = DashboardPostsSyncManager(postService: postService, blogService: blogService)
let manager = DashboardPostsSyncManager(postRepository: postRepository, blogService: blogService)
let listener = SyncManagerListenerMock()
manager.addListener(listener)

// When
manager.syncPosts(blog: blog, postType: .page, statuses: draftStatuses)
wait(for: [expectation(that: \.postsSyncedCalled, on: listener, willEqual: true)], timeout: 0.1)

// Then
XCTAssertTrue(listener.postsSyncedCalled)
XCTAssertTrue(listener.postsSyncSuccess ?? false)
XCTAssertEqual(listener.postsSyncBlog, blog)
XCTAssertEqual(listener.postsSyncType, .page)
XCTAssertEqual(blog.dashboardState.pagesSyncingStatuses, [])
XCTAssertTrue(postService.syncPostsCalled)
XCTAssertFalse(blogService.syncAuthorsCalled)
}

func testFailingPostsSync() {
// Given
postService.syncShouldSucceed = false
stubGetPostsListWithServerError()

let manager = DashboardPostsSyncManager(postService: postService, blogService: blogService)
let manager = DashboardPostsSyncManager(postRepository: postRepository, blogService: blogService)
let listener = SyncManagerListenerMock()
manager.addListener(listener)

// When
manager.syncPosts(blog: blog, postType: .post, statuses: draftStatuses)
wait(for: [expectation(that: \.postsSyncedCalled, on: listener, willEqual: true)], timeout: 0.1)

// Then
XCTAssertTrue(listener.postsSyncedCalled)
XCTAssertFalse(listener.postsSyncSuccess ?? false)
XCTAssertEqual(listener.postsSyncBlog, blog)
XCTAssertEqual(listener.postsSyncType, .post)
XCTAssertEqual(blog.dashboardState.postsSyncingStatuses, [])
XCTAssertTrue(postService.syncPostsCalled)
XCTAssertFalse(blogService.syncAuthorsCalled)
}

func testNotSyncingIfAnotherSyncinProgress() {
// Given
postService.syncShouldSucceed = false
blog.dashboardState.postsSyncingStatuses = draftStatuses

let manager = DashboardPostsSyncManager(postService: postService, blogService: blogService)
let manager = DashboardPostsSyncManager(postRepository: postRepository, blogService: blogService)
let listener = SyncManagerListenerMock()
manager.addListener(listener)

// When
manager.syncPosts(blog: blog, postType: .post, statuses: draftStatuses)

// Then
XCTAssertFalse(listener.postsSyncedCalled)
XCTAssertFalse(postService.syncPostsCalled)
XCTAssertFalse(blogService.syncAuthorsCalled)
}

func testSyncingPostsIfSomeStatusesAreNotBeingSynced() {
// Given
postService.syncShouldSucceed = false
stubGetPostsListWithServerError()
blog.dashboardState.postsSyncingStatuses = draftStatuses

let manager = DashboardPostsSyncManager(postService: postService, blogService: blogService)
let manager = DashboardPostsSyncManager(postRepository: postRepository, blogService: blogService)
let listener = SyncManagerListenerMock()
manager.addListener(listener)

// When
let toBeSynced = draftStatuses + scheduledStatuses
manager.syncPosts(blog: blog, postType: .post, statuses: toBeSynced)
wait(for: [expectation(that: \.postsSyncedCalled, on: listener, willEqual: true)], timeout: 0.1)

// Then
XCTAssertTrue(listener.postsSyncedCalled)
Expand All @@ -129,24 +133,24 @@ class DashboardPostsSyncManagerTests: CoreDataTestCase {
XCTAssertEqual(listener.postsSyncType, .post)
XCTAssertEqual(listener.statusesSynced, scheduledStatuses)
XCTAssertEqual(blog.dashboardState.postsSyncingStatuses, draftStatuses)
XCTAssertTrue(postService.syncPostsCalled)
XCTAssertFalse(blogService.syncAuthorsCalled)
}

func testSuccessfulSyncAfterAuthorsSync() {
// Given
postService.syncShouldSucceed = true
stubGetPostsList(type: "post", total: 50)
blogService.syncShouldSucceed = true
blog.userID = nil
blog.isAdmin = true

let manager = DashboardPostsSyncManager(postService: postService, blogService: blogService)
let manager = DashboardPostsSyncManager(postRepository: postRepository, blogService: blogService)
let listener = SyncManagerListenerMock()
manager.addListener(listener)


// When
manager.syncPosts(blog: blog, postType: .post, statuses: draftStatuses)
wait(for: [expectation(that: \.postsSyncedCalled, on: listener, willEqual: true)], timeout: 0.1)

// Then
XCTAssertTrue(listener.postsSyncedCalled)
Expand All @@ -155,22 +159,22 @@ class DashboardPostsSyncManagerTests: CoreDataTestCase {
XCTAssertEqual(listener.postsSyncType, .post)
XCTAssertEqual(blog.dashboardState.postsSyncingStatuses, [])
XCTAssertTrue(blogService.syncAuthorsCalled)
XCTAssertTrue(postService.syncPostsCalled)
}

func testFailingAuthorsSync() {
// Given
postService.syncShouldSucceed = true
stubGetPostsList(type: "post", total: 50)
blogService.syncShouldSucceed = false
blog.userID = nil
blog.isAdmin = true

let manager = DashboardPostsSyncManager(postService: postService, blogService: blogService)
let manager = DashboardPostsSyncManager(postRepository: postRepository, blogService: blogService)
let listener = SyncManagerListenerMock()
manager.addListener(listener)

// When
manager.syncPosts(blog: blog, postType: .post, statuses: draftStatuses)
wait(for: [expectation(that: \.postsSyncedCalled, on: listener, willEqual: true)], timeout: 0.1)

// Then
XCTAssertTrue(listener.postsSyncedCalled)
Expand All @@ -179,14 +183,13 @@ class DashboardPostsSyncManagerTests: CoreDataTestCase {
XCTAssertEqual(listener.postsSyncType, .post)
XCTAssertEqual(blog.dashboardState.postsSyncingStatuses, [])
XCTAssertTrue(blogService.syncAuthorsCalled)
XCTAssertFalse(postService.syncPostsCalled)
}

}

class SyncManagerListenerMock: DashboardPostsSyncManagerListener {
class SyncManagerListenerMock: NSObject, DashboardPostsSyncManagerListener {

var postsSyncedCalled = false
@objc dynamic var postsSyncedCalled = false
var postsSyncSuccess: Bool?
var postsSyncBlog: Blog?
var postsSyncType: DashboardPostsSyncManager.PostType?
Expand Down

0 comments on commit 30507e6

Please sign in to comment.