-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Search for bookmarks and folders by title (#2936)
- Loading branch information
1 parent
ce9dca1
commit 7577a09
Showing
7 changed files
with
223 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
DuckDuckGo/Bookmarks/ViewModel/BookmarkSearchViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// BookmarkSearchViewModel.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
|
||
struct BookmarkSearchViewModel { | ||
|
||
enum BookmarkSearchResult: Equatable { | ||
case emptyQuery | ||
case results([BaseBookmarkEntity]) | ||
|
||
static let noResults = Self.results([]) | ||
} | ||
|
||
let manager: BookmarkManager | ||
|
||
func search(by query: String) -> BookmarkSearchResult { | ||
if query.isBlank { | ||
return .emptyQuery | ||
} | ||
|
||
let filteredBookmarks = manager.search(by: query) | ||
|
||
return filteredBookmarks.isEmpty ? .noResults : .results(filteredBookmarks) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
UnitTests/Bookmarks/ViewModels/BookmarkSearchViewModelTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// BookmarkSearchViewModelTests.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import XCTest | ||
@testable import DuckDuckGo_Privacy_Browser | ||
|
||
final class BookmarkSearchViewModelTests: XCTestCase { | ||
|
||
func testWhenQueryIsEmpty_thenEmptyQueryIsReturned() { | ||
let sut = BookmarkSearchViewModel(manager: MockBookmarkManager()) | ||
let result = sut.search(by: "") | ||
|
||
XCTAssertEqual(result, .emptyQuery) | ||
} | ||
|
||
func testWhenQueryIsBlank_thenEmptyQueryIsReturned() { | ||
let sut = BookmarkSearchViewModel(manager: MockBookmarkManager()) | ||
let result = sut.search(by: " ") | ||
|
||
XCTAssertEqual(result, .emptyQuery) | ||
} | ||
|
||
func testWhenQueryIsNotBlankAndQueryDoesNotMatchBookmarks_thenNoResultsIsReturned() { | ||
let sut = BookmarkSearchViewModel(manager: MockBookmarkManager()) | ||
let result = sut.search(by: "abc") | ||
|
||
XCTAssertEqual(result, .noResults) | ||
} | ||
|
||
func testWhenQueryIsNotBlankAndQueryMatchesBookmarks_thenResultsAreReturned() { | ||
let bookmark = Bookmark(id: "1", url: "www.test.com", title: "Bookmark", isFavorite: false) | ||
let manager = MockBookmarkManager() | ||
manager.bookmarksReturnedForSearch = [bookmark] | ||
let sut = BookmarkSearchViewModel(manager: manager) | ||
let result = sut.search(by: "abc") | ||
|
||
XCTAssertEqual(result, .results([bookmark])) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters