Skip to content

Commit

Permalink
fix: updating orderedFacets of the DynamicFacetListInteractor ign…
Browse files Browse the repository at this point in the history
…ores `disjunctiveFacets` (#258)
  • Loading branch information
VladislavFitz authored Oct 13, 2022
1 parent 5981de8 commit abcc825
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

/// List of ordered facets with their attribute.
public struct AttributedFacets: Codable {
public struct AttributedFacets: Codable, Equatable {

/// Facet attribute
public let attribute: Attribute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ public extension DynamicFacetListInteractor {

public func connect() {
searcher.onResults.subscribe(with: interactor) { (interactor, searchResponse) in
if let facetOrdering = searchResponse.renderingContent?.facetOrdering,
let facets = searchResponse.facets {
interactor.orderedFacets = FacetsOrderer(facetOrder: facetOrdering, facets: facets)()
} else {
interactor.orderedFacets = []
}
interactor.update(with: searchResponse)
}
(searcher as? ErrorObservable)?.onError.subscribe(with: interactor) { interactor, _ in
interactor.orderedFacets = []
Expand All @@ -61,3 +56,20 @@ public extension DynamicFacetListInteractor {
}

}

extension DynamicFacetListInteractor {

/// Update `orderedFacets` property with `renderingContent` and
/// `facets`/`disjunctiveFacets` received of the `SearchResponse` instance
public func update(with searchResponse: SearchResponse) {
guard let facetOrdering = searchResponse.renderingContent?.facetOrdering else {
orderedFacets = []
return
}
let commonFacets = searchResponse.facets ?? [:]
let disjunctiveFacets = searchResponse.disjunctiveFacets ?? [:]
let facets = disjunctiveFacets.merging(commonFacets, uniquingKeysWith: { disjunctiveFacets, _ in disjunctiveFacets })
orderedFacets = FacetsOrderer(facetOrder: facetOrdering, facets: facets)()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// DynamicFacetListInteractorTests.swift
//
//
// Created by Vladislav Fitc on 12/10/2022.
//

import Foundation
@testable import InstantSearchCore
import XCTest

class DynamicFacetListInteractorTests: XCTestCase {

func testDisjunctiveFacetsPropagation() {
let interactor = DynamicFacetListInteractor()

func facets(of raw: (String, Int)...) -> [Facet] {
raw.map { Facet(value: $0.0, count: $0.1) }
}

var response = SearchResponse()

response.facets = [
"size": facets(of: ("s", 10)),
"brand": facets(of: ("samsung", 5), ("sony", 4), ("philips", 12)),
]
response.disjunctiveFacets = [
"size": facets(of: ("s", 10), ("m", 20), ("l", 30), ("xl", 40)),
"color": facets(of: ("red", 5), ("green", 10), ("blue", 15))
]
response.renderingContent = try! RenderingContent(json: [
"facetOrdering": [
"facets": [
"order": [
"color",
"size",
"brand",
],
],
],
])

interactor.update(with: response)

XCTAssertEqual(interactor.orderedFacets.count, 3)
XCTAssertTrue(interactor.orderedFacets.contains(AttributedFacets(attribute: "size",
facets: facets(of: ("s", 10), ("m", 20), ("l", 30), ("xl", 40)))))
XCTAssertTrue(interactor.orderedFacets.contains(AttributedFacets(attribute: "brand",
facets: facets(of: ("samsung", 5), ("sony", 4), ("philips", 12)))))
XCTAssertTrue(interactor.orderedFacets.contains(AttributedFacets(attribute: "color",
facets: facets(of: ("red", 5), ("green", 10), ("blue", 15)))))

}

}

0 comments on commit abcc825

Please sign in to comment.