-
Notifications
You must be signed in to change notification settings - Fork 0
/
QminderEventsTests.swift
207 lines (170 loc) · 6.6 KB
/
QminderEventsTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// QminderEventsTests.swift
// QminderAPITests
//
// Created by Kristaps Grinbergs on 10/05/2018.
// Copyright © 2018 Kristaps Grinbergs. All rights reserved.
//
import XCTest
@testable import QminderAPI
class QminderEventsMock: QminderEventsProtocol {
weak var delegate: QminderEventsDelegate?
func openSocket() {
delegate?.onConnected()
}
func reOpenSocket() {
delegate?.onConnected()
}
func closeSocket() {
let error = NSError(domain: "FakeError", code: 1, userInfo: [:])
delegate?.onDisconnected(error: error)
}
func subscribe(toTicketEvent eventType: TicketWebsocketEvent,
parameters: [String: Any],
callback: @escaping (Result<Ticket, QminderError>) -> Void) {
let ticket = Ticket(statusCode: 200,
id: "1",
line: 1,
source: .name,
status: .new,
firstName: "Name",
lastName: "Surname",
phoneNumber: 123456789,
email: "test@example.com",
created: Created.init(date: Date()),
served: nil,
labels: [Label(color: "black", value: "blackLabel")],
extra: [Extra(title: "Title", value: "value", url: "https:///www.google.com")],
orderAfter: nil,
interactions: nil)
callback(Result(ticket))
}
func subscribe(toDeviceEvent eventType: DeviceWebsocketEvent,
parameters: [String: Any],
callback: @escaping (Result<TVDevice?, QminderError>) -> Void) {
let settings = Settings.init(selectedLine: 1,
lines: [1, 2, 3],
clearTickets: .afterCalling,
notificationViewLineVisible: false)
let tvDevice = TVDevice(statusCode: 200,
id: 1,
name: "Device",
theme: "standard",
layout: "standard",
settings: settings,
features: ["FEATURE"])
callback(Result(tvDevice))
}
func subscribe(toLineEvent eventType: LineWebsocketEvent,
parameters: [String: Any],
callback: @escaping (Result<[Line], QminderError>) -> Void) {
let lines = [Line(statusCode: 200, id: 1, name: "Line1", location: 1),
Line(statusCode: 200, id: 2, name: "Line2", location: 1),
Line(statusCode: 200, id: 3, name: "Line3", location: 1)]
callback(Result(lines))
}
}
class QminderEventsTests: XCTestCase {
private var qminderEvents: QminderEventsProtocol?
private var isConnected = false
private var error: Error?
override func setUp() {
qminderEvents = QminderEventsMock()
qminderEvents?.delegate = self
qminderEvents?.openSocket()
}
func testConnected() {
XCTAssertTrue(isConnected)
}
func testClosed() {
qminderEvents?.closeSocket()
XCTAssertFalse(isConnected)
let nsError = self.error as NSError?
XCTAssertNotNil(nsError)
XCTAssertEqual(nsError?.domain, "FakeError")
XCTAssertEqual(nsError?.code, 1)
}
func testOverviewMonitorChange() {
let overviewMonitorChangeExpectation = self.expectation(description: "overviewMonitorChange")
qminderEvents?.subscribe(toDeviceEvent: .overviewMonitorChange, parameters: ["id": "1"]) { result in
XCTAssertNil(result.error)
switch result {
case .failure(let error):
print("fail \(error)")
case let .success(device):
XCTAssertNotNil(device)
XCTAssertEqual(device?.id, 1)
XCTAssertEqual(device?.name, "Device")
XCTAssertEqual(device?.layout, "standard")
XCTAssertEqual(device?.theme, "standard")
let settings = device?.settings
XCTAssertEqual(settings?.selectedLine, 1)
XCTAssertEqual(settings?.lines, [1, 2, 3])
XCTAssertEqual(settings?.clearTickets, .afterCalling)
overviewMonitorChangeExpectation.fulfill()
}
}
self.waitForExpectations(timeout: 1.0, handler: nil)
}
func testLineEvent() {
let lineEventExpectation = self.expectation(description: "lineEvent")
qminderEvents?.subscribe(toLineEvent: .changed, parameters: ["id": 1]) { result in
switch result {
case .failure(let error):
print("fail \(error)")
case let .success(lines):
XCTAssertEqual(lines.count, 3)
let firstLine = lines.first
XCTAssertEqual(firstLine?.id, 1)
XCTAssertEqual(firstLine?.name, "Line1")
XCTAssertEqual(firstLine?.location, 1)
let lastLine = lines.last
XCTAssertEqual(lastLine?.id, 3)
XCTAssertEqual(lastLine?.name, "Line3")
XCTAssertEqual(lastLine?.location, 1)
lineEventExpectation.fulfill()
}
}
self.waitForExpectations(timeout: 1.0, handler: nil)
}
func testTicketEvent() {
let ticketCreatedExpectation = self.expectation(description: "ticketCreated")
qminderEvents?.subscribe(toTicketEvent: .created, parameters: ["location": 1]) { result in
switch result {
case .failure(let error):
print("fail \(error)")
case let .success(ticket):
XCTAssertEqual(ticket.id, "1")
XCTAssertEqual(ticket.firstName, "Name")
XCTAssertEqual(ticket.lastName, "Surname")
XCTAssertEqual(ticket.source, .name)
XCTAssertEqual(ticket.status, .new)
XCTAssertEqual(ticket.phoneNumber, 123456789)
XCTAssertNotNil(ticket.created.date)
XCTAssertNotNil(ticket.labels)
XCTAssertEqual(ticket.labels?.count, 1)
let firstLabel = ticket.labels?.first
XCTAssertEqual(firstLabel?.color, "black")
XCTAssertEqual(firstLabel?.value, "blackLabel")
XCTAssertNotNil(ticket.extra)
XCTAssertEqual(ticket.extra?.count, 1)
let firstExtra = ticket.extra?.first
XCTAssertEqual(firstExtra?.title, "Title")
XCTAssertEqual(firstExtra?.value, "value")
XCTAssertEqual(firstExtra?.url, "https:///www.google.com")
ticketCreatedExpectation.fulfill()
}
}
self.waitForExpectations(timeout: 1.0, handler: nil)
}
}
extension QminderEventsTests: QminderEventsDelegate {
func onConnected() {
isConnected = true
}
func onDisconnected(error: Error?) {
print(error ?? "")
self.error = error
isConnected = false
}
}