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
@testable import Grain
import SwiftUI
import XCTest
/// The screens presented as sheets or pushes from the feed and story viewer.
/// None of them was reachable from the suite, and between them they are a large
/// share of the app's view code.
@MainActor
final class SheetRenderTests: GrainTestCase {
private var account: TestAccount!
/// Async overrides: the synchronous `setUp`/`tearDown` are nonisolated, so
/// touching main-actor state from them warns.
override func setUp() async throws {
try await super.setUp()
account = TestAccount()
}
override func tearDown() async throws {
MockURLProtocol.handler = nil
account.restore()
try await super.tearDown()
}
// MARK: - Sign in
func testRendersLogin() {
let env = TestEnvironment(authenticated: false)
ViewRender.render(LoginView().withTestEnvironment(env))
}
// MARK: - Camera feed
func testRendersCameraFeed() {
MockURLProtocol.respondWithJSON(Fixtures.feed)
let env = TestEnvironment()
ViewRender.render(
CameraFeedView(client: env.client, camera: "Fujifilm X100V").withTestEnvironment(env)
)
}
func testRendersCameraFeedWhenEmpty() {
MockURLProtocol.respondWithJSON(#"{"items": [], "cursor": null}"#)
let env = TestEnvironment()
ViewRender.render(
CameraFeedView(client: env.client, camera: "Leica M6").withTestEnvironment(env)
)
}
// MARK: - Story creation
func testRendersStoryCreate() {
MockURLProtocol.respondWithJSON("{}")
let env = TestEnvironment()
ViewRender.render(
StoryCreateView(client: env.client, onCreated: {}).withTestEnvironment(env)
)
}
// MARK: - Gallery comment sheet
/// The sheet owns its own view model and loads the thread on appear, so a
/// render covers both the loading and the loaded branches.
func testRendersGalleryCommentSheet() {
MockURLProtocol.handler = { request in
let body = request.url!.path.contains("getGallery")
? Fixtures.galleryResponse
: Fixtures.commentThread
let response = HTTPURLResponse(url: request.url!, statusCode: 200, httpVersion: nil, headerFields: nil)!
return (Data(body.utf8), response)
}
let env = TestEnvironment()
ViewRender.render(
CommentSheetView(
client: env.client,
galleryUri: "at://did:plc:test/social.grain.gallery/1",
onProfileTap: { _ in },
onHashtagTap: { _ in },
onCommentCountChanged: { _ in }
)
.withTestEnvironment(env),
settle: 0.2
)
}
func testRendersGalleryCommentSheetWhenTheThreadFailsToLoad() {
MockURLProtocol.respondWithError(statusCode: 500)
let env = TestEnvironment()
ViewRender.render(
CommentSheetView(
client: env.client,
galleryUri: "at://did:plc:test/social.grain.gallery/1"
)
.withTestEnvironment(env)
)
}
// MARK: - Story comment sheet
func testRendersStoryCommentSheet() {
MockURLProtocol.respondWithJSON(Fixtures.commentThread)
let env = TestEnvironment()
let viewModel = StoryCommentsViewModel(client: env.client)
ViewRender.render(
StoryCommentSheet(
viewModel: viewModel,
storyUri: "at://did:plc:test/social.grain.story/1",
client: env.client,
onProfileTap: { _ in },
onDismiss: {}
)
.withTestEnvironment(env),
settle: 0.2
)
}
/// Opening the sheet from the comment button focuses the input, which puts
/// the keyboard accessory and the mention strip on screen.
func testRendersStoryCommentSheetWithTheInputFocused() {
MockURLProtocol.respondWithJSON(#"{"comments": [], "cursor": null}"#)
let env = TestEnvironment()
ViewRender.render(
StoryCommentSheet(
viewModel: StoryCommentsViewModel(client: env.client),
storyUri: "at://did:plc:test/social.grain.story/1",
client: env.client,
focusInput: true
)
.withTestEnvironment(env),
settle: 0.2
)
}
// MARK: - Comment presenter
/// Without `configure` the presenter has no environment to hand the sheet,
/// so it must decline rather than present a sheet with no auth in it.
func testTheCommentPresenterDeclinesToOpenBeforeItIsConfigured() {
let env = TestEnvironment()
let presenter = StoryCommentPresenter()
presenter.open(
storyUri: "at://did:plc:test/social.grain.story/1",
focusInput: false,
commentsViewModel: StoryCommentsViewModel(client: env.client),
client: env.client
)
XCTAssertNil(presenter.presentedStoryUri)
}
/// Closing something that was never opened has to be harmless — the story
/// viewer calls it on teardown regardless.
func testClosingAnUnopenedCommentPresenterIsHarmless() {
let presenter = StoryCommentPresenter()
presenter.close()
XCTAssertNil(presenter.presentedStoryUri)
}
}