Skip to content

Commit

Permalink
Add tests for prefixing and adding add item
Browse files Browse the repository at this point in the history
  • Loading branch information
dus7 committed Sep 23, 2024
1 parent 87f2790 commit e5f9c63
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions DuckDuckGoTests/NewTabPageFavoritesModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,43 @@ final class NewTabPageFavoritesModelTests: XCTestCase {
XCTAssertEqual(PixelFiringMock.lastPixel, .newTabPageFavoritesPlaceholderTapped)
}

func testPrefixFavoritesCreatesRemainingPlaceholders() {
let sut = createSUT()

let slice = sut.prefixedFavorites(for: 3)

XCTAssertEqual(slice.items.count(where: \.isPlaceholder), 2)

Check failure on line 96 in DuckDuckGoTests/NewTabPageFavoritesModelTests.swift

View workflow job for this annotation

GitHub Actions / Unit Tests

cannot call value of non-function type 'Int'

Check failure on line 96 in DuckDuckGoTests/NewTabPageFavoritesModelTests.swift

View workflow job for this annotation

GitHub Actions / Unit Tests

cannot infer key path type from context; consider explicitly specifying a root type
XCTAssertEqual(slice.items.count, 3)
XCTAssertFalse(slice.isCollapsible)
}

func testPrefixFavoritesLimitsToTwoRows() {
favoriteDataSource.favorites.append(contentsOf: Array(repeating: Favorite.stub(), count: 10))
let sut = createSUT()

let slice = sut.prefixedFavorites(for: 4)

XCTAssertEqual(slice.items.count, 8)
XCTAssertTrue(slice.isCollapsible)
}

func testAddItemIsLastWhenFavoritesPresent() throws {
favoriteDataSource.favorites.append(contentsOf: Array(repeating: Favorite.stub(), count: 10))
let sut = createSUT()

let lastItem = try XCTUnwrap(sut.allFavorites.last)

XCTAssertTrue(lastItem == .addFavorite)
}

func testAddItemIsFirstWhenFavoritesEmpty() throws {
let sut = createSUT()

let firstItem = try XCTUnwrap(sut.allFavorites.first)

XCTAssertTrue(firstItem == .addFavorite)
}

private func createSUT() -> FavoritesViewModel {
FavoritesViewModel(favoriteDataSource: favoriteDataSource,
faviconLoader: FavoritesFaviconLoader(),
Expand Down Expand Up @@ -120,3 +157,12 @@ private extension Favorite {
Favorite(id: UUID().uuidString, title: "foo", domain: "bar")
}
}

private extension FavoriteItem {
var isPlaceholder: Bool {
switch self {
case .placeholder: return true
case .favorite, .addFavorite: return false
}
}
}

0 comments on commit e5f9c63

Please sign in to comment.