Skip to content

Commit b18dab2

Browse files
Mark Pospeselmpospese
authored andcommitted
[Issue 74] Increase text coverage for TextStyleLabel and TypographyLabelRepresentable
1 parent 90fa200 commit b18dab2

File tree

5 files changed

+148
-47
lines changed

5 files changed

+148
-47
lines changed

Sources/YMatterType/Elements/Representables/TypographyLabelRepresentable.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import SwiftUI
1010

1111
/// A wrapper for a UIKit TypographyLabel view that allows to integrate that view into a SwiftUI view hierarchy.
12-
struct TypographyLabelRepresentable: UIViewRepresentable {
12+
struct TypographyLabelRepresentable {
1313
/// The type of view to present.
1414
typealias UIViewType = TypographyLabel
1515

@@ -21,14 +21,20 @@ struct TypographyLabelRepresentable: UIViewRepresentable {
2121

2222
/// A closure that gets called on the init and refresh of the View
2323
var configureTextStyleLabel: ((TypographyLabel) -> Void)?
24+
}
2425

26+
extension TypographyLabelRepresentable: UIViewRepresentable {
2527
/// Creates the view object and configures its initial state.
2628
///
2729
/// - Parameter context: A context structure containing information about
2830
/// the current state of the system.
2931
///
3032
/// - Returns: `TypographyLabel` view configured with the provided information.
3133
func makeUIView(context: Context) -> TypographyLabel {
34+
getLabel()
35+
}
36+
37+
func getLabel() -> TypographyLabel {
3238
let label = TypographyLabel(typography: typography)
3339
label.text = text
3440

@@ -49,8 +55,12 @@ struct TypographyLabelRepresentable: UIViewRepresentable {
4955
/// - context: A context structure containing information about the current
5056
/// state of the system.
5157
func updateUIView(_ uiView: TypographyLabel, context: Context) {
52-
uiView.typography = typography
53-
uiView.text = text
54-
configureTextStyleLabel?(uiView)
58+
updateLabel(uiView)
59+
}
60+
61+
func updateLabel(_ label: TypographyLabel) {
62+
label.typography = typography
63+
label.text = text
64+
configureTextStyleLabel?(label)
5565
}
5666
}

Sources/YMatterType/Elements/TextStyleLabel.swift

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import SwiftUI
1010

1111
/// A singe line text label that supports `Typography` for SwiftUI.
12-
public struct TextStyleLabel: View {
12+
public struct TextStyleLabel {
1313
/// The text that the label displays.
1414
var text: String
1515

@@ -20,16 +20,6 @@ public struct TextStyleLabel: View {
2020
/// This closure allows you to provide additional configuration to the `TypographyLabel`
2121
var configureTextStyleLabel: ((TypographyLabel) -> Void)?
2222

23-
/// The content and behavior of the view.
24-
public var body: some View {
25-
TypographyLabelRepresentable(
26-
text: text,
27-
typography: typography,
28-
configureTextStyleLabel: configureTextStyleLabel
29-
)
30-
.fixedSize(horizontal: false, vertical: true)
31-
}
32-
3323
/// Initializes a `TextStyleLabel` instance with the specified parameters
3424
/// - Parameters:
3525
/// - text: The text that the label displays
@@ -40,8 +30,24 @@ public struct TextStyleLabel: View {
4030
typography: Typography,
4131
configuration: ((TypographyLabel) -> Void)? = nil
4232
) {
43-
self.typography = typography
4433
self.text = text
34+
self.typography = typography
4535
self.configureTextStyleLabel = configuration
4636
}
4737
}
38+
39+
extension TextStyleLabel: View {
40+
/// The content and behavior of the view.
41+
public var body: some View {
42+
getLabel()
43+
.fixedSize(horizontal: false, vertical: true)
44+
}
45+
46+
func getLabel() -> some View {
47+
TypographyLabelRepresentable(
48+
text: text,
49+
typography: typography,
50+
configureTextStyleLabel: configureTextStyleLabel
51+
)
52+
}
53+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// TypographyLabelRepresentableTests.swift
3+
// YMatterTypeTests
4+
//
5+
// Created by Mark Pospesel on 3/20/23.
6+
// Copyright © 2023 Y Media Labs. All rights reserved.
7+
//
8+
9+
import XCTest
10+
@testable import YMatterType
11+
12+
final class TypographyLabelRepresentableTests: XCTestCase {
13+
enum Constants {
14+
static let helloWorldText = "Hello, World"
15+
static let goodbyeText = "Goodbye!"
16+
static let fontSize = CGFloat.random(in: 18...36)
17+
static let textColor: UIColor = .systemPurple
18+
}
19+
20+
func test_getLabel_deliversLabel() {
21+
// Given
22+
let text = Constants.helloWorldText
23+
let typography: Typography = .smallBody.fontSize(Constants.fontSize)
24+
let textColor = Constants.textColor
25+
let sut = TypographyLabelRepresentable(text: text, typography: typography) { label in
26+
label.textColor = textColor
27+
}
28+
29+
// When
30+
let label = sut.getLabel()
31+
32+
// Then
33+
XCTAssertEqual(label.text, text)
34+
XCTAssertTypographyEqual(label.typography, typography)
35+
XCTAssertEqual(label.numberOfLines, 1)
36+
XCTAssertEqual(label.textColor, textColor)
37+
}
38+
39+
func test_updateLabel_updatesLabel() {
40+
// Given
41+
let text = Constants.goodbyeText
42+
let typography: Typography = .bodyBold.fontSize(Constants.fontSize)
43+
let textColor = Constants.textColor
44+
var sut = TypographyLabelRepresentable(text: Constants.helloWorldText, typography: .smallBody)
45+
let label = sut.getLabel()
46+
XCTAssertNotEqual(label.text, text)
47+
XCTAssertNotEqual(label.typography.fontSize, typography.fontSize)
48+
XCTAssertNotEqual(label.typography.fontWeight, typography.fontWeight)
49+
XCTAssertNotEqual(label.textColor, textColor)
50+
51+
// When
52+
sut.text = text
53+
sut.typography = typography
54+
sut.configureTextStyleLabel = { label in
55+
label.textColor = textColor
56+
}
57+
sut.updateLabel(label)
58+
59+
// Then
60+
XCTAssertEqual(label.text, text)
61+
XCTAssertTypographyEqual(label.typography, typography)
62+
XCTAssertEqual(label.textColor, textColor)
63+
}
64+
}

Tests/YMatterTypeTests/Elements/TextStyleLabelTests.swift

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ final class TextStyleLabelTests: XCTestCase {
1515
static let fontSize = CGFloat.random(in: 10...60)
1616
}
1717

18-
func testTextStyleLabelSingleLine() throws {
19-
let fontSize = Constants.fontSize
20-
let expectedTypography = Typography.systemLabel.fontSize(fontSize)
18+
func testTextStyleLabelSingleLine() {
2119
let expectedText = Constants.helloWorldText
20+
let expectedTypography = Typography.systemLabel.fontSize(Constants.fontSize)
2221

2322
// Given a TextStyleLabel with a single line of text
2423
let sut = TextStyleLabel(
@@ -36,8 +35,8 @@ final class TextStyleLabelTests: XCTestCase {
3635
XCTAssertEqual(sut.text, expectedText)
3736

3837
// we expect the font to match the expected
39-
XCTAssertEqual(sut.typography.fontSize, fontSize)
40-
38+
XCTAssertTypographyEqual(sut.typography, expectedTypography)
39+
4140
// we expect the configuration closusure to update the label
4241
let labelToUpdate = TypographyLabel(typography: expectedTypography)
4342
XCTAssertNotEqual(labelToUpdate.lineBreakMode, .byTruncatingMiddle)
@@ -46,34 +45,21 @@ final class TextStyleLabelTests: XCTestCase {
4645
XCTAssertEqual(labelToUpdate.lineBreakMode, .byTruncatingMiddle)
4746
}
4847

49-
func testTypographyLabelRepresentable() throws {
50-
let fontSize = Constants.fontSize
51-
52-
let expectedTypography = Typography.systemLabel.fontSize(fontSize)
48+
func test_getLabel_deliversRepresentable() throws {
5349
let expectedText = Constants.helloWorldText
50+
let expectedTypography = Typography.systemLabel.fontSize(Constants.fontSize)
5451

55-
// Given a TypographyLabelRepresentable with a single line of text
56-
let sut = TypographyLabelRepresentable(
57-
text: expectedText,
58-
typography: expectedTypography,
59-
configureTextStyleLabel: { (label: TypographyLabel) in
60-
label.textColor = .yellow
61-
}
52+
// Given a TextStyleLabel with a single line of text
53+
let sut = TextStyleLabel(
54+
expectedText,
55+
typography: expectedTypography
6256
)
63-
// we expect a value
64-
XCTAssertNotNil(sut)
65-
66-
// we expect the text to match the expected
67-
XCTAssertEqual(sut.text, expectedText)
68-
69-
// we expect the font to match the expected
70-
XCTAssertEqual(sut.typography.fontSize, fontSize)
71-
72-
// we expect the configuration closusure to update the label
73-
let labelToUpdate = TypographyLabel(typography: expectedTypography)
74-
XCTAssertNotEqual(labelToUpdate.textColor, .yellow)
75-
76-
sut.configureTextStyleLabel?(labelToUpdate)
77-
XCTAssertEqual(labelToUpdate.textColor, .yellow)
57+
58+
// When
59+
let label = try XCTUnwrap(sut.getLabel() as? TypographyLabelRepresentable)
60+
61+
// Then
62+
XCTAssertEqual(label.text, expectedText)
63+
XCTAssertTypographyEqual(label.typography, expectedTypography)
7864
}
7965
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// XCTestCase+TypographyEquatable.swift
3+
// YMatterTypeTests
4+
//
5+
// Created by Mark Pospesel on 3/21/23.
6+
// Copyright © 2023 Y Media Labs. All rights reserved.
7+
//
8+
9+
import XCTest
10+
@testable import YMatterType
11+
12+
extension XCTestCase {
13+
/// Compares two typographies and asserts if they are not equal.
14+
///
15+
/// `Typography` does not conform to `Equatable` because `fontFamily` is just a protocol
16+
/// that is itself not `Equatable`. We can however compare some properties of `FontFamily` as
17+
/// well as all the other properties of `Typography`.
18+
/// - Parameters:
19+
/// - lhs: the first typography to compare
20+
/// - rh2: the second typography to compare
21+
func XCTAssertTypographyEqual(_ lhs: Typography, _ rh2: Typography) {
22+
XCTAssertEqual(lhs.fontFamily.familyName, rh2.fontFamily.familyName)
23+
XCTAssertEqual(lhs.fontFamily.fontNameSuffix, rh2.fontFamily.fontNameSuffix)
24+
XCTAssertEqual(lhs.fontFamily.supportedWeights, rh2.fontFamily.supportedWeights)
25+
XCTAssertEqual(lhs.fontWeight, rh2.fontWeight)
26+
XCTAssertEqual(lhs.fontSize, rh2.fontSize)
27+
XCTAssertEqual(lhs.lineHeight, rh2.lineHeight)
28+
XCTAssertEqual(lhs.letterSpacing, rh2.letterSpacing)
29+
XCTAssertEqual(lhs.paragraphIndent, rh2.paragraphIndent)
30+
XCTAssertEqual(lhs.paragraphSpacing, rh2.paragraphSpacing)
31+
XCTAssertEqual(lhs.textDecoration, rh2.textDecoration)
32+
XCTAssertEqual(lhs.textStyle, rh2.textStyle)
33+
XCTAssertEqual(lhs.isFixed, rh2.isFixed)
34+
}
35+
}

0 commit comments

Comments
 (0)