notifications SC unit tests

This commit is contained in:
Ryan Nystrom
2017-05-13 17:51:38 -04:00
parent 720aaeafa2
commit 1d68b1be2b
7 changed files with 158 additions and 9 deletions

View File

@@ -17,10 +17,10 @@ final class NotificationCell: UICollectionViewCell {
weak var delegate: NotificationCellDelegate? = nil
fileprivate let reasonImageView = UIImageView()
fileprivate let markButton = UIButton(type: .custom)
fileprivate let dateLabel = UILabel()
fileprivate let titleLabel = UILabel()
let reasonImageView = UIImageView()
let markButton = UIButton(type: .custom)
let dateLabel = UILabel()
let titleLabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
@@ -55,6 +55,7 @@ extension NotificationCell: IGListBindable {
func bindViewModel(_ viewModel: Any) {
guard let viewModel = viewModel as? NotificationViewModel else { return }
markButton.isHidden = viewModel.read
titleLabel.text = viewModel.title
setNeedsLayout()
}

View File

@@ -11,7 +11,7 @@ import IGListKit
final class NotificationRepoCell: UICollectionViewCell {
fileprivate let label = UILabel()
let label = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)

View File

@@ -20,11 +20,19 @@ final class RepoNotificationsSectionController: IGListBindingSectionController<R
// MARK: IGListBindingSectionControllerDataSource
func sectionController(_ sectionController: IGListBindingSectionController<IGListDiffable>, viewModelsFor object: Any) -> [IGListDiffable] {
return self.object?.notifications ?? []
func sectionController(
_ sectionController: IGListBindingSectionController<IGListDiffable>,
viewModelsFor object: Any
) -> [IGListDiffable] {
guard let object = self.object else { return [] }
return [object.repoName as IGListDiffable] + object.notifications
}
func sectionController(_ sectionController: IGListBindingSectionController<IGListDiffable>, cellForViewModel viewModel: Any, at index: Int) -> UICollectionViewCell {
func sectionController(
_ sectionController: IGListBindingSectionController<IGListDiffable>,
cellForViewModel viewModel: Any,
at index: Int
) -> UICollectionViewCell {
guard let context = collectionContext else { return UICollectionViewCell() }
if viewModel is String {
return context.dequeueReusableCell(of: NotificationRepoCell.self, for: self, at: index)
@@ -33,7 +41,11 @@ final class RepoNotificationsSectionController: IGListBindingSectionController<R
}
}
func sectionController(_ sectionController: IGListBindingSectionController<IGListDiffable>, sizeForViewModel viewModel: Any, at index: Int) -> CGSize {
func sectionController(
_ sectionController: IGListBindingSectionController<IGListDiffable>,
sizeForViewModel viewModel: Any,
at index: Int
) -> CGSize {
guard let context = collectionContext else { return .zero }
let height: CGFloat
if viewModel is String {

View File

@@ -35,6 +35,18 @@
29A195111EC7AC9500C3E289 /* NotificationRepoCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29A195101EC7AC9500C3E289 /* NotificationRepoCell.swift */; };
29A195131EC7AD2D00C3E289 /* RepoNotificationsSectionController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29A195121EC7AD2D00C3E289 /* RepoNotificationsSectionController.swift */; };
29A195151EC7AD4A00C3E289 /* RepoNotifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29A195141EC7AD4A00C3E289 /* RepoNotifications.swift */; };
29A195191EC7AF6800C3E289 /* NotificationsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29A195181EC7AF6800C3E289 /* NotificationsTests.swift */; };
29C295021EC7AF8C00D46CD2 /* NotificationCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29C9FDDA1EC6627200EE3A52 /* NotificationCell.swift */; };
29C295031EC7AF8C00D46CD2 /* NotificationViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29C9FDDC1EC6628200EE3A52 /* NotificationViewModel.swift */; };
29C295041EC7AF8C00D46CD2 /* NotificationType+Icon.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29A195091EC78B4800C3E289 /* NotificationType+Icon.swift */; };
29C295051EC7AF8C00D46CD2 /* NotificationRepoCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29A195101EC7AC9500C3E289 /* NotificationRepoCell.swift */; };
29C295061EC7AF8C00D46CD2 /* NotificationType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29A1950B1EC7901400C3E289 /* NotificationType.swift */; };
29C295071EC7AF8C00D46CD2 /* RepoNotificationsSectionController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29A195121EC7AD2D00C3E289 /* RepoNotificationsSectionController.swift */; };
29C295081EC7AF8C00D46CD2 /* RepoNotifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29A195141EC7AD4A00C3E289 /* RepoNotifications.swift */; };
29C2950A1EC7AFA500D46CD2 /* ListTestKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29C295091EC7AFA500D46CD2 /* ListTestKit.swift */; };
29C2950B1EC7B16800D46CD2 /* Styles.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29C9FDE01EC667AE00EE3A52 /* Styles.swift */; };
29C2950C1EC7B16C00D46CD2 /* UIColor+Hex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29A195011EC66B8B00C3E289 /* UIColor+Hex.swift */; };
29C2950E1EC7B43B00D46CD2 /* ListKitTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29C2950D1EC7B43B00D46CD2 /* ListKitTestCase.swift */; };
29C9FDC51EC65FEE00EE3A52 /* Branch.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29C9FDB11EC65FEE00EE3A52 /* Branch.swift */; };
29C9FDC61EC65FEE00EE3A52 /* Comment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29C9FDB21EC65FEE00EE3A52 /* Comment.swift */; };
29C9FDC71EC65FEE00EE3A52 /* Commit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29C9FDB31EC65FEE00EE3A52 /* Commit.swift */; };
@@ -105,6 +117,9 @@
29A195101EC7AC9500C3E289 /* NotificationRepoCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NotificationRepoCell.swift; sourceTree = "<group>"; };
29A195121EC7AD2D00C3E289 /* RepoNotificationsSectionController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RepoNotificationsSectionController.swift; sourceTree = "<group>"; };
29A195141EC7AD4A00C3E289 /* RepoNotifications.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RepoNotifications.swift; sourceTree = "<group>"; };
29A195181EC7AF6800C3E289 /* NotificationsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NotificationsTests.swift; sourceTree = "<group>"; };
29C295091EC7AFA500D46CD2 /* ListTestKit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ListTestKit.swift; sourceTree = "<group>"; };
29C2950D1EC7B43B00D46CD2 /* ListKitTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ListKitTestCase.swift; sourceTree = "<group>"; };
29C9FDB11EC65FEE00EE3A52 /* Branch.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Branch.swift; sourceTree = "<group>"; };
29C9FDB21EC65FEE00EE3A52 /* Comment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Comment.swift; sourceTree = "<group>"; };
29C9FDB31EC65FEE00EE3A52 /* Commit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Commit.swift; sourceTree = "<group>"; };
@@ -188,6 +203,9 @@
children = (
297AE84C1EC0D58A00B44A1F /* DateDisplayTests.swift */,
297AE84E1EC0D58A00B44A1F /* Info.plist */,
29A195181EC7AF6800C3E289 /* NotificationsTests.swift */,
29C2950D1EC7B43B00D46CD2 /* ListKitTestCase.swift */,
29C295091EC7AFA500D46CD2 /* ListTestKit.swift */,
);
path = FreetimeTests;
sourceTree = "<group>";
@@ -606,8 +624,20 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
29C295021EC7AF8C00D46CD2 /* NotificationCell.swift in Sources */,
29C295041EC7AF8C00D46CD2 /* NotificationType+Icon.swift in Sources */,
29C2950E1EC7B43B00D46CD2 /* ListKitTestCase.swift in Sources */,
29C2950A1EC7AFA500D46CD2 /* ListTestKit.swift in Sources */,
29C2950B1EC7B16800D46CD2 /* Styles.swift in Sources */,
29C295061EC7AF8C00D46CD2 /* NotificationType.swift in Sources */,
29C295031EC7AF8C00D46CD2 /* NotificationViewModel.swift in Sources */,
29C295051EC7AF8C00D46CD2 /* NotificationRepoCell.swift in Sources */,
29A195051EC75F3D00C3E289 /* Date+Display.swift in Sources */,
29C295071EC7AF8C00D46CD2 /* RepoNotificationsSectionController.swift in Sources */,
29C2950C1EC7B16C00D46CD2 /* UIColor+Hex.swift in Sources */,
297AE84D1EC0D58A00B44A1F /* DateDisplayTests.swift in Sources */,
29C295081EC7AF8C00D46CD2 /* RepoNotifications.swift in Sources */,
29A195191EC7AF6800C3E289 /* NotificationsTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@@ -0,0 +1,20 @@
//
// ListKitTestCase.swift
// Freetime
//
// Created by Ryan Nystrom on 5/13/17.
// Copyright © 2017 Ryan Nystrom. All rights reserved.
//
import XCTest
class ListKitTestCase: XCTestCase {
let kit = ListTestKit()
override func setUp() {
super.setUp()
kit.setup()
}
}

View File

@@ -0,0 +1,44 @@
//
// ListTestKit.swift
// Freetime
//
// Created by Ryan Nystrom on 5/13/17.
// Copyright © 2017 Ryan Nystrom. All rights reserved.
//
import Foundation
import IGListKit
class ListTestKit: NSObject, IGListAdapterDataSource {
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 320, height: 480))
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
let adapter = IGListAdapter(updater: IGListAdapterUpdater(), viewController: nil)
var objects: [IGListDiffable] = []
var sectionControllerBlock: (Any) -> (IGListSectionController) = { _ in
return IGListSectionController()
}
func setup() {
window.addSubview(collectionView)
collectionView.frame = window.bounds
adapter.collectionView = collectionView
adapter.dataSource = self
}
// MARK: IGListAdapterDataSource
func objects(for listAdapter: IGListAdapter) -> [IGListDiffable] {
return objects
}
func listAdapter(_ listAdapter: IGListAdapter, sectionControllerFor object: Any) -> IGListSectionController {
return sectionControllerBlock(object)
}
func emptyView(for listAdapter: IGListAdapter) -> UIView? {
return nil
}
}

View File

@@ -0,0 +1,42 @@
//
// NotificationsTests.swift
// Freetime
//
// Created by Ryan Nystrom on 5/13/17.
// Copyright © 2017 Ryan Nystrom. All rights reserved.
//
import XCTest
class NotificationsTests: ListKitTestCase {
override func setUp() {
super.setUp()
kit.sectionControllerBlock = { _ in
return RepoNotificationsSectionController()
}
}
func test_withOneNotification() {
let notification = NotificationViewModel(title: "Notification 1 title", type: .issue, date: Date(), read: false)
let repo = RepoNotifications(repoName: "Repo Name", notifications: [notification])
kit.objects = [repo]
let expect = expectation(description: #function)
kit.adapter.performUpdates(animated: false) { _ in
expect.fulfill()
XCTAssertEqual(self.kit.collectionView.numberOfSections, 1)
XCTAssertEqual(self.kit.collectionView.numberOfItems(inSection: 0), 2)
let titleCell = self.kit.collectionView.cellForItem(at: IndexPath(item: 0, section: 0)) as! NotificationRepoCell
XCTAssertEqual(titleCell.label.text, "Repo Name")
let notificationCell = self.kit.collectionView.cellForItem(at: IndexPath(item: 1, section: 0)) as! NotificationCell
XCTAssertEqual(notificationCell.titleLabel.text, "Notification 1 title")
}
waitForExpectations(timeout: 10, handler: nil)
}
}