Skip to content

Commit

Permalink
Remove commented code. Fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdubey committed Jan 7, 2025
1 parent 010ae95 commit f709e1b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 41 deletions.
9 changes: 0 additions & 9 deletions Production/govuk_ios/Model/Topics/TopicDetailResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ struct TopicDetailResponse: Decodable {
let isStepByStep: Bool
let popular: Bool
let url: URL

// var activityItem: ActivityItemCreateParams {
// ActivityItemCreateParams(
// id: url.absoluteString,
// title: title,
// date: Date(),
// url: url.absoluteString
// )
// }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import GOVKit
class TestRepository: CoreDataRepositoryInterface {

private let persistentContainer: NSPersistentContainer
private let notificationCenter: NotificationCenter = .default
lazy var viewContext: NSManagedObjectContext = persistentContainer.viewContext
lazy var backgroundContext: NSManagedObjectContext = persistentContainer.newBackgroundContext()

init() {
persistentContainer = Self.arrange()
viewContext = persistentContainer.viewContext
backgroundContext = persistentContainer.newBackgroundContext()
}

static func arrange() -> NSPersistentContainer {
Expand Down Expand Up @@ -62,6 +61,31 @@ class TestRepository: CoreDataRepositoryInterface {
fatalError("Unable to load persistent stores: \(error)")
}
})
addBackgroundObserver()
addViewObserver()
return self
}

private func addViewObserver() {
notificationCenter.addObserver(
forName: .NSManagedObjectContextDidSave,
object: viewContext,
queue: .main,
using: { [weak self] notification in
self?.backgroundContext.mergeChanges(fromContextDidSave: notification)
}
)
}

private func addBackgroundObserver() {
notificationCenter.addObserver(
forName: .NSManagedObjectContextDidSave,
object: backgroundContext,
queue: .main,
using: { [weak self] notification in
self?.viewContext.mergeChanges(fromContextDidSave: notification)
}
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -100,34 +100,34 @@ struct ActivityRepositoryTests {
#expect(backgroundContextResults.count == 2)
}

// @Test
// func fetch_returnsControllerWithExpectedObjects() async throws {
// let coreData = TestRepository().load()
// let sut = ActivityRepository(
// coreData: coreData
// )
//
// let controller = sut.fetch()
// #expect(controller.fetchedObjects?.isEmpty == true)
// let mockDelegate = MockFetchedResultsDelegate(
// controller: controller
// )
// let count = await withCheckedContinuation { continuation in
//
// mockDelegate.firedAction = {
// continuation.resume(returning: controller.fetchedObjects?.count)
// }
//
// let originalId = UUID().uuidString
// let params = ActivityItemCreateParams(
// id: originalId,
// title: "title",
// date: .init(timeIntervalSince1970: 0),
// url: "test"
// )
// sut.save(params: params)
// }
// #expect(count == 1)
// mockDelegate.retainerMethod()
// }
@Test
func fetch_returnsControllerWithExpectedObjects() async throws {
let coreData = TestRepository().load()
let sut = ActivityRepository(
coreData: coreData
)

let controller = sut.fetch()
#expect(controller.fetchedObjects?.isEmpty == true)
let mockDelegate = MockFetchedResultsDelegate(
controller: controller
)
let count = await withCheckedContinuation { continuation in

mockDelegate.firedAction = {
continuation.resume(returning: controller.fetchedObjects?.count)
}

let originalId = UUID().uuidString
let params = ActivityItemCreateParams(
id: originalId,
title: "title",
date: .init(timeIntervalSince1970: 0),
url: "test"
)
sut.save(params: params)
}
#expect(count == 1)
mockDelegate.retainerMethod()
}
}

0 comments on commit f709e1b

Please sign in to comment.