Skip to content

Commit

Permalink
updated tests, made a correct to include h6 tag in view element type.
Browse files Browse the repository at this point in the history
  • Loading branch information
msadoon committed Aug 17, 2023
1 parent b88e19f commit 55d2d28
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 43 deletions.
52 changes: 32 additions & 20 deletions KsApi/lib/HTML Parser/HTMLParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,27 +218,39 @@ final class HTMLParserTests: XCTestCase {
XCTAssertEqual(viewElement.embeddedURLContentHeight, 400)
}

func testHTMLParser_WithTextHeadline_Success() {
let viewElements = self.htmlParser.parse(bodyHtml: HTMLParserTemplates.validHeaderText.data)

guard let viewElement = viewElements.first as? TextViewElement else {
XCTFail("text view element should be created.")

return
}

guard viewElement.components.count == 1 else {
XCTFail()

return
func testHTMLParser_WithTextHeadlines_Success() {
let viewElementsToTextStyleHeaders =
[
HTMLParserTemplates.validHeader1Text.data: TextComponent.TextStyleType.header1,
HTMLParserTemplates.validHeader2Text.data: TextComponent.TextStyleType.header2,
HTMLParserTemplates.validHeader3Text.data: TextComponent.TextStyleType.header3,
HTMLParserTemplates.validHeader4Text.data: TextComponent.TextStyleType.header4,
HTMLParserTemplates.validHeader5Text.data: TextComponent.TextStyleType.header5,
HTMLParserTemplates.validHeader6Text.data: TextComponent.TextStyleType.header6
]

_ = viewElementsToTextStyleHeaders.map { textData, headerStyle in
let viewElements = self.htmlParser.parse(bodyHtml: textData)

guard let viewElement = viewElements.first as? TextViewElement else {
XCTFail("text view element should be created.")

Check warning on line 236 in KsApi/lib/HTML Parser/HTMLParserTests.swift

View check run for this annotation

Codecov / codecov/patch

KsApi/lib/HTML Parser/HTMLParserTests.swift#L236

Added line #L236 was not covered by tests

return

Check warning on line 238 in KsApi/lib/HTML Parser/HTMLParserTests.swift

View check run for this annotation

Codecov / codecov/patch

KsApi/lib/HTML Parser/HTMLParserTests.swift#L238

Added line #L238 was not covered by tests
}

guard viewElement.components.count == 1 else {
XCTFail()

Check warning on line 242 in KsApi/lib/HTML Parser/HTMLParserTests.swift

View check run for this annotation

Codecov / codecov/patch

KsApi/lib/HTML Parser/HTMLParserTests.swift#L242

Added line #L242 was not covered by tests

return

Check warning on line 244 in KsApi/lib/HTML Parser/HTMLParserTests.swift

View check run for this annotation

Codecov / codecov/patch

KsApi/lib/HTML Parser/HTMLParserTests.swift#L244

Added line #L244 was not covered by tests
}

XCTAssertEqual(
viewElement.components[0].text,
"Please participate in helping me finish my film! Just pick a level in the right hand column and click to donate — it only takes a minute."
)
XCTAssertNil(viewElement.components[0].link)
XCTAssertEqual(viewElement.components[0].styles, [headerStyle])
}

XCTAssertEqual(
viewElement.components[0].text,
"Please participate in helping me finish my film! Just pick a level in the right hand column and click to donate — it only takes a minute."
)
XCTAssertNil(viewElement.components[0].link)
XCTAssertEqual(viewElement.components[0].styles, [TextComponent.TextStyleType.header])
}

func testHTMLParser_WithMultipleParagraphsLinksAndStyles_Success() {
Expand Down
58 changes: 54 additions & 4 deletions KsApi/lib/HTML Parser/Templates/HTMLParserTemplates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ public enum HTMLParserTemplates {
case validHiddenVideo
case validIFrame
case validIFrameWithEmbeddedSource
case validHeaderText
case validHeader1Text
case validHeader2Text
case validHeader3Text
case validHeader4Text
case validHeader5Text
case validHeader6Text
case validParagraphTextWithStyles
case validParagraphTextWithLinksAndStyles
case validListWithNestedLinks
Expand Down Expand Up @@ -39,8 +44,18 @@ public enum HTMLParserTemplates {
return self.validExternalSource
case .validIFrameWithEmbeddedSource:
return self.validEmbeddedExternalSource
case .validHeaderText:
return self.validHeaderText
case .validHeader1Text:
return self.validHeader1Text
case .validHeader2Text:
return self.validHeader2Text
case .validHeader3Text:
return self.validHeader3Text
case .validHeader4Text:
return self.validHeader4Text
case .validHeader5Text:
return self.validHeader5Text
case .validHeader6Text:
return self.validHeader6Text
case .validParagraphTextWithLinksAndStyles:
return self.validParagraphTextWithLinksAndStyles
case .validParagraphTextWithStyles:
Expand Down Expand Up @@ -74,13 +89,48 @@ public enum HTMLParserTemplates {
"""
}

private var validHeaderText: String {
private var validHeader1Text: String {
"""
<h1 id=\"h:please-participate-i\" class=\"page-anchor\">Please participate in helping me finish my film! Just pick a level in the right hand column and click to donate — it only takes a minute.</h1>
\n<br>\n
"""
}

private var validHeader2Text: String {
"""
<h2 id=\"h:please-participate-i\" class=\"page-anchor\">Please participate in helping me finish my film! Just pick a level in the right hand column and click to donate — it only takes a minute.</h2>
\n<br>\n
"""
}

private var validHeader3Text: String {
"""
<h3 id=\"h:please-participate-i\" class=\"page-anchor\">Please participate in helping me finish my film! Just pick a level in the right hand column and click to donate — it only takes a minute.</h3>
\n<br>\n
"""
}

private var validHeader4Text: String {
"""
<h4 id=\"h:please-participate-i\" class=\"page-anchor\">Please participate in helping me finish my film! Just pick a level in the right hand column and click to donate — it only takes a minute.</h4>
\n<br>\n
"""
}

private var validHeader5Text: String {
"""
<h5 id=\"h:please-participate-i\" class=\"page-anchor\">Please participate in helping me finish my film! Just pick a level in the right hand column and click to donate — it only takes a minute.</h5>
\n<br>\n
"""
}

private var validHeader6Text: String {
"""
<h6 id=\"h:please-participate-i\" class=\"page-anchor\">Please participate in helping me finish my film! Just pick a level in the right hand column and click to donate — it only takes a minute.</h6>
\n<br>\n
"""
}

private var validAudio: String {
"""
\n
Expand Down
1 change: 1 addition & 0 deletions KsApi/lib/HTML Parser/View Elements/ViewElementType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ enum ViewElementType: String {
TextComponent.TextBlockType.header3.rawValue,
TextComponent.TextBlockType.header4.rawValue,
TextComponent.TextBlockType.header5.rawValue,
TextComponent.TextBlockType.header6.rawValue,
TextComponent.TextBlockType.unorderedList.rawValue,
TextComponent.TextBlockType.orderedList.rawValue,
TextComponent.TextBlockType.paragraph.rawValue:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ internal final class TextElementCellViewModelTests: TestCase {

private let expectedSampleString = "sample attributed string"
private let expectedBaseFontSize: CGFloat = 16.0
private let expectedHeaderFontSize: CGFloat = 20.0
private let expectedHeader1FontSize: CGFloat = 28.0
private let expectedHeader2FontSize: CGFloat = 26.0
private let expectedHeader3FontSize: CGFloat = 24.0
private let expectedHeader4FontSize: CGFloat = 22.0
private let expectedHeader5FontSize: CGFloat = 20.0
private let expectedHeader6FontSize: CGFloat = 18.0
private var expectedBaseFont = UIFont.ksr_body()
private var expectedParagraphStyle = NSMutableParagraphStyle()
private var expectedHeaderFont = UIFont.ksr_body()
Expand All @@ -21,7 +26,6 @@ internal final class TextElementCellViewModelTests: TestCase {
super.setUp()

self.expectedBaseFont = UIFont.ksr_body(size: self.expectedBaseFontSize)
self.expectedHeaderFont = UIFont.ksr_body(size: self.expectedHeaderFontSize).bolded
self.expectedParagraphStyle.minimumLineHeight = 22
self.expectedFontAttributes = [
NSAttributedString.Key.font: self.expectedBaseFont,
Expand Down Expand Up @@ -148,26 +152,37 @@ internal final class TextElementCellViewModelTests: TestCase {
self.bodyText.assertValue(expectedLinkWithNoStylesAttributedText)
}

func testHeaderElement() {
let headerTextComponent = TextComponent(
text: expectedSampleString,
link: nil,
styles: [.header]
)
let headerTextElement = TextViewElement(components: [headerTextComponent])
func testHeaderElements() {
let headerTypesToFontSizes = [
TextComponent.TextStyleType.header1: self.expectedHeader1FontSize,
TextComponent.TextStyleType.header2: self.expectedHeader2FontSize,
TextComponent.TextStyleType.header3: self.expectedHeader3FontSize,
TextComponent.TextStyleType.header4: self.expectedHeader4FontSize,
TextComponent.TextStyleType.header5: self.expectedHeader5FontSize,
TextComponent.TextStyleType.header6: self.expectedHeader6FontSize
]
_ = headerTypesToFontSizes.map { headerType, fontSize in
let headerTextComponent = TextComponent(
text: expectedSampleString,
link: nil,
styles: [headerType]
)
let headerTextElement = TextViewElement(components: [headerTextComponent])

expectedFontAttributes[NSAttributedString.Key.font] = self.expectedHeaderFont
self.expectedFontAttributes[NSAttributedString.Key.foregroundColor] = UIColor.ksr_support_700
self.expectedParagraphStyle.minimumLineHeight = 25
self.expectedFontAttributes[NSAttributedString.Key.paragraphStyle] = self.expectedParagraphStyle
self.expectedHeaderFont = UIFont.ksr_body(size: fontSize).bolded
expectedFontAttributes[NSAttributedString.Key.font] = self.expectedHeaderFont
self.expectedFontAttributes[NSAttributedString.Key.foregroundColor] = UIColor.ksr_support_700
self.expectedParagraphStyle.minimumLineHeight = 25
self.expectedFontAttributes[NSAttributedString.Key.paragraphStyle] = self.expectedParagraphStyle

let expectedHeaderAttributedText = NSAttributedString(
string: expectedSampleString,
attributes: expectedFontAttributes
)
let expectedHeaderAttributedText = NSAttributedString(
string: expectedSampleString,
attributes: expectedFontAttributes
)

self.vm.inputs.configureWith(textElement: headerTextElement)
self.bodyText.assertValue(expectedHeaderAttributedText)
self.vm.inputs.configureWith(textElement: headerTextElement)
self.bodyText.assertLastValue(expectedHeaderAttributedText)
}
}

func testListElement() {
Expand Down

0 comments on commit 55d2d28

Please sign in to comment.