Skip to content

Commit

Permalink
More unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
suhaibabsi-inst committed Oct 21, 2024
1 parent 7fdd29e commit 28dc665
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ struct CourseSmartSearchGroupedResultsView: View {
})
LazyVStack(spacing: 0) {

ForEach($resultSections, id: \.type) { sec in
let section = sec.wrappedValue
ForEach($resultSections, id: \.type) { sectionBinding in
let section = sectionBinding.wrappedValue
let title = "\(section.type.title) (\(section.results.count))"

DisclosureGroup(title, isExpanded: sec.expanded) {
DisclosureGroup(title, isExpanded: sectionBinding.expanded) {
ForEach(section.results) { result in
CourseSearchResultRowView(
selected: $selected,
Expand Down Expand Up @@ -158,7 +158,7 @@ private struct RowDivider: View {
SwiftUI
.Divider()
.overlay {
// This to fix an issue on collapse/expanding of disclosure group
// This is to fix an issue on collapse/expanding of disclosure group
Color.borderMedium.frame(maxHeight: 0.5)
}
.padding(.horizontal, withPadding ? 16 : 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ class CourseSmartSearchViewModel: ObservableObject {
@Published private(set) var results: [CourseSmartSearchResult] = []
@Published var filter: CourseSmartSearchFilter?

let sortStrategy: (CourseSmartSearchResult, CourseSmartSearchResult) -> Bool = { (result1, result2) in
// First: Sort on relevance
// Ideally, API should be returning results sorted according to relevance,
// This is to double check on this, in addition to unit testing.
if result1.relevance != result2.relevance {
return result1.relevance > result2.relevance
}

// Then: Sort on alphabetical order
return result1.title < result2.title
}

var sectionedResults: [CourseSmartSearchResultsSection] {
let filtered = filter.flatMap { filter in
return results.filter(filter.apply(to:))
Expand Down Expand Up @@ -90,18 +102,7 @@ class CourseSmartSearchViewModel: ObservableObject {
}

private func updateResults(_ results: [CourseSmartSearchResult]?) {
self.results = (results ?? [])
.sorted(by: { (result1, result2) in
// First: Sort on relevance
// Ideally, API should be returning results sorted according to relevance,
// This is to double check on this, in addition to unit testing.
if result1.relevance != result2.relevance {
return result1.relevance > result2.relevance
}

// Then: Sort on alphabetical order
return result1.title < result2.title
})
self.results = (results ?? []).sorted(by: sortStrategy)
applyFilter()
}

Expand Down
49 changes: 44 additions & 5 deletions Core/CoreTests/Search/CourseSmartSearchViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,22 @@ final class CourseSmartSearchViewModelTests: CoreTestCase {
static var searchContext: CoreSearchContext<CourseSmartSearch> { .init(info: info) }

static var results: [CourseSmartSearchResult] = [
.make(type: .page),
.make(type: .page),
.make(type: .assignment),
.make(type: .assignment),
.make(type: .discussion),
.make(type: .discussion),
.make(type: .announcement),
.make(type: .page)
.make(type: .discussion),
.make(type: .discussion)
]
}

func test_searching_phases() throws {
override func setUp() {
super.setUp()
API.resetMocks()
}

func test_searching_results() throws {
// Given
let searchWord = "Demo Search"
let context = TestConstants.searchContext
Expand Down Expand Up @@ -73,7 +78,41 @@ final class CourseSmartSearchViewModelTests: CoreTestCase {
drainMainQueue()

// Then
XCTAssertEqual(model.results, TestConstants.results)
XCTAssertEqual(model.results, TestConstants.results.sorted(by: model.sortStrategy))
}

func test_searching_no_match() throws {
// Given
let searchWord = "Search Nothing"
let context = TestConstants.searchContext

let mockRequest = api.mock(
CourseSmartSearchRequest(
courseId: TestConstants.courseId,
searchText: searchWord,
filter: nil
),
value: nil
)
mockRequest.suspend()

// When
let model = CourseSmartSearchViewModel()
// Then
XCTAssertEqual(model.phase, .start)

// When
model.startSearch(of: searchWord, in: context, using: environment)
// Then
XCTAssertEqual(model.phase, .loading)

// When
mockRequest.resume()
drainMainQueue()

// Then
XCTAssertEqual(model.phase, .noMatch)
XCTAssertEqual(model.results, [])
}

func test_searching_filtered() throws {
Expand Down

0 comments on commit 28dc665

Please sign in to comment.