Skip to content

Commit c68e9d0

Browse files
committed
test shadow properties are set irrespective of useShadowPath
1 parent dc710ce commit c68e9d0

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

Sources/YCoreUI/Components/Elevation.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public struct Elevation {
1919
/// The color of the layer’s shadow.
2020
public let color: UIColor
2121
/// The opacity of the layer’s shadow.
22-
public let opacity: CGFloat
22+
public let opacity: Float
2323
/// Flag to set shadow path. Default is `true`.
2424
public let useShadowPath: Bool
2525

@@ -36,7 +36,7 @@ public struct Elevation {
3636
blur: CGFloat,
3737
spread: CGFloat,
3838
color: UIColor,
39-
opacity: CGFloat,
39+
opacity: Float,
4040
useShadowPath: Bool = true
4141
) {
4242
self.offset = offset
@@ -52,9 +52,18 @@ public struct Elevation {
5252
/// - layer: layer of the corresponding view
5353
/// - cornerRadius: the radius to use when drawing rounded corners for the layer’s background.
5454
public func apply(layer: CALayer, cornerRadius: CGFloat) {
55-
guard useShadowPath else { return }
55+
applyShadow(layer: layer)
5656

57-
let rect = layer.bounds.insetBy(dx: -spread, dy: -spread)
58-
layer.shadowPath = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius).cgPath
57+
if useShadowPath {
58+
let rect = layer.bounds.insetBy(dx: -spread, dy: -spread)
59+
layer.shadowPath = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius).cgPath
60+
}
61+
}
62+
63+
private func applyShadow(layer: CALayer) {
64+
layer.shadowOpacity = opacity
65+
layer.shadowColor = color.cgColor
66+
layer.shadowOffset = offset
67+
layer.shadowRadius = blur / 2
5968
}
6069
}

Tests/YCoreUITests/Components/ElevationTests.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,32 @@ final class ElevationTests: XCTestCase {
5252

5353
XCTAssertNotNil(layer.shadowPath)
5454
}
55+
56+
func test_apply_setsShadowPropertiesWhenUseShadowPathIsFalse() {
57+
let sut = makeSUT(useShadowPath: false)
58+
59+
let layer = CALayer()
60+
61+
sut.apply(layer: layer, cornerRadius: 8)
62+
63+
XCTAssertEqual(layer.shadowOffset, CGSize(width: 1, height: 1))
64+
XCTAssertEqual(layer.shadowColor, UIColor.red.cgColor)
65+
XCTAssertEqual(layer.shadowOpacity, 5)
66+
XCTAssertEqual(layer.shadowRadius, sut.blur / 2)
67+
}
68+
69+
func test_apply_setsShadowPropertiesWhenUseShadowPathIsTrue() {
70+
let sut = makeSUT(useShadowPath: true)
71+
72+
let layer = CALayer()
73+
74+
sut.apply(layer: layer, cornerRadius: 8)
75+
76+
XCTAssertEqual(layer.shadowOffset, CGSize(width: 1, height: 1))
77+
XCTAssertEqual(layer.shadowColor, UIColor.red.cgColor)
78+
XCTAssertEqual(layer.shadowOpacity, 5)
79+
XCTAssertEqual(layer.shadowRadius, sut.blur / 2)
80+
}
5581
}
5682

5783
private extension ElevationTests {
@@ -60,7 +86,7 @@ private extension ElevationTests {
6086
blur: CGFloat = 2,
6187
spread: CGFloat = 3,
6288
color: UIColor = .red,
63-
opacity: CGFloat = 5,
89+
opacity: Float = 5,
6490
useShadowPath: Bool = true
6591
) -> Elevation {
6692
Elevation(

0 commit comments

Comments
 (0)