Skip to content

Commit 890d634

Browse files
committed
Add snapshot test for markdown images
1 parent 1e09715 commit 890d634

File tree

8 files changed

+61
-4
lines changed

8 files changed

+61
-4
lines changed

Package.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ let package = Package(
5555
"MarkdownUI",
5656
"SnapshotTesting",
5757
],
58-
exclude: ["__Snapshots__"]
58+
exclude: [
59+
"__Snapshots__",
60+
"__Fixtures__",
61+
]
5962
),
6063
]
6164
)

Sources/MarkdownUI/Shared/Markdown+Environment.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#if canImport(SwiftUI) && !os(watchOS)
22

3+
import CombineSchedulers
34
import SwiftUI
45

56
@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)
@@ -28,6 +29,11 @@
2829
get { self[MarkdownStyleKey.self] }
2930
set { self[MarkdownStyleKey.self] = newValue }
3031
}
32+
33+
var markdownScheduler: AnySchedulerOf<DispatchQueue> {
34+
get { self[MarkdownSchedulerKey.self] }
35+
set { self[MarkdownSchedulerKey.self] = newValue }
36+
}
3137
}
3238

3339
@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)
@@ -40,4 +46,9 @@
4046
static let defaultValue: MarkdownStyle = DefaultMarkdownStyle(font: .system(.body))
4147
}
4248

49+
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
50+
private struct MarkdownSchedulerKey: EnvironmentKey {
51+
static let defaultValue: AnySchedulerOf<DispatchQueue> = .main
52+
}
53+
4354
#endif

Sources/MarkdownUI/Shared/Markdown.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
@Environment(\.markdownBaseURL) private var markdownBaseURL: URL?
4747
@Environment(\.markdownStyle) private var markdownStyle: MarkdownStyle
4848
@Environment(\.networkImageLoader) private var imageLoader
49-
@Environment(\.networkImageScheduler) private var imageScheduler
49+
@Environment(\.markdownScheduler) private var markdownScheduler
5050

5151
@ObservedObject private var store: MarkdownStore
5252

@@ -71,7 +71,7 @@
7171
.onAppear(
7272
environment: .init(
7373
imageLoader: imageLoader,
74-
mainQueue: imageScheduler,
74+
mainQueue: markdownScheduler,
7575
baseURL: markdownBaseURL,
7676
writingDirection: NSWritingDirection(layoutDirection: layoutDirection),
7777
alignment: NSTextAlignment(

Tests/MarkdownUITests/Fixtures.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#if !os(macOS) && !targetEnvironment(macCatalyst)
2+
3+
import UIKit
4+
5+
enum Fixtures {
6+
static let anyImageURL = URL(string: "https://picsum.photos/id/237/200/300")!
7+
8+
// Photo by André Spieker (https://unsplash.com/@andrespieker)
9+
static let anyImage = UIImage(
10+
data: try! Data(contentsOf: fixtureURL("puppy.jpg"))
11+
)!
12+
}
13+
14+
private func fixtureURL(_ fileName: String, file: StaticString = #file) -> URL {
15+
URL(fileURLWithPath: "\(file)", isDirectory: false)
16+
.deletingLastPathComponent()
17+
.appendingPathComponent("__Fixtures__")
18+
.appendingPathComponent(fileName)
19+
}
20+
21+
#endif

Tests/MarkdownUITests/MarkdownTests.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#if canImport(SwiftUI) && !os(macOS) && !targetEnvironment(macCatalyst)
22

3+
import Combine
34
import SnapshotTesting
45
import SwiftUI
56
import XCTest
67

7-
import MarkdownUI
8+
@testable import MarkdownUI
89

910
@available(iOS 14.0, tvOS 14.0, *)
1011
final class MarkdownTests: XCTestCase {
@@ -332,6 +333,27 @@
332333

333334
assertSnapshot(matching: view, as: .image(layout: layout), named: platformName)
334335
}
336+
337+
func testImage() {
338+
let view = TestView(
339+
#"""
340+
If you want to embed images, this is how you do it:
341+
342+
![Puppy](https://picsum.photos/id/237/200/300)
343+
344+
Photo by André Spieker.
345+
"""#
346+
)
347+
.networkImageLoader(
348+
.mock(
349+
url: Fixtures.anyImageURL,
350+
withResponse: Just(Fixtures.anyImage).setFailureType(to: Error.self)
351+
)
352+
)
353+
.environment(\.markdownScheduler, .immediate)
354+
355+
assertSnapshot(matching: view, as: .image(layout: layout), named: platformName)
356+
}
335357
}
336358

337359
#endif
11.3 KB
Loading
521 KB
Loading
185 KB
Loading

0 commit comments

Comments
 (0)