Skip to content

Commit

Permalink
fixup! Cover QueuesMonitor with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ykyivskyi-gl committed Oct 22, 2024
1 parent aee2692 commit bfe23a5
Showing 1 changed file with 104 additions and 50 deletions.
154 changes: 104 additions & 50 deletions GliaWidgetsTests/Sources/QueuesMonitor/QueuesMonitorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import GliaCoreSDK
@testable import GliaWidgets

class QueuesMonitorTests: XCTestCase {
private enum Call {
case listQueues
case subscribeForQueuesUpdates
case unsubscribeFromUpdates
}

private var monitor: QueuesMonitor!
private var cancellables: CancelBag!

Expand All @@ -21,181 +27,229 @@ class QueuesMonitorTests: XCTestCase {
}

// MARK: Start monitoring
func test_startMonitoringWithQueues() {
func test_startMonitoringWithQueuesUpdatesWithQueuesList() {
var envCalls: [Call] = []

let mockQueueId = UUID().uuidString
let expectedObservedQueues = [Queue.mock(id: mockQueueId)]
let mockQueues = [expectedObservedQueues[0], Queue.mock(id: UUID().uuidString)]

monitor.environment.listQueues = { completion in
envCalls.append(.listQueues)
completion(mockQueues, nil)
}
monitor.environment.subscribeForQueuesUpdates = { _, completion in
envCalls.append(.subscribeForQueuesUpdates)
completion(.success(expectedObservedQueues[0]))
return UUID().uuidString
}

let expectation = XCTestExpectation(description: "State is updated with queues")

var receivedQueues: [Queue]?
monitor.$state
.sink { state in
if case let .updated(queues) = state {
XCTAssertEqual(queues, expectedObservedQueues)
expectation.fulfill()
receivedQueues = queues
}
}
.store(in: &cancellables)

monitor.startMonitoring(queuesIds: [mockQueueId])

wait(for: [expectation], timeout: 1.0)
XCTAssertEqual(receivedQueues, expectedObservedQueues)
XCTAssertEqual(envCalls, [.listQueues, .subscribeForQueuesUpdates])
}

func test_startMonitoringWithNoQueues() {
func test_startMonitoringWithWrongQueueIdsReturnsUpdatesWithDefaultQueue() {
var envCalls: [Call] = []

let expectedObservedQueues = [Queue.mock(isDefault: true)]
let mockQueues = [expectedObservedQueues[0], Queue.mock()]

monitor.environment.listQueues = { completion in
envCalls.append(.listQueues)
completion(mockQueues, nil)
}
monitor.environment.subscribeForQueuesUpdates = { _, completion in
envCalls.append(.subscribeForQueuesUpdates)
return UUID().uuidString
}

let expectation = XCTestExpectation(
description: "State is updated with default queues if no queue is found"
)

var receivedQueues: [Queue]?
monitor.$state
.sink { state in
if case let .updated(queues) = state {
XCTAssertEqual(queues, expectedObservedQueues)
expectation.fulfill()
receivedQueues = queues
}
}
.store(in: &cancellables)

monitor.startMonitoring(queuesIds: ["1"])

wait(for: [expectation], timeout: 1.0)
XCTAssertEqual(receivedQueues, expectedObservedQueues)
XCTAssertEqual(envCalls, [.listQueues, .subscribeForQueuesUpdates])
}

func test_startMonitoringError() {
func test_startMonitoringListQueuesReturnsError() {
var envCalls: [Call] = []

let expectedError = CoreSdkClient.SalemoveError.mock()

monitor.environment.listQueues = { completion in
envCalls.append(.listQueues)
completion(nil, expectedError)
}
monitor.environment.subscribeForQueuesUpdates = { _, completion in
envCalls.append(.subscribeForQueuesUpdates)
return UUID().uuidString
}

let expectation = XCTestExpectation(
description: "State is failed with list queues error"
)

var receivedError: CoreSdkClient.SalemoveError?
monitor.$state
.sink { state in
if case let .failed(error as CoreSdkClient.SalemoveError) = state {
XCTAssertEqual(error, expectedError)
expectation.fulfill()
receivedError = error
}
}
.store(in: &cancellables)

monitor.startMonitoring(queuesIds: ["1"])

wait(for: [expectation], timeout: 1.0)
XCTAssertEqual(receivedError, expectedError)
XCTAssertEqual(envCalls, [.listQueues])
}

func test_startMonitoringWithQueuesAndReceiveUpdatedQueueSuccess() {
var envCalls: [Call] = []

let mockQueueId = UUID().uuidString
let expectedObservedQueue = Queue.mock(id: mockQueueId, status: .closed)
let expectedUpdatedQueue = Queue.mock(id: mockQueueId, status: .open)
let mockQueues = [expectedObservedQueue, Queue.mock(id: UUID().uuidString)]

monitor.environment.listQueues = { completion in
envCalls.append(.listQueues)
completion(mockQueues, nil)
}

monitor.environment.subscribeForQueuesUpdates = { _, completion in
envCalls.append(.subscribeForQueuesUpdates)
completion(.success(expectedUpdatedQueue))
return UUID().uuidString
}

let expectation = XCTestExpectation(
description: "State is updated with queues received from socket"
)

var receivedQueues: [Queue]?
var receivedUpdatedQueue: Queue?
monitor.$state
// Drop initial .idle and .updated with listed queues state update
.dropFirst(2)
.sink { state in
print(state)
if case let .updated(queues) = state {
XCTAssertEqual(queues, [expectedUpdatedQueue])
XCTAssertEqual(queues[0].state.status, .open)
expectation.fulfill()
receivedQueues = queues
receivedUpdatedQueue = queues[0]
}
}
.store(in: &cancellables)

monitor.startMonitoring(queuesIds: [mockQueueId])

wait(for: [expectation], timeout: 1.0)
XCTAssertEqual(receivedQueues, [expectedUpdatedQueue])
XCTAssertEqual(receivedUpdatedQueue?.state.status, .open)
XCTAssertEqual(envCalls, [.listQueues, .subscribeForQueuesUpdates])
}

func test_startMonitoringWithQueuesAndReceiveUpdatedQueueError() {
var envCalls: [Call] = []

let expectedError = CoreSdkClient.SalemoveError.mock()
let mockQueues = [Queue.mock()]

monitor.environment.listQueues = { completion in
envCalls.append(.listQueues)
completion(mockQueues, nil)
}

monitor.environment.subscribeForQueuesUpdates = { _, completion in
envCalls.append(.subscribeForQueuesUpdates)
completion(.failure(expectedError))
return nil
}

let expectation = XCTestExpectation(
description: "State is updated with socket error"
)

var receivedError: CoreSdkClient.SalemoveError?
monitor.$state
// Drop initial .idle and .updated with listed queues state update
.dropFirst(2)
.sink { state in
print(state)
if case let .failed(error as CoreSdkClient.SalemoveError) = state {
XCTAssertEqual(error, expectedError)
expectation.fulfill()
receivedError = error
}
}
.store(in: &cancellables)

monitor.startMonitoring(queuesIds: [UUID().uuidString])

wait(for: [expectation], timeout: 1.0)
XCTAssertEqual(receivedError, expectedError)
XCTAssertEqual(envCalls, [.listQueues, .subscribeForQueuesUpdates])
}

// MARK: Stop
func test_stopMonitoringError() {
// MARK: Stop monitoring
func test_stopMonitoringReturnError() {
var envCalls: [Call] = []

let expectedError = CoreSdkClient.SalemoveError.mock()

monitor.environment.unsubscribeFromUpdates = { subscriptionId, completion in
completion(expectedError)
}
monitor.environment.listQueues = { completion in
envCalls.append(.listQueues)
completion([], nil)
}
monitor.environment.subscribeForQueuesUpdates = { _, completion in
envCalls.append(.subscribeForQueuesUpdates)
completion(.success(.mock()))
return UUID().uuidString
}
monitor.environment.unsubscribeFromUpdates = { subscriptionId, completion in
envCalls.append(.unsubscribeFromUpdates)
completion(expectedError)
}

monitor.startMonitoring(queuesIds: ["1"])

let expectation = XCTestExpectation(
description: "State is updated with unsubscribe request error"
)
var receivedError: CoreSdkClient.SalemoveError?
monitor.$state
.dropFirst()
.sink { state in
if case .failed(let error as CoreSdkClient.SalemoveError) = state {
XCTAssertEqual(error, expectedError)
expectation.fulfill()
receivedError = error
}
}
.store(in: &cancellables)

monitor.stopMonitoring()

wait(for: [expectation], timeout: 1.0)
XCTAssertEqual(receivedError, expectedError)
XCTAssertEqual(envCalls, [.listQueues, .subscribeForQueuesUpdates, .unsubscribeFromUpdates])
}

func test_stopMonitoringSuccess() {
var envCalls: [Call] = []

monitor.environment.listQueues = { completion in
envCalls.append(.listQueues)
completion([], nil)
}
monitor.environment.subscribeForQueuesUpdates = { _, completion in
envCalls.append(.subscribeForQueuesUpdates)
completion(.success(.mock()))
return UUID().uuidString
}
monitor.environment.unsubscribeFromUpdates = { subscriptionId, completion in
envCalls.append(.unsubscribeFromUpdates)
completion(.mock())
}

monitor.startMonitoring(queuesIds: ["1"])

monitor.stopMonitoring()

XCTAssertEqual(envCalls, [.listQueues, .subscribeForQueuesUpdates, .unsubscribeFromUpdates])
}
}

0 comments on commit bfe23a5

Please sign in to comment.