Skip to content

Commit bff177a

Browse files
committed
Restore the mocked magic item provider after each snapshot
1 parent 1ab1335 commit bff177a

1 file changed

Lines changed: 31 additions & 19 deletions

File tree

Tests/App/Settings/MagicItemCustomizationView.test.swift

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,8 @@ import Testing
1111
/// there is still only one thing to tap.
1212
@MainActor
1313
struct MagicItemCustomizationViewTests {
14-
init() {
15-
Current.magicItemProvider = { MagicItemCustomizationMagicItemProvider() }
16-
}
17-
1814
@Test func widgetOffersTapAndIconTapBehaviors() {
19-
assertLightDarkSnapshots(
20-
of: view(context: .widget, item: .init(id: "light.kitchen", serverId: "1", type: .entity)),
21-
drawHierarchyInKeyWindow: true
22-
)
15+
assertSnapshots(context: .widget, item: .init(id: "light.kitchen", serverId: "1", type: .entity))
2316
}
2417

2518
/// The behavior that needs more than a name — a navigation path — brings its own row along, for
@@ -28,24 +21,43 @@ struct MagicItemCustomizationViewTests {
2821
var item = MagicItem(id: "light.kitchen", serverId: "1", type: .entity)
2922
item.tapAction = .navigate("/lovelace/0")
3023
item.action = .moreInfoDialog
31-
assertLightDarkSnapshots(
32-
of: view(context: .widget, item: item),
33-
drawHierarchyInKeyWindow: true
34-
)
24+
assertSnapshots(context: .widget, item: item)
3525
}
3626

3727
/// An app icon shortcut is a single action — there is no tile, so there is no second half.
3828
@Test func appIconShortcutKeepsASingleBehavior() {
39-
assertLightDarkSnapshots(
40-
of: view(context: .appIconShortcut, item: .init(id: "script.morning", serverId: "1", type: .script)),
41-
drawHierarchyInKeyWindow: true
29+
assertSnapshots(
30+
context: .appIconShortcut,
31+
item: .init(id: "script.morning", serverId: "1", type: .script)
4232
)
4333
}
4434

45-
private func view(context: MagicItemAddView.Context, item: MagicItem) -> some View {
46-
NavigationView {
47-
MagicItemCustomizationView(mode: .edit, context: context, item: item) { _ in }
48-
}
35+
/// `Current` is a shared global, so the mocked provider is put back as soon as the snapshot is
36+
/// taken — left in place it would follow whichever suite runs next.
37+
private func assertSnapshots(
38+
context: MagicItemAddView.Context,
39+
item: MagicItem,
40+
fileID: StaticString = #fileID,
41+
filePath: StaticString = #filePath,
42+
testName: String = #function,
43+
line: UInt = #line,
44+
column: UInt = #column
45+
) {
46+
let previousProvider = Current.magicItemProvider
47+
Current.magicItemProvider = { MagicItemCustomizationMagicItemProvider() }
48+
defer { Current.magicItemProvider = previousProvider }
49+
50+
assertLightDarkSnapshots(
51+
of: NavigationView {
52+
MagicItemCustomizationView(mode: .edit, context: context, item: item) { _ in }
53+
},
54+
drawHierarchyInKeyWindow: true,
55+
fileID: fileID,
56+
file: filePath,
57+
testName: testName,
58+
line: line,
59+
column: column
60+
)
4961
}
5062
}
5163

0 commit comments

Comments
 (0)