diff --git a/WordPress/WordPressTest/Menus/Controllers/MenuItemsViewControllerTests.swift b/WordPress/WordPressTest/Menus/Controllers/MenuItemsViewControllerTests.swift index a102ad933d55..366351356136 100644 --- a/WordPress/WordPressTest/Menus/Controllers/MenuItemsViewControllerTests.swift +++ b/WordPress/WordPressTest/Menus/Controllers/MenuItemsViewControllerTests.swift @@ -1,8 +1,5 @@ -import UIKit import XCTest -@testable import WordPress - class MenuItemsViewControllerTests: XCTestCase { private var context: NSManagedObjectContext! @@ -20,53 +17,96 @@ class MenuItemsViewControllerTests: XCTestCase { /// Tests that no string is provided when there is nothing to announce. func testOrderingChangeVOStringNoChanges() { - let noChanges = MenuItemsViewController.generateOrderingChangeVOString(nil, parentChanged: false, before: nil, after: nil) + let noChanges = MenuItemsViewController.generateOrderingChangeVOString( + nil, + parentChanged: false, + before: nil, + after: nil + ) XCTAssertNil(noChanges) - let newParent = MenuItem(context: context) - newParent.name = "New Parent" + let newParent = newMenuItem(named: "New Parent") /// This is a programming error. parentChanged should always be set to true if the parent indeed changed. - let parentChangedFalse = MenuItemsViewController.generateOrderingChangeVOString(newParent, parentChanged: false, before: nil, after: nil) + let parentChangedFalse = MenuItemsViewController.generateOrderingChangeVOString( + newParent, + parentChanged: false, + before: nil, + after: nil + ) XCTAssertNil(parentChangedFalse) } /// Tests handling of changes to the menu item's parent. func testOrderingChangeVOStringParentChanged() { - let newParent = MenuItem(context: context) - newParent.name = "New Parent" - - /// test when the parent changed but it was the top level (no parent) - let topLevelString = MenuItemsViewController.generateOrderingChangeVOString(nil, parentChanged: true, before: nil, after: nil) + let newParent = newMenuItem(named: "New Parent") + + let topLevelString = MenuItemsViewController.generateOrderingChangeVOString( + nil, + parentChanged: true, + before: nil, + after: nil + ) XCTAssertEqual(topLevelString, "Top level") - /// test when the parent changed and it was a menu item - let newParentString = MenuItemsViewController.generateOrderingChangeVOString(newParent, parentChanged: true, before: nil, after: nil) + let newParentString = MenuItemsViewController.generateOrderingChangeVOString( + newParent, + parentChanged: true, + before: nil, + after: nil + ) XCTAssertEqual(newParentString, "Child of New Parent") } /// Tests handling of changes to the menu item's parent and order. func testOrderingChangeVOString() { - let newParent = MenuItem(context: context) - newParent.name = "New Parent" - - let afterItem = MenuItem(context: context) - afterItem.name = "Item A" - - let beforeItem = MenuItem(context: context) - beforeItem.name = "Item B" - - let beforeString = MenuItemsViewController.generateOrderingChangeVOString(nil, parentChanged: false, before: beforeItem, after: nil) + let newParent = newMenuItem(named: "New Parent") + let afterItem = newMenuItem(named: "Item A") + let beforeItem = newMenuItem(named: "Item B") + + let beforeString = MenuItemsViewController.generateOrderingChangeVOString( + nil, + parentChanged: false, + before: beforeItem, + after: nil + ) XCTAssertEqual(beforeString, "Before Item B") - let afterString = MenuItemsViewController.generateOrderingChangeVOString(nil, parentChanged: false, before: nil, after: afterItem) + let afterString = MenuItemsViewController.generateOrderingChangeVOString( + nil, + parentChanged: false, + before: nil, + after: afterItem + ) XCTAssertEqual(afterString, "After Item A") - let parentAndBeforeString = MenuItemsViewController.generateOrderingChangeVOString(newParent, parentChanged: true, before: beforeItem, after: nil) + let parentAndBeforeString = MenuItemsViewController.generateOrderingChangeVOString( + newParent, + parentChanged: true, + before: beforeItem, + after: nil + ) XCTAssertEqual(parentAndBeforeString, "Child of New Parent. Before Item B") - let parentAndBeforeAndAfterString = MenuItemsViewController.generateOrderingChangeVOString(newParent, parentChanged: true, before: beforeItem, after: afterItem) + let parentAndBeforeAndAfterString = MenuItemsViewController.generateOrderingChangeVOString( + newParent, + parentChanged: true, + before: beforeItem, + after: afterItem + ) XCTAssertEqual(parentAndBeforeAndAfterString, "Child of New Parent. After Item A. Before Item B") } + // MARK: - Private Helpers + + fileprivate func newMenuItem(named name: String) -> MenuItem { + let entityName = MenuItem.classNameWithoutNamespaces() + let entity = NSEntityDescription.insertNewObject(forEntityName: entityName, into: context) + + let menuItem = entity as! MenuItem + // set a name to make debugging easier + menuItem.name = name + + return menuItem + } } diff --git a/WordPress/WordPressTest/Menus/MenuItemTests.swift b/WordPress/WordPressTest/Menus/MenuItemTests.swift index bd60c29ce9d5..b2be819f465e 100644 --- a/WordPress/WordPressTest/Menus/MenuItemTests.swift +++ b/WordPress/WordPressTest/Menus/MenuItemTests.swift @@ -15,6 +15,7 @@ class MenuItemTests: XCTestCase { context = nil } + /// Tests detection of descendants. func testIsDescendantOfItem() { let itemA = newMenuItem(named: "Item A") let itemB = newMenuItem(named: "Item B") @@ -50,6 +51,7 @@ class MenuItemTests: XCTestCase { XCTAssertFalse(fIsNotDescendantOfA) } + /// Tests that the last descendant of an item is found. func testLastDescendantInOrderedItems() { let itemA = newMenuItem(named: "Item A") let itemB = newMenuItem(named: "Item B") @@ -81,19 +83,17 @@ class MenuItemTests: XCTestCase { itemE.parent = itemD itemF.parent = itemD - /// Item B has a child, but is the last descendant of Item A let lastDescendant = itemA.lastDescendant(inOrderedItems: orderedItems) XCTAssertEqual(lastDescendant, itemB) - /// Item C has no descendants let descendantOfItemC = itemC.lastDescendant(inOrderedItems: orderedItems) XCTAssertEqual(descendantOfItemC, nil) - /// Item F should be the last descendant of Item D let descendantOfItemD = itemD.lastDescendant(inOrderedItems: orderedItems) XCTAssertEqual(descendantOfItemD, itemF) } + /// Tests that preceding siblings are found. func testPrecedingSiblingInOrderedItems() { let itemA = newMenuItem(named: "Item A") let itemB = newMenuItem(named: "Item B")