Skip to content

Commit

Permalink
Add missing tests for stubs rearrangement (#793)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/1201493110486074/1207136222342904/f
iOS PR: n/a
macOS PR: n/a
What kind of version bump will this require?: Patch

Description:

Expand test suite to cover stub objects when rearranging Bookmarks.
  • Loading branch information
bwaresiak authored Apr 29, 2024
1 parent f8c7329 commit 8d8f54f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
57 changes: 57 additions & 0 deletions Tests/BookmarksTests/BookmarkListViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,63 @@ final class BookmarkListViewModelTests: XCTestCase {
}
}

func testWhenBookmarkIsMovedAndThereAreStubsThenCorrectIndexIsCalculated() {

let context = bookmarkListViewModel.context

let bookmarkTree = BookmarkTree {
Bookmark(id: "1")
Bookmark(id: "s1", isStub: true)
Bookmark(id: "2")
Bookmark(id: "s2", isStub: true)
Bookmark(id: "3")
Bookmark(id: "s3", isStub: true)
}

context.performAndWait {
bookmarkTree.createEntities(in: context)
try! context.save()

let rootFolder = BookmarkUtils.fetchRootFolder(context)!

let bookmark = BookmarkEntity.fetchBookmark(withUUID: "1", context: context)!

bookmarkListViewModel.reloadData()

bookmarkListViewModel.moveBookmark(bookmark, fromIndex: 0, toIndex: 0)
assertEquivalent(withTimestamps: false, rootFolder, BookmarkTree {
Bookmark(id: "1")
Bookmark(id: "s1", isStub: true)
Bookmark(id: "2")
Bookmark(id: "s2", isStub: true)
Bookmark(id: "3")
Bookmark(id: "s3", isStub: true)
})

bookmarkListViewModel.moveBookmark(bookmark, fromIndex: 0, toIndex: 1)
assertEquivalent(withTimestamps: false, rootFolder, BookmarkTree {
Bookmark(id: "s1", isStub: true)
Bookmark(id: "2")
Bookmark(id: "1")
Bookmark(id: "s2", isStub: true)
Bookmark(id: "3")
Bookmark(id: "s3", isStub: true)
})

bookmarkListViewModel.moveBookmark(bookmark, fromIndex: 1, toIndex: 2)
assertEquivalent(withTimestamps: false, rootFolder, BookmarkTree {
Bookmark(id: "s1", isStub: true)
Bookmark(id: "2")
Bookmark(id: "s2", isStub: true)
Bookmark(id: "3")
Bookmark(id: "1")
Bookmark(id: "s3", isStub: true)
})

XCTAssertEqual(firedEvents, [])
}
}

func testWhenOrphanedBookmarkIsMovedThenItIsAttachedToRootFolder() async throws {

let context = bookmarkListViewModel.context
Expand Down
40 changes: 40 additions & 0 deletions Tests/BookmarksTests/FavoriteListViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,46 @@ final class FavoriteListViewModelTests: XCTestCase {
}
}

func testWhenBookmarkIsMovedAndThereAreStubsThenCorrectIndexIsCalculated() {

let context = favoriteListViewModel.context

let bookmarkTree = BookmarkTree {
Bookmark(id: "1", favoritedOn: [.mobile, .unified])
Bookmark(id: "s1", favoritedOn: [.mobile, .unified], isStub: true)
Bookmark(id: "2", favoritedOn: [.mobile, .unified])
Bookmark(id: "s2", favoritedOn: [.mobile, .unified], isStub: true)
Bookmark(id: "3", favoritedOn: [.mobile, .unified])
Bookmark(id: "s3", favoritedOn: [.mobile, .unified], isStub: true)
}

context.performAndWait {
bookmarkTree.createEntities(in: context)
try! context.save()

let bookmark = BookmarkEntity.fetchBookmark(withUUID: "1", context: context)!

let favoriteFolderUUID = favoriteListViewModel.favoritesDisplayMode.displayedFolder.rawValue
let rootFavoriteFolder = BookmarkUtils.fetchFavoritesFolder(withUUID: favoriteFolderUUID, in: context)!

favoriteListViewModel.reloadData()

favoriteListViewModel.moveFavorite(bookmark, fromIndex: 0, toIndex: 0)
XCTAssertEqual((rootFavoriteFolder.favorites?.array as! [BookmarkEntity]).map(\.uuid), ["1", "s1", "2", "s2", "3", "s3"])
XCTAssertEqual(rootFavoriteFolder.favoritesArray.map(\.title), ["1", "2", "3"])

favoriteListViewModel.moveFavorite(bookmark, fromIndex: 0, toIndex: 1)
XCTAssertEqual((rootFavoriteFolder.favorites?.array as! [BookmarkEntity]).map(\.uuid), ["s1", "2", "1", "s2", "3", "s3"])
XCTAssertEqual(rootFavoriteFolder.favoritesArray.map(\.title), ["2", "1", "3"])

favoriteListViewModel.moveFavorite(bookmark, fromIndex: 1, toIndex: 2)
XCTAssertEqual((rootFavoriteFolder.favorites?.array as! [BookmarkEntity]).map(\.uuid), ["s1", "2", "s2", "3", "1", "s3"])
XCTAssertEqual(rootFavoriteFolder.favoritesArray.map(\.title), ["2", "3", "1"])

XCTAssertEqual(firedEvents, [])
}
}

func testDisplayNativeMode_WhenFavoriteIsUnfavoritedThenItIsRemovedFromNativeAndUnifiedFolder() async throws {

favoriteListViewModel.favoritesDisplayMode = .displayNative(.mobile)
Expand Down

0 comments on commit 8d8f54f

Please sign in to comment.