ActionSheetItemTests.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // ActionSheetItemTests.swift
  3. // Sheeeeeeeeet
  4. //
  5. // Created by Daniel Saidi on 2017-11-24.
  6. // Copyright © 2017 Daniel Saidi. All rights reserved.
  7. //
  8. import Quick
  9. import Nimble
  10. import Sheeeeeeeeet
  11. class ActionSheetItemTests: QuickSpec {
  12. func prepareStandardAppearance() {
  13. let appearance = ActionSheetAppearance.standard.item
  14. appearance.backgroundColor = .red
  15. appearance.font = UIFont.systemFont(ofSize: 31)
  16. appearance.height = 314
  17. appearance.separatorInsets = UIEdgeInsets(top: 1, left: 20, bottom: 3, right: 40)
  18. appearance.textColor = .green
  19. appearance.tintColor = .blue
  20. appearance.subtitleFont = UIFont.systemFont(ofSize: 34)
  21. appearance.subtitleTextColor = .yellow
  22. }
  23. func restoreStandardAppearance() {
  24. let appearance = ActionSheetAppearance.standard.item
  25. appearance.backgroundColor = nil
  26. appearance.font = nil
  27. appearance.height = 50
  28. appearance.separatorInsets = .zero
  29. appearance.textColor = nil
  30. appearance.tintColor = nil
  31. appearance.subtitleFont = nil
  32. appearance.subtitleTextColor = nil
  33. }
  34. func compare(
  35. _ appearance1: ActionSheetItemAppearance,
  36. _ appearance2: ActionSheetItemAppearance,
  37. textColor: UIColor? = nil) -> Bool {
  38. let textColor = textColor ?? appearance2.textColor
  39. return appearance1.backgroundColor == appearance2.backgroundColor
  40. && appearance1.font == appearance2.font
  41. && appearance1.height == appearance2.height
  42. && appearance1.separatorInsets == appearance2.separatorInsets
  43. && appearance1.textColor == textColor
  44. && appearance1.tintColor == appearance2.tintColor
  45. && appearance1.subtitleFont == appearance2.subtitleFont
  46. && appearance1.subtitleTextColor == appearance2.subtitleTextColor
  47. }
  48. func compare(
  49. _ cell: UITableViewCell,
  50. item: ActionSheetItem,
  51. appearance: ActionSheetItemAppearance,
  52. textColor: UIColor? = nil,
  53. textAlignment: NSTextAlignment = .left) -> Bool {
  54. let compareColor = textColor ?? appearance.textColor
  55. return cell.imageView?.image == item.image
  56. && cell.selectionStyle == .default
  57. //&& cell.separatorInset == appearance.item.separatorInsets))
  58. && cell.tintColor == appearance.tintColor
  59. && cell.textLabel?.text == item.title
  60. && cell.textLabel?.textAlignment == textAlignment
  61. && cell.textLabel?.textColor == compareColor
  62. && cell.textLabel?.font == appearance.font
  63. && cell.detailTextLabel?.text == item.subtitle
  64. && cell.detailTextLabel?.textColor == appearance.subtitleTextColor
  65. && cell.detailTextLabel?.font == appearance.subtitleFont
  66. }
  67. override func spec() {
  68. func createItem(subtitle: String? = nil) -> MockActionSheetItem {
  69. return MockActionSheetItem(title: "foo", subtitle: subtitle, value: true, image: UIImage())
  70. }
  71. func createItem(_ tapBehavior: ActionSheetItem.TapBehavior) -> MockActionSheetItem {
  72. return MockActionSheetItem(title: "foo", subtitle: "bar", value: true, image: UIImage(), tapBehavior: tapBehavior)
  73. }
  74. beforeEach {
  75. self.prepareStandardAppearance()
  76. }
  77. afterEach {
  78. self.restoreStandardAppearance()
  79. }
  80. describe("when created") {
  81. it("applies provided values") {
  82. let item = createItem(.none)
  83. expect(item.title).to(equal("foo"))
  84. expect(item.subtitle).to(equal("bar"))
  85. expect(item.value as? Bool).to(equal(true))
  86. expect(item.image).toNot(beNil())
  87. expect(item.tapBehavior).to(equal(ActionSheetItem.TapBehavior.none))
  88. }
  89. it("uses dismiss tap behavior by default") {
  90. let item = createItem()
  91. expect(item.tapBehavior).to(equal(ActionSheetItem.TapBehavior.dismiss))
  92. }
  93. it("copies standard item appearance initially") {
  94. let item = createItem()
  95. let standard = ActionSheetAppearance.standard.item
  96. let isEqual = self.compare(item.appearance, standard)
  97. expect(isEqual).to(beTrue())
  98. }
  99. }
  100. describe("cell reuse identifier") {
  101. it("is class name") {
  102. let item = createItem()
  103. expect(item.cellReuseIdentifier).to(equal("MockActionSheetItem"))
  104. }
  105. }
  106. describe("cell style") {
  107. it("is default if no subtitle is set") {
  108. let item = createItem(subtitle: nil)
  109. expect(item.cellStyle).to(equal(.default))
  110. }
  111. it("is value1 if subtitle is set") {
  112. let item = createItem(subtitle: "bar")
  113. expect(item.cellStyle).to(equal(.value1))
  114. }
  115. }
  116. describe("custom appearance") {
  117. it("is nil by default") {
  118. let item = createItem()
  119. expect(item.customAppearance).to(beNil())
  120. }
  121. }
  122. describe("applying appearance") {
  123. it("applies standard copy if no custom appearance is set") {
  124. let item = createItem()
  125. item.applyAppearance(ActionSheetAppearance.standard)
  126. expect(self.compare(item.appearance, ActionSheetAppearance.standard.item)).to(beTrue())
  127. }
  128. it("applies custom appearance if set") {
  129. let item = createItem()
  130. let standard = ActionSheetAppearance.standard
  131. let custom = ActionSheetAppearance(copy: standard)
  132. custom.item.backgroundColor = .yellow
  133. item.customAppearance = custom.item
  134. item.applyAppearance(standard)
  135. expect(item.appearance).to(be(custom.item))
  136. }
  137. }
  138. describe("applying appearance to cell") {
  139. it("applies correct style") {
  140. let item = createItem()
  141. let appearance = ActionSheetAppearance.standard
  142. let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "Cell")
  143. item.applyAppearance(appearance)
  144. item.applyAppearance(to: cell)
  145. expect(self.compare(cell, item: item, appearance: appearance.item)).to(beTrue())
  146. }
  147. }
  148. describe("resolving cell") {
  149. func tableView() -> UITableView {
  150. return UITableView(frame: .zero)
  151. }
  152. it("always returns a cell even if table view fails to dequeue") {
  153. let item = createItem()
  154. let cell = item.cell(for: tableView())
  155. expect(cell).toNot(beNil())
  156. }
  157. it("applies appearance to cell") {
  158. let item = createItem()
  159. let cell = item.cell(for: tableView())
  160. expect(item.applyAppearanceInvokeCount).to(equal(1))
  161. expect(item.applyAppearanceInvokeCells.count).to(equal(1))
  162. expect(item.applyAppearanceInvokeCells[0]).to(be(cell))
  163. }
  164. }
  165. }
  166. }