-
Notifications
You must be signed in to change notification settings - Fork 194
/
UnknownChannelEvent_Tests.swift
103 lines (90 loc) 路 3.78 KB
/
UnknownChannelEvent_Tests.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
//
// Copyright 漏 2021 Stream.io Inc. All rights reserved.
//
@testable import StreamChat
import XCTest
final class UnknownChannelEvent_Tests: XCTestCase {
func test_unkownEvent_decoding() throws {
// Create event fields
let userId: UserId = .unique
let cid: ChannelId = .unique
let ideaPayload: IdeaEventPayload = .unique
let createdAt: String = "2020-07-16T15:38:10.289007Z"
// Create event JSON
let json = """
{
"user" : {
"id" : "\(userId)",
"banned" : false,
"unread_channels" : 0,
"totalUnreadCount" : 0,
"created_at" : "2019-12-12T15:33:46.488935Z",
"invisible" : false,
"unreadChannels" : 0,
"unread_count" : 0,
"image" : "https://getstream.io/random_svg/?id=broken-waterfall-5&name=Broken+waterfall",
"updated_at" : "2020-07-16T15:38:10.289007Z",
"role" : "user",
"total_unread_count" : 0,
"online" : true,
"name" : "broken-waterfall-5"
},
"channel_type" : "\(cid.type.rawValue)",
"channel_id" : "\(cid.id)",
"cid" : "\(cid.rawValue)",
"created_at" : "\(createdAt)",
"type" : "\(IdeaEventPayload.eventType.rawValue)",
"idea" : "\(ideaPayload.idea)"
}
""".data(using: .utf8)!
// Decode unkown event from JSON.
let unknownEvent = try JSONDecoder.default.decode(UnknownChannelEvent.self, from: json)
let payload = try JSONDecoder.default.decode([String: RawJSON].self, from: json)
// Assert all fields are correct.
XCTAssertEqual(unknownEvent.cid, cid)
XCTAssertEqual(unknownEvent.userId, userId)
XCTAssertEqual(unknownEvent.createdAt, createdAt.toDate())
XCTAssertEqual(unknownEvent.type, IdeaEventPayload.eventType)
XCTAssertEqual(unknownEvent.payload, payload)
}
func test_whenAllFieldsArePresentedAndTypeMatches_customPayloadIsDecoded() throws {
// Create custom event payload.
let payload = IdeaEventPayload.unique
// Create event with `IdeaEventPayload` payload.
let unkownEvent = UnknownChannelEvent(
type: IdeaEventPayload.eventType,
cid: .unique,
userId: .unique,
createdAt: .unique,
payload: ["idea": .string(payload.idea)]
)
// Assert payload is decoded.
XCTAssertEqual(unkownEvent.payload(ofType: IdeaEventPayload.self), payload)
}
func test_whenFieldsAreMissing_customPayloadIsNotDecoded() throws {
// Create event with `IdeaEventPayload` fields missing.
let unkownEvent = UnknownChannelEvent(
type: IdeaEventPayload.eventType,
cid: .unique,
userId: .unique,
createdAt: .unique,
payload: [:]
)
// Assert payload is not decoded because fields are missing.
XCTAssertNil(unkownEvent.payload(ofType: IdeaEventPayload.self))
}
func test_whenAllFieldsArePresentedButTypeDoesNotMatch_customPayloadIsNotDecoded() throws {
// Create random event type.
let randomEventType = EventType(rawValue: .unique)
// Create event with `IdeaEventPayload` fields missing.
let unkownEvent = UnknownChannelEvent(
type: randomEventType,
cid: .unique,
userId: .unique,
createdAt: .unique,
payload: ["idea": .string(.unique)]
)
// Assert payload is not decoded because the type does not match.
XCTAssertNil(unkownEvent.payload(ofType: IdeaEventPayload.self))
}
}