Skip to content

Commit 3bf2439

Browse files
committed
Added unit tests
1 parent aade4f7 commit 3bf2439

13 files changed

+774
-699
lines changed

ChatLayout.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'ChatLayout'
3-
s.version = '0.2.4'
3+
s.version = '0.2.5'
44
s.summary = 'An alternative solution to MessageKit. It uses custom UICollectionViewLayout to provide you full control over the presentation.'
55
s.swift_version = '5.2'
66

Example/ChatLayout.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
017E5EA5A707CF0065C9C060 /* Pods_ChatLayout_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 187B89468A68AF411025A59A /* Pods_ChatLayout_Example.framework */; };
11+
1BE544269CC3DC62CEB1D56E /* StateControllerInternalTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BE54A1413D0DCF3036C9508 /* StateControllerInternalTests.swift */; };
1112
1BE5486E03A837AE2F8457DA /* MockCollectionLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BE5467A07C249BDD8C03D51 /* MockCollectionLayout.swift */; };
1213
1BE54BC5C70CB9FA1260B54D /* ChatDateFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BE544BF8E723F3D86B4D774 /* ChatDateFormatter.swift */; };
1314
1BE54DBD5F84AF25128B6789 /* MockUICollectionViewUpdateItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1BE5443B3AE1C502DEC3F805 /* MockUICollectionViewUpdateItem.swift */; };
@@ -89,6 +90,7 @@
8990
1BE5443B3AE1C502DEC3F805 /* MockUICollectionViewUpdateItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MockUICollectionViewUpdateItem.swift; sourceTree = "<group>"; };
9091
1BE544BF8E723F3D86B4D774 /* ChatDateFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ChatDateFormatter.swift; sourceTree = "<group>"; };
9192
1BE5467A07C249BDD8C03D51 /* MockCollectionLayout.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MockCollectionLayout.swift; sourceTree = "<group>"; };
93+
1BE54A1413D0DCF3036C9508 /* StateControllerInternalTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StateControllerInternalTests.swift; sourceTree = "<group>"; };
9294
1E094931D1426F33B8D1D082 /* Pods-ChatLayout_Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ChatLayout_Tests.debug.xcconfig"; path = "Target Support Files/Pods-ChatLayout_Tests/Pods-ChatLayout_Tests.debug.xcconfig"; sourceTree = "<group>"; };
9395
4504F3AB24FA66F500385590 /* Constants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
9496
4504F3AD24FA66F500385590 /* ChatControllerDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ChatControllerDelegate.swift; sourceTree = "<group>"; };
@@ -454,6 +456,7 @@
454456
607FACE91AFB9204008FA782 /* Supporting Files */,
455457
1BE5467A07C249BDD8C03D51 /* MockCollectionLayout.swift */,
456458
1BE5443B3AE1C502DEC3F805 /* MockUICollectionViewUpdateItem.swift */,
459+
1BE54A1413D0DCF3036C9508 /* StateControllerInternalTests.swift */,
457460
);
458461
path = Tests;
459462
sourceTree = "<group>";
@@ -731,6 +734,7 @@
731734
607FACEC1AFB9204008FA782 /* StateControllerProcessUpdatesTests.swift in Sources */,
732735
1BE5486E03A837AE2F8457DA /* MockCollectionLayout.swift in Sources */,
733736
1BE54DBD5F84AF25128B6789 /* MockUICollectionViewUpdateItem.swift in Sources */,
737+
1BE544269CC3DC62CEB1D56E /* StateControllerInternalTests.swift in Sources */,
734738
);
735739
runOnlyForDeploymentPostprocessing = 0;
736740
};

Example/Tests/MockCollectionLayout.swift

