Skip to content

Commit

Permalink
Add tests for Interactor state updates on engagement end
Browse files Browse the repository at this point in the history
  • Loading branch information
ykyivskyi-gl authored and Egor Egorov committed Dec 31, 2024
1 parent a0ef66e commit 7ad0d5b
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions GliaWidgetsTests/Sources/InteractorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -583,4 +583,110 @@ class InteractorTests: XCTestCase {

items.forEach(test)
}

func test_endAfterEnqueuedEngagementSetsEndedState() {
let mockQueueTicket = CoreSdkClient.QueueTicket.mock
let interactor = makeEnqueuingSetupInteractor(with: mockQueueTicket, mediaType: .audio)

interactor.enqueueForEngagement(
mediaType: .audio,
success: {},
failure: { _ in }
)
XCTAssertEqual(interactor.state, .enqueued(mockQueueTicket))

interactor.endEngagement { _ in }
XCTAssertEqual(interactor.state, .ended(.byVisitor))
}

func test_endSessionMakesCleanupWhenEngagementEndedByOperator() {
let mockQueueTicket = CoreSdkClient.QueueTicket.mock
let mockEngagement = CoreSdkClient.Engagement.mock(id: UUID.mock.uuidString)
let interactor = makeEnqueuingSetupInteractor(with: mockQueueTicket, mediaType: .audio, engagement: mockEngagement)

interactor.enqueueForEngagement(
mediaType: .audio,
success: {},
failure: { _ in }
)
XCTAssertEqual(interactor.state, .enqueued(mockQueueTicket))

interactor.end(with: .operatorHungUp)
XCTAssertEqual(interactor.state, .ended(.byOperator))
XCTAssertEqual(interactor.endedEngagement, mockEngagement)

interactor.endSession { _ in }
XCTAssertEqual(interactor.state, .none)
XCTAssertEqual(interactor.endedEngagement, nil)
}

func test_endSessionMakesCleanupWhenEngagementEndedForFollowUp() {
let mockQueueTicket = CoreSdkClient.QueueTicket.mock
let mockEngagement = CoreSdkClient.Engagement.mock(id: UUID.mock.uuidString)
let interactor = makeEnqueuingSetupInteractor(with: mockQueueTicket, mediaType: .audio, engagement: mockEngagement)

interactor.enqueueForEngagement(
mediaType: .audio,
success: {},
failure: { _ in }
)
XCTAssertEqual(interactor.state, .enqueued(mockQueueTicket))

interactor.end(with: .followUp)
XCTAssertEqual(interactor.state, .ended(.byOperator))
XCTAssertEqual(interactor.endedEngagement, mockEngagement)

interactor.endSession { _ in }
XCTAssertEqual(interactor.state, .none)
XCTAssertEqual(interactor.endedEngagement, nil)
}

func test_cleanupResetesStateAndNilifyEndedEngagement() {
let mockQueueTicket = CoreSdkClient.QueueTicket.mock
let mockEngagement = CoreSdkClient.Engagement.mock(id: UUID.mock.uuidString)
let interactor = makeEnqueuingSetupInteractor(with: mockQueueTicket, mediaType: .audio, engagement: mockEngagement)

interactor.enqueueForEngagement(
mediaType: .audio,
success: {},
failure: { _ in }
)
XCTAssertEqual(interactor.state, .enqueued(mockQueueTicket))

interactor.end(with: .followUp)
XCTAssertEqual(interactor.state, .ended(.byOperator))
XCTAssertEqual(interactor.endedEngagement, mockEngagement)

interactor.cleanup()
XCTAssertEqual(interactor.state, .none)
XCTAssertEqual(interactor.endedEngagement, nil)
}
}

extension InteractorTests {
func makeEnqueuingSetupInteractor(
with queueTicket: CoreSdkClient.QueueTicket,
mediaType: CoreSdkClient.MediaType,
engagement: CoreSdkClient.Engagement? = nil
) -> Interactor {
var coreSdk = CoreSdkClient.failing
coreSdk.queueForEngagement = { _, completion in
completion(.success(queueTicket))
}
coreSdk.endEngagement = { completion in
completion(true, nil)
}
coreSdk.getCurrentEngagement = {
engagement
}

var interactorEnv = Interactor.Environment(coreSdk: coreSdk, queuesMonitor: .mock(), gcd: .failing, log: .failing)
interactorEnv.log.infoClosure = { _, _, _, _ in }
interactorEnv.log.prefixedClosure = { _ in interactorEnv.log }

let interactor = Interactor.mock(environment: interactorEnv)
interactor.state = .enqueueing(mediaType)

return interactor
}
}

0 comments on commit 7ad0d5b

Please sign in to comment.