Skip to content

Commit

Permalink
Rewrite item tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Jan 15, 2019
1 parent c5e8ccd commit 1800bf5
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 153 deletions.
35 changes: 18 additions & 17 deletions Sheeeeeeeeet/Items/Items/ActionSheetCollectionItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,31 @@
action sheet items. If you look at `cell(for: ...)`, you'll
see that it uses `ActionSheetCollectionItemCell` for its id.

TODO: Unit test

*/

import Foundation

open class ActionSheetCollectionItem<T: ActionSheetCollectionItemContentCell>: ActionSheetItem, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {


// MARK: - Deprecated - Remove in 1.4.0 ****************
@available(*, deprecated, message: "applyAppearance will be removed in 1.4.0. Use the new appearance model instead.")
open override func applyAppearance(_ appearance: ActionSheetAppearance) {
super.applyAppearance(appearance)
self.appearance = ActionSheetCollectionItemAppearance(copy: appearance.collectionItem)
self.appearance.height = T.defaultSize.height + T.topInset + T.bottomInset + 0.5
}
@available(*, deprecated, message: "applyAppearance(to:) will be removed in 1.4.0. Use the new appearance model instead.")
open override func applyAppearance(to cell: UITableViewCell) {
super.applyAppearance(to: cell)
guard let itemCell = cell as? ActionSheetCollectionItemCell else { return }
itemCell.setup(withNib: T.nib, owner: self)
}
// MARK: - Deprecated - Remove in 1.4.0 ****************


// MARK: - Initialization

public init(
Expand Down Expand Up @@ -53,23 +71,6 @@ open class ActionSheetCollectionItem<T: ActionSheetCollectionItemContentCell>: A
public let setupAction: CellAction


// MARK: - Deprecated

@available(*, deprecated, message: "applyAppearance will be removed in 1.4.0. Use the new appearance model instead.")
open override func applyAppearance(_ appearance: ActionSheetAppearance) {
super.applyAppearance(appearance)
self.appearance = ActionSheetCollectionItemAppearance(copy: appearance.collectionItem)
self.appearance.height = T.defaultSize.height + T.topInset + T.bottomInset + 0.5
}

@available(*, deprecated, message: "applyAppearance(to:) will be removed in 1.4.0. Use the new appearance model instead.")
open override func applyAppearance(to cell: UITableViewCell) {
super.applyAppearance(to: cell)
guard let itemCell = cell as? ActionSheetCollectionItemCell else { return }
itemCell.setup(withNib: T.nib, owner: self)
}


// MARK: - Functions

open override func cell(for tableView: UITableView) -> ActionSheetItemCell {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ open class ActionSheetMultiSelectToggleItem: ActionSheetItem {
open func updateState(for actionSheet: ActionSheet) {
let selectItems = actionSheet.items.compactMap { $0 as? ActionSheetMultiSelectItem }
let items = selectItems.filter { $0.group == group }
guard items.count > 0 else { return state = .selectAll }
state = items.contains { !$0.isSelected } ? .selectAll : .deselectAll
}
}
Expand Down
34 changes: 24 additions & 10 deletions SheeeeeeeeetTests/Items/Items/ActionSheetLinkItemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,35 @@ class ActionSheetLinkItemTests: QuickSpec {

override func spec() {

let item = ActionSheetLinkItem(title: "foo", value: true, image: UIImage())

describe("when created") {
describe("cell") {

it("applies provided values") {
expect(item.title).to(equal("foo"))
expect(item.value as? Bool).to(equal(true))
expect(item.image).toNot(beNil())
it("is of correct type") {
let item = ActionSheetLinkItem(title: "foo")
let cell = item.cell(for: UITableView())

expect(cell is ActionSheetLinkItemCell).to(beTrue())
expect(cell.reuseIdentifier).to(equal(item.cellReuseIdentifier))
}
}
}
}


class ActionSheetLinkItemCellTests: QuickSpec {

override func spec() {

describe("tap behavior") {
describe("refreshing") {

it("is dismiss") {
expect(item.tapBehavior).to(equal(ActionSheetItem.TapBehavior.dismiss))
it("applies accessory view with link icon") {
let item = ActionSheetLinkItem(title: "foo")
let cell = item.cell(for: UITableView()) as? ActionSheetLinkItemCell
cell?.linkIcon = UIImage()
cell?.refresh()
let imageView = cell?.accessoryView as? UIImageView

expect(imageView?.image).toNot(beNil())
expect(imageView?.image).to(be(cell?.linkIcon))
}
}
}
Expand Down
61 changes: 46 additions & 15 deletions SheeeeeeeeetTests/Items/Items/ActionSheetMultiSelectItemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,62 @@ class ActionSheetMultiSelectItemTests: QuickSpec {

override func spec() {

func getItem(isSelected: Bool, group: String = "") -> ActionSheetMultiSelectItem {
return ActionSheetMultiSelectItem(title: "foo", isSelected: isSelected, group: group, value: true, image: UIImage())
}

describe("when created") {
describe("instance") {

it("can be created with default values") {
let item = ActionSheetMultiSelectItem(title: "foo", isSelected: false)
expect(item.title).to(equal("foo"))
expect(item.isSelected).to(beFalse())
expect(item.group).to(equal(""))
expect(item.value).to(beNil())
expect(item.image).to(beNil())
expect(item.tapBehavior).to(equal(ActionSheetItem.TapBehavior.none))
}

it("applies provided values") {
let item = getItem(isSelected: true, group: "my group")
it("can be created with custom values") {
let item = ActionSheetMultiSelectItem(title: "foo", isSelected: true, group: "group", value: true, image: UIImage())
expect(item.title).to(equal("foo"))
expect(item.group).to(equal("my group"))
expect(item.isSelected).to(beTrue())
expect(item.group).to(equal("group"))
expect(item.value as? Bool).to(equal(true))
expect(item.image).toNot(beNil())
expect(item.tapBehavior).to(equal(ActionSheetItem.TapBehavior.none))
}
}


describe("cell") {

it("applies provided selection state") {
expect(getItem(isSelected: true).isSelected).to(beTrue())
expect(getItem(isSelected: false).isSelected).to(beFalse())
it("is of correct type") {
let item = ActionSheetMultiSelectItem(title: "foo", isSelected: false)
let cell = item.cell(for: UITableView())

expect(cell is ActionSheetMultiSelectItemCell).to(beTrue())
expect(cell.reuseIdentifier).to(equal(item.cellReuseIdentifier))
}
}

describe("tap behavior") {

describe("handling tap") {

it("is none") {
let item = getItem(isSelected: true)
expect(item.tapBehavior).to(equal(ActionSheetItem.TapBehavior.none))
it("updates toggle item in the same group") {
let item1 = ActionSheetMultiSelectItem(title: "foo", isSelected: false, group: "group 1")
let item2 = ActionSheetMultiSelectItem(title: "bar", isSelected: false, group: "group 2")
let item3 = ActionSheetMultiSelectItem(title: "baz", isSelected: false, group: "group 1")
let toggle1 = ActionSheetMultiSelectToggleItem(title: "toggle 1", state: .selectAll, group: "group 1", selectAllTitle: "", deselectAllTitle: "")
let toggle2 = ActionSheetMultiSelectToggleItem(title: "toggle 2", state: .selectAll, group: "group 2", selectAllTitle: "", deselectAllTitle: "")
let items = [item1, item2, item3, toggle1, toggle2]
let sheet = ActionSheet(items: items) { (_, _) in }

item1.handleTap(in: sheet)
expect(toggle1.state).to(equal(.selectAll))
expect(toggle2.state).to(equal(.selectAll))
item2.handleTap(in: sheet)
expect(toggle1.state).to(equal(.selectAll))
expect(toggle2.state).to(equal(.deselectAll))
item3.handleTap(in: sheet)
expect(toggle1.state).to(equal(.deselectAll))
expect(toggle2.state).to(equal(.deselectAll))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,80 @@ class ActionSheetMultiSelectToggleItemTests: QuickSpec {

override func spec() {

func getItem(group: String = "") -> ActionSheetMultiSelectToggleItem {
func createItem(group: String) -> ActionSheetMultiSelectToggleItem {
return ActionSheetMultiSelectToggleItem(title: "foo", state: .selectAll, group: group, selectAllTitle: "select all", deselectAllTitle: "deselect all")
}

describe("when created") {
describe("instance") {

it("applies provided values") {
let item = getItem(group: "my group")
it("can be created with custom values") {
let item = createItem(group: "group")
expect(item.title).to(equal("foo"))
expect(item.group).to(equal("my group"))
expect(item.state).to(equal(.selectAll))
expect(item.group).to(equal("group"))
expect(item.value).to(beNil())
expect(item.selectAllTitle).to(equal("select all"))
expect(item.deselectAllTitle).to(equal("deselect all"))
expect(item.tapBehavior).to(equal(ActionSheetItem.TapBehavior.none))
}
}

describe("cell style") {

describe("cell") {

it("is value1") {
expect(getItem().cellStyle).to(equal(UITableViewCell.CellStyle.value1))
it("is of correct type") {
let item = createItem(group: "group")
let cell = item.cell(for: UITableView())

expect(cell is ActionSheetMultiSelectToggleItemCell).to(beTrue())
expect(cell.reuseIdentifier).to(equal(item.cellReuseIdentifier))
}
}

describe("tap behavior") {

describe("handling tap") {

var sheet: ActionSheet!
var item1: ActionSheetMultiSelectItem!
var item2: ActionSheetMultiSelectItem!
var item3: ActionSheetMultiSelectItem!
var toggle1: ActionSheetMultiSelectToggleItem!
var toggle2: ActionSheetMultiSelectToggleItem!

beforeEach {
item1 = ActionSheetMultiSelectItem(title: "foo", isSelected: false, group: "group 1")
item2 = ActionSheetMultiSelectItem(title: "bar", isSelected: false, group: "group 2")
item3 = ActionSheetMultiSelectItem(title: "baz", isSelected: false, group: "group 1")
toggle1 = createItem(group: "group 1")
toggle2 = createItem(group: "group 3")
sheet = ActionSheet(items: [item1, item2, item3, toggle1, toggle2]) { (_, _) in }
}

it("resets state if no matching items exist in sheet") {
toggle2.state = .deselectAll
toggle2.handleTap(in: sheet)
expect(toggle2.state).to(equal(.selectAll))
}

it("is none") {
expect(getItem().tapBehavior).to(equal(ActionSheetItem.TapBehavior.none))
it("toggles select items in the same group") {
toggle1.handleTap(in: sheet)
expect(item1.isSelected).to(beTrue())
expect(item2.isSelected).to(beFalse())
expect(item3.isSelected).to(beTrue())
expect(toggle1.state).to(equal(.deselectAll))
expect(toggle2.state).to(equal(.selectAll))
toggle1.handleTap(in: sheet)
expect(item1.isSelected).to(beFalse())
expect(item2.isSelected).to(beFalse())
expect(item3.isSelected).to(beFalse())
expect(toggle1.state).to(equal(.selectAll))
expect(toggle2.state).to(equal(.selectAll))
toggle2.handleTap(in: sheet)
expect(item1.isSelected).to(beFalse())
expect(item2.isSelected).to(beFalse())
expect(item3.isSelected).to(beFalse())
expect(toggle1.state).to(equal(.selectAll))
expect(toggle2.state).to(equal(.selectAll))
}
}
}
Expand Down
Loading

0 comments on commit 1800bf5

Please sign in to comment.