+15-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,23 @@ class MockCollectionLayout: ChatLayoutRepresentation, ChatLayoutDelegate {
2020
lazy var delegate: ChatLayoutDelegate? = self
2121
var settings: ChatLayoutSettings = ChatLayoutSettings(estimatedItemSize: CGSize(width: 300, height: 40), interItemSpacing: 7, interSectionSpacing: 3)
2222
var viewSize: CGSize = CGSize(width: 300, height: 400)
23+
2324
lazy var visibleBounds: CGRect = CGRect(origin: .zero, size: viewSize)
24-
lazy var layoutFrame: CGRect = visibleBounds
25+
26+
var state: ModelState = .beforeUpdate
27+
28+
lazy var controller = StateController(collectionLayout: self)
29+
30+
/// Represent the rectangle where all the items are aligned.
31+
public var layoutFrame: CGRect {
32+
return CGRect(x: adjustedContentInset.left + settings.additionalInsets.left,
33+
y: adjustedContentInset.top + settings.additionalInsets.top,
34+
width: visibleBounds.width - settings.additionalInsets.left - settings.additionalInsets.right,
35+
height: controller.contentHeight(at: state) - settings.additionalInsets.top - settings.additionalInsets.bottom)
36+
}
37+
2538
let adjustedContentInset: UIEdgeInsets = .zero
39+
2640
let keepContentOffsetAtBottomOnBatchUpdates: Bool = true
2741

2842
func numberOfItems(inSection section: Int) -> Int {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// Created by Eugene Kazaev on 05/09/2020.
3+
// Copyright (c) 2020 CocoaPods. All rights reserved.
4+
//
5+
6+
@testable import ChatLayout
7+
import XCTest
8+
9+
class StateControllerInternalTests: XCTestCase {
10+
11+
func testUpdatePreferredSize() {
12+
let layout = MockCollectionLayout()
13+
layout.controller.set(layout.getPreparedSections(), at: .beforeUpdate)
14+
layout.controller.update(preferredSize: CGSize(width: 300, height: 100), for: IndexPath(item: 0, section: 0), kind: .footer, at: .beforeUpdate)
15+
layout.controller.update(preferredSize: CGSize(width: 300, height: 300), for: IndexPath(item: 0, section: 0), kind: .cell, at: .beforeUpdate)
16+
layout.controller.update(preferredSize: CGSize(width: 300, height: 100), for: IndexPath(item: 0, section: 0), kind: .header, at: .beforeUpdate)
17+
XCTAssertEqual(layout.controller.itemFrame(for: IndexPath(item: 0, section: 0), kind: .header, at: .beforeUpdate)?.size, CGSize(width: 300, height: 100))
18+
XCTAssertEqual(layout.controller.itemFrame(for: IndexPath(item: 0, section: 0), kind: .footer, at: .beforeUpdate)?.size, CGSize(width: 300, height: 100))
19+
XCTAssertEqual(layout.controller.itemFrame(for: IndexPath(item: 0, section: 0), kind: .cell, at: .beforeUpdate)?.size, CGSize(width: 300, height: 300))
20+
XCTAssertEqual(layout.controller.itemFrame(for: IndexPath(item: 1, section: 0), kind: .cell, at: .beforeUpdate)?.size, CGSize(width: 300, height: 40))
21+
}
22+
23+
func testUpdatePreferredAlignment() {
24+
let layout = MockCollectionLayout()
25+
layout.settings.additionalInsets = UIEdgeInsets(top: 0, left: 13, bottom: 0, right: 7)
26+
layout.controller.set(layout.getPreparedSections(), at: .beforeUpdate)
27+
28+
layout.controller.update(preferredSize: CGSize(width: 100, height: 100), for: IndexPath(item: 0, section: 0), kind: .footer, at: .beforeUpdate)
29+
layout.controller.update(preferredSize: CGSize(width: 100, height: 300), for: IndexPath(item: 0, section: 0), kind: .cell, at: .beforeUpdate)
30+
layout.controller.update(preferredSize: CGSize(width: 100, height: 100), for: IndexPath(item: 0, section: 0), kind: .header, at: .beforeUpdate)
31+
layout.controller.update(preferredSize: CGSize(width: 100, height: 100), for: IndexPath(item: 1, section: 0), kind: .cell, at: .beforeUpdate)
32+
33+
layout.controller.update(alignment: .leading, for: IndexPath(item: 0, section: 0), kind: .footer, at: .beforeUpdate)
34+
layout.controller.update(alignment: .trailing, for: IndexPath(item: 0, section: 0), kind: .header, at: .beforeUpdate)
35+
layout.controller.update(alignment: .center, for: IndexPath(item: 0, section: 0), kind: .cell, at: .beforeUpdate)
36+
layout.controller.update(alignment: .full, for: IndexPath(item: 1, section: 0), kind: .cell, at: .beforeUpdate)
37+
38+
XCTAssertEqual(layout.controller.itemAttributes(for: IndexPath(item: 0, section: 0), kind: .header, at: .beforeUpdate)?.alignment, .trailing)
39+
XCTAssertEqual(layout.controller.itemAttributes(for: IndexPath(item: 0, section: 0), kind: .footer, at: .beforeUpdate)?.alignment, .leading)
40+
XCTAssertEqual(layout.controller.itemAttributes(for: IndexPath(item: 0, section: 0), kind: .cell, at: .beforeUpdate)?.alignment, .center)
41+
XCTAssertEqual(layout.controller.itemAttributes(for: IndexPath(item: 1, section: 0), kind: .cell, at: .beforeUpdate)?.alignment, .full)
42+
XCTAssertEqual(layout.controller.itemAttributes(for: IndexPath(item: 2, section: 0), kind: .cell, at: .beforeUpdate)?.alignment, .full)
43+
44+
XCTAssertEqual(layout.controller.itemAttributes(for: IndexPath(item: 0, section: 0), kind: .header, at: .beforeUpdate)?.frame.origin.x, 300 - 100 - layout.settings.additionalInsets.right)
45+
XCTAssertEqual(layout.controller.itemAttributes(for: IndexPath(item: 0, section: 0), kind: .footer, at: .beforeUpdate)?.frame.origin.x, layout.settings.additionalInsets.left)
46+
XCTAssertEqual(layout.controller.itemAttributes(for: IndexPath(item: 0, section: 0), kind: .cell, at: .beforeUpdate)?.frame.origin.x, layout.settings.additionalInsets.left + (300 - layout.settings.additionalInsets.right - layout.settings.additionalInsets.left) / 2 - 100 / 2)
47+
XCTAssertEqual(layout.controller.itemAttributes(for: IndexPath(item: 1, section: 0), kind: .cell, at: .beforeUpdate)?.frame.origin.x, layout.settings.additionalInsets.left)
48+
XCTAssertEqual(layout.controller.itemAttributes(for: IndexPath(item: 1, section: 0), kind: .cell, at: .beforeUpdate)?.frame.width, 300 - layout.settings.additionalInsets.left - layout.settings.additionalInsets.right)
49+
}
50+
51+
func testItemIdentifierAtIndexPath() {
52+
let layout = MockCollectionLayout()
53+
layout.controller.set(layout.getPreparedSections(), at: .beforeUpdate)
54+
XCTAssertEqual(layout.controller.itemIdentifier(for: IndexPath(item: 0, section: 0), kind: .cell, at: .beforeUpdate), layout.controller.storage[.beforeUpdate]?.sections[0].items[0].id)
55+
XCTAssertEqual(layout.controller.itemIdentifier(for: IndexPath(item: 0, section: 0), kind: .header, at: .beforeUpdate), layout.controller.storage[.beforeUpdate]?.sections[0].header?.id)
56+
XCTAssertEqual(layout.controller.itemIdentifier(for: IndexPath(item: 0, section: 0), kind: .footer, at: .beforeUpdate), layout.controller.storage[.beforeUpdate]?.sections[0].footer?.id)
57+
}
58+
59+
func testSectionIdentifierAtIndexPath() {
60+
let layout = MockCollectionLayout()
61+
layout.controller.set(layout.getPreparedSections(), at: .beforeUpdate)
62+
XCTAssertEqual(layout.controller.sectionIdentifier(for: 0, at: .beforeUpdate), layout.controller.storage[.beforeUpdate]?.sections[0].id)
63+
XCTAssertEqual(layout.controller.sectionIdentifier(for: 1, at: .beforeUpdate), layout.controller.storage[.beforeUpdate]?.sections[1].id)
64+
}
65+
66+
}

0 commit comments

Comments
 (0)