ActionSheetItemTests.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. @testable import Sheeeeeeeeet
  11. class ActionSheetItemTests: QuickSpec {
  12. override func spec() {
  13. func createItem(subtitle: String? = nil) -> MockActionSheetItem {
  14. return MockActionSheetItem(title: "foo", subtitle: subtitle, value: true, image: UIImage())
  15. }
  16. func createItem(_ tapBehavior: ActionSheetItem.TapBehavior) -> MockActionSheetItem {
  17. return MockActionSheetItem(title: "foo", subtitle: "bar", value: true, image: UIImage(), tapBehavior: tapBehavior)
  18. }
  19. describe("created instance") {
  20. it("applies default values") {
  21. let item = ActionSheetItem(title: "foo")
  22. expect(item.title).to(equal("foo"))
  23. expect(item.subtitle).to(beNil())
  24. expect(item.value).to(beNil())
  25. expect(item.image).to(beNil())
  26. expect(item.tapBehavior).to(equal(.dismiss))
  27. expect(item.cellStyle).to(equal(.default))
  28. }
  29. it("applies provided values") {
  30. let image = UIImage()
  31. let item = ActionSheetItem(title: "foo", subtitle: "bar", value: true, image: image, tapBehavior: .none)
  32. expect(item.title).to(equal("foo"))
  33. expect(item.subtitle).to(equal("bar"))
  34. expect(item.value as? Bool).to(equal(true))
  35. expect(item.image).to(be(image))
  36. expect(item.tapBehavior).to(equal(ActionSheetItem.TapBehavior.none))
  37. expect(item.cellStyle).to(equal(.value1))
  38. }
  39. }
  40. describe("cell reuse identifier") {
  41. it("is class name") {
  42. let item = createItem()
  43. expect(item.cellReuseIdentifier).to(equal("MockActionSheetItem"))
  44. }
  45. }
  46. describe("height") {
  47. let preset = CustomItem.height
  48. afterEach {
  49. CustomItem.height = preset
  50. }
  51. it("uses standard height if no custom value is registered") {
  52. expect(ActionSheetItem.height).to(equal(50))
  53. }
  54. it("only uses custom height for registered type") {
  55. CustomItem.height = 123
  56. expect(CustomItem.height).to(equal(123))
  57. expect(CustomItem(title: "").height).to(equal(123))
  58. expect(ActionSheetItem.height).to(equal(50))
  59. }
  60. }
  61. describe("resolving cell") {
  62. it("returns correct cell") {
  63. let item = createItem()
  64. let cell = item.cell(for: UITableView(frame: .zero))
  65. expect(cell.reuseIdentifier).to(equal(item.cellReuseIdentifier))
  66. }
  67. }
  68. }
  69. }
  70. class ActionSheetItemCellTests: QuickSpec {
  71. override func spec() {
  72. var cell: ActionSheetItemCell!
  73. var item: ActionSheetItem! {
  74. didSet { cell = item.cell(for: UITableView()) }
  75. }
  76. beforeEach {
  77. item = ActionSheetItem(title: "foo")
  78. }
  79. describe("moving to window") {
  80. it("refreshes cell") {
  81. cell.refresh(with: item)
  82. cell.textLabel?.text = ""
  83. cell.didMoveToWindow()
  84. expect(cell.textLabel?.text).to(equal("foo"))
  85. }
  86. }
  87. describe("refreshing") {
  88. it("aborts if cell has no item reference") {
  89. cell.refresh()
  90. expect(cell.textLabel?.text).to(beNil())
  91. }
  92. it("refreshes if cell has item reference") {
  93. let image = UIImage()
  94. item = ActionSheetItem(title: "foo", subtitle: "bar", value: "baz", image: image)
  95. cell.titleColor = .yellow
  96. cell.titleFont = .boldSystemFont(ofSize: 1)
  97. cell.subtitleColor = .brown
  98. cell.subtitleFont = .boldSystemFont(ofSize: 2)
  99. cell.refresh(with: item)
  100. expect(cell.imageView?.image).to(be(image))
  101. expect(cell.selectionStyle).to(equal(.default))
  102. expect(cell.textLabel?.font).toNot(beNil())
  103. expect(cell.textLabel?.font).to(be(cell.titleFont))
  104. expect(cell.textLabel?.text).to(equal("foo"))
  105. expect(cell.textLabel?.textAlignment).to(equal(.left))
  106. expect(cell.detailTextLabel?.font).toNot(beNil())
  107. expect(cell.detailTextLabel?.font).to(be(cell.subtitleFont))
  108. expect(cell.detailTextLabel?.text).to(equal("bar"))
  109. }
  110. }
  111. }
  112. }
  113. private class CustomItem: ActionSheetItem {}