Skip to content

Commit

Permalink
test(banner): add tests for topsort banner
Browse files Browse the repository at this point in the history
  • Loading branch information
smargozzini committed Jul 31, 2024
1 parent a33e295 commit 7515cd9
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ let package = Package(
.target(
name: "TopsortBanners",
dependencies: ["Topsort-Analytics"]),
// .testTarget(
// name: "banners.swiftTests",
// dependencies: ["Topsort-Banners"])
.testTarget(
name: "banners.swiftTests",
dependencies: ["TopsortBanners", "Topsort-Analytics"]),

]
)
60 changes: 60 additions & 0 deletions Tests/banners.swiftTests/bannerView_swiftTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import XCTest
@testable import TopsortBanners
@testable import Topsort_Analytics

class TopsortBannerTests: XCTestCase {

func testTopsortBannerInitialization() {
let expectation = self.expectation(description: "Button clicked action")
let banner = TopsortBanner(
apiKey: "test_api_key",
url: "test_url",
width: 300,
height: 250,
slotId: "test_slot_id",
deviceType: "test_device_type"
) { response in
expectation.fulfill()
}

XCTAssertEqual(banner.width, 300)
XCTAssertEqual(banner.height, 250)

banner.buttonClickedAction(nil)

wait(for: [expectation], timeout: 1.0)
}

func testExecuteAuctions() async {
// Mock the response
let asset = Asset(url: "https://example.com")
let winner = Winner(rank: 1, asset: [asset], type: "type", id: "id", resolvedBidId: "resolved_bid_id")
let auctionResult = AuctionResult(resultType: "result_type", winners: [winner], error: false)
let auctionResponse = AuctionResponse(results: [auctionResult])

// Mock Analytics and response
let mockAnalytics = MockAnalytics()
mockAnalytics.executeAuctionsMockResponse = auctionResponse

let banner = await TopsortBanner(
apiKey: "test_api_key",
url: "test_url",
width: 300,
height: 250,
slotId: "test_slot_id",
deviceType: "test_device_type",
buttonClickedAction: { response in
},
analytics: mockAnalytics
)

// Execute the method
await banner.executeAuctions(deviceType: "test_device_type", slotId: "test_slot_id")

await MainActor.run {
XCTAssertEqual(banner.sharedValues.resolvedBidId, "resolved_bid_id")
XCTAssertEqual(banner.sharedValues.urlString, "https://example.com")
XCTAssertFalse(banner.sharedValues.loading)
}
}
}
32 changes: 32 additions & 0 deletions Tests/banners.swiftTests/mocks/analyticsMock.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Foundation
import XCTest
@testable import Topsort_Analytics

public class MockAnalytics: AnalyticsProtocol {
public var opaqueUserId: String = "mocked-opaque-user-id"
public var executeAuctionsMockResponse: AuctionResponse?

public func set(opaqueUserId: String?) {
// Mock implementation
}

public func configure(apiKey: String, url: String? = nil) {
// Mock implementation
}

public func track(impression event: Event) {
// Mock implementation
}

public func track(click event: Event) {
// Mock implementation
}

public func track(purchase event: PurchaseEvent) {
// Mock implementation
}

public func executeAuctions(auctions: [Auction]) async -> AuctionResponse? {
return executeAuctionsMockResponse
}
}
29 changes: 29 additions & 0 deletions Tests/banners.swiftTests/sharedValues_swiftTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import XCTest
@testable import TopsortBanners
@testable import Topsort_Analytics

class SharedValuesTests: XCTestCase {

func testSharedValuesInitialization() {
let sharedValues = SharedValues()

XCTAssertNil(sharedValues.resolvedBidId)
XCTAssertTrue(sharedValues.loading)
XCTAssertNil(sharedValues.urlString)
XCTAssertNil(sharedValues.response)
}

func testSetResolvedBidIdAndUrlFromResponse() {
let asset = Asset(url: "https://example.com")
let winner = Winner(rank: 1, asset: [asset], type: "type", id: "id", resolvedBidId: "resolved_bid_id")
let auctionResult = AuctionResult(resultType: "result_type", winners: [winner], error: false)
let auctionResponse = AuctionResponse(results: [auctionResult])

let sharedValues = SharedValues()
sharedValues.response = auctionResponse
sharedValues.setResolvedBidIdAndUrlFromResponse()

XCTAssertEqual(sharedValues.resolvedBidId, "resolved_bid_id")
XCTAssertEqual(sharedValues.urlString, "https://example.com")
}
}

0 comments on commit 7515cd9

Please sign in to comment.