Skip to content

Commit

Permalink
Improve more action sheet namespace tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Jan 15, 2019
1 parent 37d6775 commit 256fa2d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 33 deletions.
6 changes: 3 additions & 3 deletions Sheeeeeeeeet/ActionSheet/ActionSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,13 @@ open class ActionSheet: UIViewController {
}

open func refreshItems() {
items.forEach { $0.applyAppearance(appearance) }
items.forEach { $0.applyAppearance(appearance) } // TODO: Deprecated - Remove in 1.4.0
itemsTableViewHeight?.constant = itemsHeight
}

open func refreshButtons() {
buttonsTableView?.isHidden = buttons.count == 0
buttons.forEach { $0.applyAppearance(appearance) }
buttons.forEach { $0.applyAppearance(appearance) } // TODO: Deprecated - Remove in 1.4.0
buttonsTableViewHeight?.constant = buttonsHeight
}

Expand All @@ -233,7 +233,7 @@ open class ActionSheet: UIViewController {
}

open func margin(at margin: ActionSheetMargin) -> CGFloat {
let view = self.view.superview ?? self.view
let view: UIView! = self.view.superview ?? self.view
switch margin {
case .top: return margin.value(in: view, minimum: minimumContentInsets.top)
case .left: return margin.value(in: view, minimum: minimumContentInsets.left)
Expand Down
2 changes: 1 addition & 1 deletion Sheeeeeeeeet/ActionSheet/ActionSheetItemHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ extension ActionSheetItemHandler: UITableViewDataSource {
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let item = self.item(at: indexPath) else { return UITableViewCell(frame: .zero) }
let cell = item.cell(for: tableView)
item.applyAppearance(to: cell)
item.applyAppearance(to: cell) // TODO: Deprecated - Remove in 1.4.0
cell.refresh(with: item)
return cell
}
Expand Down
11 changes: 5 additions & 6 deletions Sheeeeeeeeet/ActionSheet/ActionSheetMargin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import UIKit

public enum ActionSheetMargin {

case top, left, right, bottom

func value(in view: UIView?) -> CGFloat? {
guard let view = view else { return nil }
func value(in view: UIView) -> CGFloat {
if #available(iOS 11.0, *) {
let insets = view.safeAreaInsets
switch self {
Expand All @@ -22,12 +22,11 @@ public enum ActionSheetMargin {
case .bottom: return insets.bottom
}
} else {
return nil
return 0
}
}

func value(in view: UIView?, minimum: CGFloat) -> CGFloat {
guard let value = self.value(in: view) else { return minimum }
return max(value, minimum)
func value(in view: UIView, minimum: CGFloat) -> CGFloat {
return max(value(in: view), minimum)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ActionSheet_PresenterTests: QuickSpec {

describe("default presenter") {

func getDifferentIdiom() -> UIUserInterfaceIdiom {
func getReversedIdiom() -> UIUserInterfaceIdiom {
switch UIDevice.current.userInterfaceIdiom {
case .phone: return .pad
case .pad: return .phone
Expand All @@ -26,7 +26,7 @@ class ActionSheet_PresenterTests: QuickSpec {
}

it("is different from other idioms") {
let idiom = getDifferentIdiom()
let idiom = getReversedIdiom()
let idiomPresenter = idiom.defaultPresenter
let defaultPresenter = ActionSheet.defaultPresenter
let isSameKind = type(of: defaultPresenter) == type(of: idiomPresenter)
Expand Down
32 changes: 18 additions & 14 deletions SheeeeeeeeetTests/ActionSheet/ActionSheetItemHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ActionSheetItemHandlerTests: QuickSpec {

override func spec() {

func tableView() -> MockItemTableView {
func createTableView() -> MockItemTableView {
return MockItemTableView(frame: .zero)
}

Expand All @@ -32,6 +32,7 @@ class ActionSheetItemHandlerTests: QuickSpec {
handler = ActionSheetItemHandler(actionSheet: sheet, itemType: .items)
}


describe("configured with item type") {

beforeEach {
Expand All @@ -46,6 +47,7 @@ class ActionSheetItemHandlerTests: QuickSpec {
}
}


describe("configured with button type") {

beforeEach {
Expand All @@ -59,7 +61,8 @@ class ActionSheetItemHandlerTests: QuickSpec {
}
}

describe("as table view data source") {

describe("when used as table view data source") {

it("returns correct item at index") {
let path1 = IndexPath(row: 0, section: 0)
Expand All @@ -69,54 +72,55 @@ class ActionSheetItemHandlerTests: QuickSpec {
}

it("has correct section count") {
let sections = handler.numberOfSections(in: tableView())
let sections = handler.numberOfSections(in: createTableView())
expect(sections).to(equal(1))
}

it("has correct row count") {
let rows = handler.tableView(tableView(), numberOfRowsInSection: 0)
let rows = handler.tableView(createTableView(), numberOfRowsInSection: 0)
expect(rows).to(equal(2))
}

it("returns correct cell for existing item") {
let path = IndexPath(row: 0, section: 0)
item1.cell = ActionSheetItemCell(frame: .zero)
let result = handler.tableView(tableView(), cellForRowAt: path)
let result = handler.tableView(createTableView(), cellForRowAt: path)
expect(result).to(be(item1.cell))
}

it("returns fallback cell for existing item") {
let path = IndexPath(row: 1, section: 1)
let result = handler.tableView(tableView(), cellForRowAt: path)
let result = handler.tableView(createTableView(), cellForRowAt: path)
expect(result).toNot(beNil())
}

it("returns correct height for existing item") {
let path = IndexPath(row: 0, section: 0)
MockActionSheetItem.height = 123
let result = handler.tableView(tableView(), heightForRowAt: path)
let result = handler.tableView(createTableView(), heightForRowAt: path)
expect(result).to(equal(123))
}

it("returns zero height for existing item") {
let path = IndexPath(row: 1, section: 1)
let result = handler.tableView(tableView(), heightForRowAt: path)
let result = handler.tableView(createTableView(), heightForRowAt: path)
expect(result).to(equal(0))
}
}

describe("as table view delegate") {

describe("when used as table view delegate") {

it("does not deselect row for invalid path") {
let path = IndexPath(row: 1, section: 1)
let view = tableView()
let view = createTableView()
handler.tableView(view, didSelectRowAt: path)
expect(view.deselectRowInvokeCount).to(equal(0))
}

it("deselects row for valid path") {
let path = IndexPath(row: 0, section: 0)
let view = tableView()
let view = createTableView()
handler.tableView(view, didSelectRowAt: path)
expect(view.deselectRowInvokeCount).to(equal(1))
expect(view.deselectRowInvokePaths.count).to(equal(1))
Expand All @@ -128,21 +132,21 @@ class ActionSheetItemHandlerTests: QuickSpec {
it("does not handle tap if missing action sheet") {
sheet = nil
let path = IndexPath(row: 0, section: 0)
handler.tableView(tableView(), didSelectRowAt: path)
handler.tableView(createTableView(), didSelectRowAt: path)
expect(item1.handleTapInvokeCount).to(equal(0))
}

it("handles item tap for existing action sheet") {
let path = IndexPath(row: 0, section: 0)
handler.tableView(tableView(), didSelectRowAt: path)
handler.tableView(createTableView(), didSelectRowAt: path)
expect(item1.handleTapInvokeCount).to(equal(1))
expect(item1.handleTapInvokeActionSheets.count).to(equal(1))
expect(item1.handleTapInvokeActionSheets[0]).to(be(sheet))
}

it("handles sheet item tap for existing action sheet") {
let path = IndexPath(row: 0, section: 0)
handler.tableView(tableView(), didSelectRowAt: path)
handler.tableView(createTableView(), didSelectRowAt: path)
expect(sheet.handleTapInvokeCount).to(equal(1))
expect(sheet.handleTapInvokeItems.count).to(equal(1))
expect(sheet.handleTapInvokeItems[0]).to(be(item1))
Expand Down
21 changes: 14 additions & 7 deletions SheeeeeeeeetTests/ActionSheet/ActionSheetMarginTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,23 @@ class ActionSheetMarginTests: QuickSpec {

describe("value with minimum fallback in view") {

func value(for margin: ActionSheetMargin) -> CGFloat? {
func value(for margin: ActionSheetMargin, minimum: CGFloat) -> CGFloat {
let view = UIView(frame: .zero)
return margin.value(in: view, minimum: 10)
return margin.value(in: view, minimum: minimum)
}

it("returns safe area inset value") {
expect(value(for: .top)).to(equal(10))
expect(value(for: .left)).to(equal(10))
expect(value(for: .right)).to(equal(10))
expect(value(for: .bottom)).to(equal(10))
it("returns safe area inset if it is greater than minimum value") {
expect(value(for: .top, minimum: -1)).to(equal(0))
expect(value(for: .left, minimum: -1)).to(equal(0))
expect(value(for: .right, minimum: -1)).to(equal(0))
expect(value(for: .bottom, minimum: -1)).to(equal(0))
}

it("returns minimum value if it is greater than safe area insets") {
expect(value(for: .top, minimum: 10)).to(equal(10))
expect(value(for: .left, minimum: 10)).to(equal(10))
expect(value(for: .right, minimum: 10)).to(equal(10))
expect(value(for: .bottom, minimum: 10)).to(equal(10))
}
}
}
Expand Down

0 comments on commit 256fa2d

Please sign in to comment.