ActionSheetItemHandlerTests.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // ActionSheetItemHandlerTests.swift
  3. // SheeeeeeeeetTests
  4. //
  5. // Created by Daniel Saidi on 2018-10-17.
  6. // Copyright © 2018 Daniel Saidi. All rights reserved.
  7. //
  8. import Quick
  9. import Nimble
  10. @testable import Sheeeeeeeeet
  11. class ActionSheetItemHandlerTests: QuickSpec {
  12. override func spec() {
  13. func createTableView() -> MockItemTableView {
  14. return MockItemTableView(frame: .zero)
  15. }
  16. var sheet: MockActionSheet!
  17. var handler: ActionSheetItemHandler!
  18. var item1: MockActionSheetItem!
  19. var item2: MockActionSheetButton!
  20. var item3: MockActionSheetItem!
  21. beforeEach {
  22. item1 = MockActionSheetItem(title: "1")
  23. item2 = MockActionSheetButton(title: "2", value: true)
  24. item3 = MockActionSheetItem(title: "3")
  25. sheet = MockActionSheet(items: [item1, item2, item3]) { _, _ in }
  26. handler = ActionSheetItemHandler(actionSheet: sheet, itemType: .items)
  27. }
  28. describe("configured with item type") {
  29. beforeEach {
  30. handler = ActionSheetItemHandler(actionSheet: sheet, itemType: .items)
  31. }
  32. it("uses action sheet items") {
  33. let items = handler.items
  34. expect(items.count).to(equal(2))
  35. expect(items[0].title).to(equal("1"))
  36. expect(items[1].title).to(equal("3"))
  37. }
  38. }
  39. describe("configured with button type") {
  40. beforeEach {
  41. handler = ActionSheetItemHandler(actionSheet: sheet, itemType: .buttons)
  42. }
  43. it("uses action sheet buttons") {
  44. let items = handler.items
  45. expect(items.count).to(equal(1))
  46. expect(items[0].title).to(equal("2"))
  47. }
  48. }
  49. describe("when used as table view data source") {
  50. it("returns correct item at index") {
  51. let path1 = IndexPath(row: 0, section: 0)
  52. let path2 = IndexPath(row: 1, section: 0)
  53. expect(handler.item(at: path1)!.title).to(equal("1"))
  54. expect(handler.item(at: path2)!.title).to(equal("3"))
  55. }
  56. it("has correct section count") {
  57. let sections = handler.numberOfSections(in: createTableView())
  58. expect(sections).to(equal(1))
  59. }
  60. it("has correct row count") {
  61. let rows = handler.tableView(createTableView(), numberOfRowsInSection: 0)
  62. expect(rows).to(equal(2))
  63. }
  64. it("returns correct cell for existing item") {
  65. let path = IndexPath(row: 0, section: 0)
  66. item1.cell = ActionSheetItemCell(frame: .zero)
  67. let result = handler.tableView(createTableView(), cellForRowAt: path)
  68. expect(result).to(be(item1.cell))
  69. }
  70. it("returns fallback cell for existing item") {
  71. let path = IndexPath(row: 1, section: 1)
  72. let result = handler.tableView(createTableView(), cellForRowAt: path)
  73. expect(result).toNot(beNil())
  74. }
  75. it("returns correct height for existing item") {
  76. let path = IndexPath(row: 0, section: 0)
  77. MockActionSheetItem.height = 123
  78. let result = handler.tableView(createTableView(), heightForRowAt: path)
  79. expect(result).to(equal(123))
  80. }
  81. it("returns zero height for existing item") {
  82. let path = IndexPath(row: 1, section: 1)
  83. let result = handler.tableView(createTableView(), heightForRowAt: path)
  84. expect(result).to(equal(0))
  85. }
  86. }
  87. describe("when used as table view delegate") {
  88. it("does not deselect row for invalid path") {
  89. let path = IndexPath(row: 1, section: 1)
  90. let view = createTableView()
  91. handler.tableView(view, didSelectRowAt: path)
  92. expect(view.deselectRowInvokeCount).to(equal(0))
  93. }
  94. it("deselects row for valid path") {
  95. let path = IndexPath(row: 0, section: 0)
  96. let view = createTableView()
  97. handler.tableView(view, didSelectRowAt: path)
  98. expect(view.deselectRowInvokeCount).to(equal(1))
  99. expect(view.deselectRowInvokePaths.count).to(equal(1))
  100. expect(view.deselectRowInvokePaths[0]).to(equal(path))
  101. expect(view.deselectRowInvokeAnimated.count).to(equal(1))
  102. expect(view.deselectRowInvokeAnimated[0]).to(beTrue())
  103. }
  104. it("does not handle tap if missing action sheet") {
  105. sheet = nil
  106. let path = IndexPath(row: 0, section: 0)
  107. handler.tableView(createTableView(), didSelectRowAt: path)
  108. expect(item1.handleTapInvokeCount).to(equal(0))
  109. }
  110. it("handles item tap for existing action sheet") {
  111. let path = IndexPath(row: 0, section: 0)
  112. handler.tableView(createTableView(), didSelectRowAt: path)
  113. expect(item1.handleTapInvokeCount).to(equal(1))
  114. expect(item1.handleTapInvokeActionSheets.count).to(equal(1))
  115. expect(item1.handleTapInvokeActionSheets[0]).to(be(sheet))
  116. }
  117. it("handles sheet item tap for existing action sheet") {
  118. let path = IndexPath(row: 0, section: 0)
  119. handler.tableView(createTableView(), didSelectRowAt: path)
  120. expect(sheet.handleTapInvokeCount).to(equal(1))
  121. expect(sheet.handleTapInvokeItems.count).to(equal(1))
  122. expect(sheet.handleTapInvokeItems[0]).to(be(item1))
  123. }
  124. }
  125. }
  126. }