diff --git a/DemoApp/Rees46DemoTests/SearchServiceTests.swift b/DemoApp/Rees46DemoTests/SearchServiceTests.swift index 7659df02..b28dc971 100644 --- a/DemoApp/Rees46DemoTests/SearchServiceTests.swift +++ b/DemoApp/Rees46DemoTests/SearchServiceTests.swift @@ -187,6 +187,34 @@ class SearchServiceImplTests: XCTestCase { waitForExpectations(timeout: 5, handler: nil) } + func testSearchBlank_popularFields() { + let expectation = self.expectation(description: "searchBlank completion") + + sdk?.searchBlank { result in + switch result { + case .success(let response): + XCTAssertFalse(response.popularLinks.isEmpty, "popular_links should not be empty for this shop") + for item in response.popularLinks { + XCTAssertFalse(item.name.isEmpty, "popular_links item must have a name") + XCTAssertFalse(item.url.isEmpty, "popular_links item must have a url") + } + for item in response.popularCategories { + XCTAssertFalse(item.name.isEmpty, "popular_categories item must have a name") + XCTAssertFalse(item.url.isEmpty, "popular_categories item must have a url") + } + for item in response.popularBrands { + XCTAssertFalse(item.name.isEmpty, "popular_brands item must have a name") + XCTAssertFalse(item.url.isEmpty, "popular_brands item must have a url") + } + case .failure(let error): + XCTFail("searchBlank failed with error: \(error)") + } + expectation.fulfill() + } + + waitForExpectations(timeout: 10, handler: nil) + } + func testSearch_withMinimalFields() { let query = "minimal query" diff --git a/REES46/Classes/Model/SearchResponse.swift b/REES46/Classes/Model/SearchResponse.swift index 3814f52b..bdc71a0c 100644 --- a/REES46/Classes/Model/SearchResponse.swift +++ b/REES46/Classes/Model/SearchResponse.swift @@ -73,12 +73,25 @@ public struct Suggest { } } +public struct PopularItem { + public var name: String + public var url: String + + init(json: [String: Any]) { + self.name = json["name"] as? String ?? "" + self.url = json["url"] as? String ?? "" + } +} + public struct SearchBlankResponse { public var lastQueries: [Query] public var suggests: [Suggest] public var lastProducts: Bool public var products: [Product] - + public var popularCategories: [PopularItem] + public var popularBrands: [PopularItem] + public var popularLinks: [PopularItem] + init(json: [String: Any]) { let lastQueriesTemp = json["last_queries"] as? [[String: Any]] ?? [] var quyArr = [Query]() @@ -86,22 +99,26 @@ public struct SearchBlankResponse { quyArr.append(Query(json: item)) } lastQueries = quyArr - + let suggestsTemp = json["suggests"] as? [[String: Any]] ?? [] var sugArr = [Suggest]() for item in suggestsTemp { sugArr.append(Suggest(json: item)) } suggests = sugArr - + lastProducts = json["last_products"] as? Bool ?? true - + let productsTemp = json["products"] as? [[String: Any]] ?? [] var productArr = [Product]() for item in productsTemp { productArr.append(Product(json: item)) } products = productArr + + popularCategories = (json["popular_categories"] as? [[String: Any]] ?? []).map { PopularItem(json: $0) } + popularBrands = (json["popular_brands"] as? [[String: Any]] ?? []).map { PopularItem(json: $0) } + popularLinks = (json["popular_links"] as? [[String: Any]] ?? []).map { PopularItem(json: $0) } } }