-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFilePickerTests.swift
115 lines (98 loc) · 3.8 KB
/
FilePickerTests.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
@testable import Coder_Desktop
@testable import CoderSDK
import Mocker
import SwiftUI
import Testing
import ViewInspector
@MainActor
@Suite(.timeLimit(.minutes(1)))
struct FilePickerTests {
let mockResponse: LSResponse
init() {
mockResponse = LSResponse(
absolute_path: ["/"],
absolute_path_string: "/",
contents: [
LSFile(name: "home", absolute_path_string: "/home", is_dir: true),
LSFile(name: "tmp", absolute_path_string: "/tmp", is_dir: true),
LSFile(name: "etc", absolute_path_string: "/etc", is_dir: true),
LSFile(name: "README.md", absolute_path_string: "/README.md", is_dir: false),
]
)
}
@Test
func testLoadError() async throws {
let host = "test-error.coder"
let sut = FilePicker(host: host, outputAbsPath: .constant(""))
let view = sut
let url = URL(string: "http://\(host):4")!
let errorMessage = "Connection failed"
Mock(
url: url.appendingPathComponent("/api/v0/list-directory"),
contentType: .json,
statusCode: 500,
data: [.post: errorMessage.data(using: .utf8)!]
).register()
try await ViewHosting.host(view) {
try await sut.inspection.inspect { view in
try #expect(await eventually { @MainActor in
let text = try view.find(ViewType.Text.self)
return try text.string().contains("Connection failed")
})
}
}
}
@Test
func testSuccessfulFileLoad() async throws {
let host = "test-success.coder"
let sut = FilePicker(host: host, outputAbsPath: .constant(""))
let view = sut
let url = URL(string: "http://\(host):4")!
try Mock(
url: url.appendingPathComponent("/api/v0/list-directory"),
statusCode: 200,
data: [.post: Client.encoder.encode(mockResponse)]
).register()
try await ViewHosting.host(view) {
try await sut.inspection.inspect { view in
try #expect(await eventually { @MainActor in
_ = try view.find(ViewType.List.self)
return true
})
_ = try view.find(text: "README.md")
_ = try view.find(text: "home")
let selectButton = try view.find(button: "Select")
#expect(selectButton.isDisabled())
}
}
}
@Test
func testDirectoryExpansion() async throws {
let host = "test-expansion.coder"
let sut = FilePicker(host: host, outputAbsPath: .constant(""))
let view = sut
let url = URL(string: "http://\(host):4")!
try Mock(
url: url.appendingPathComponent("/api/v0/list-directory"),
statusCode: 200,
data: [.post: Client.encoder.encode(mockResponse)]
).register()
try await ViewHosting.host(view) {
try await sut.inspection.inspect { view in
try #expect(await eventually { @MainActor in
_ = try view.find(ViewType.List.self)
return true
})
let disclosureGroup = try view.find(ViewType.DisclosureGroup.self)
#expect(view.findAll(ViewType.DisclosureGroup.self).count == 3)
try disclosureGroup.expand()
// Disclosure group should expand out to 3 more directories
#expect(await eventually { @MainActor in
return view.findAll(ViewType.DisclosureGroup.self).count == 6
})
}
}
}
// TODO: The writing of more extensive tests is blocked by ViewInspector,
// as it can't select an item in a list...
}