Skip to content

Commit

Permalink
[DownView] Pass DownOptions to init and update (#148)
Browse files Browse the repository at this point in the history
* add DownOptions to init and update methods - closes #147

* add test for DownOptions init and update
  • Loading branch information
ladislas authored and iwasrobbed committed May 15, 2019
1 parent cb9ee72 commit 2d64aae
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Source/Views/DownView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ open class DownView: WKWebView {
/// - configuration: Optional custom web view configuration.
/// - didLoadSuccessfully: Optional callback for when the web content has loaded successfully
/// - Throws: `DownErrors` depending on the scenario
public init(frame: CGRect, markdownString: String, openLinksInBrowser: Bool = true, templateBundle: Bundle? = nil, configuration: WKWebViewConfiguration? = nil, didLoadSuccessfully: DownViewClosure? = nil) throws {
public init(frame: CGRect, markdownString: String, openLinksInBrowser: Bool = true, templateBundle: Bundle? = nil, configuration: WKWebViewConfiguration? = nil, options: DownOptions = .default, didLoadSuccessfully: DownViewClosure? = nil) throws {
self.didLoadSuccessfully = didLoadSuccessfully
self.options = options

if let templateBundle = templateBundle {
self.bundle = templateBundle
Expand Down Expand Up @@ -66,19 +67,20 @@ open class DownView: WKWebView {
/// - markdownString: A string containing CommonMark Markdown
/// - didLoadSuccessfully: Optional callback for when the web content has loaded successfully
/// - Throws: `DownErrors` depending on the scenario
public func update(markdownString: String, didLoadSuccessfully: DownViewClosure? = nil) throws {
public func update(markdownString: String, options: DownOptions? = nil, didLoadSuccessfully: DownViewClosure? = nil) throws {
// Note: As the init method takes this callback already, we only overwrite it here if
// a non-nil value is passed in
if let didLoadSuccessfully = didLoadSuccessfully {
self.didLoadSuccessfully = didLoadSuccessfully
}

try loadHTMLView(markdownString)
try loadHTMLView(markdownString, options: options)
}

// MARK: - Private Properties

let bundle: Bundle
let options: DownOptions

private lazy var baseURL: URL = {
return self.bundle.url(forResource: "index", withExtension: "html")!
Expand All @@ -100,8 +102,8 @@ open class DownView: WKWebView {

private extension DownView {

func loadHTMLView(_ markdownString: String) throws {
let htmlString = try markdownString.toHTML()
func loadHTMLView(_ markdownString: String, options: DownOptions? = nil) throws {
let htmlString = try markdownString.toHTML(options ?? self.options)
let pageHTMLString = try htmlFromTemplate(htmlString)

#if os(iOS)
Expand Down
88 changes: 88 additions & 0 deletions Tests/DownViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,94 @@ class DownViewTests: XCTestCase {
}
}

func testInstantiationWithDownOptionsDefault() {
let expect1 = expectation(description: "DownView sets the html and validates the html is correct")
var downView: DownView?
downView = try? DownView(frame: .zero, markdownString: "## [Down](https://github.com/iwasrobbed/Down)\n\n<strong>I'm strong!</strong>", didLoadSuccessfully: {
self._pageContents(for: downView!) { (htmlString) in
XCTAssertTrue(htmlString!.contains("css/down.min.css"))
XCTAssertTrue(htmlString!.contains("https://github.com/iwasrobbed/Down"))
XCTAssertFalse(htmlString!.contains("<strong>I'm strong!</strong>"))

expect1.fulfill()
}
XCTAssertTrue(downView?.options == .default)
})

waitForExpectations(timeout: 10) { (error: Error?) in
if let error = error {
XCTFail("waitForExpectationsWithTimeout errored: \(error)")
}
}
}

func testInstantiationWithDownOptionsUnsafe() {
let expect1 = expectation(description: "DownView sets the html and validates the html is correct")
var downView: DownView?
downView = try? DownView(frame: .zero, markdownString: "## [Down](https://github.com/iwasrobbed/Down)\n\n<strong>I'm strong!</strong>", options: .unsafe, didLoadSuccessfully: {
self._pageContents(for: downView!) { (htmlString) in
XCTAssertTrue(htmlString!.contains("css/down.min.css"))
XCTAssertTrue(htmlString!.contains("https://github.com/iwasrobbed/Down"))
XCTAssertTrue(htmlString!.contains("<strong>I'm strong!</strong>"))

expect1.fulfill()
}
XCTAssertTrue(downView?.options == .unsafe)
})

waitForExpectations(timeout: 10) { (error: Error?) in
if let error = error {
XCTFail("waitForExpectationsWithTimeout errored: \(error)")
}
}
}

func testUpdateMarkdownStringWithDownOptionsDefault() {
let expect1 = expectation(description: "DownView sets the html and validates the html is correct")
var downView: DownView?
downView = try? DownView(frame: .zero, markdownString: "")

try? downView?.update(markdownString: "## [Down](https://github.com/iwasrobbed/Down)\n\n<strong>I'm strong!</strong>", didLoadSuccessfully: {
self._pageContents(for: downView!) { (htmlString) in
XCTAssertTrue(htmlString!.contains("css/down.min.css"))
XCTAssertTrue(htmlString!.contains("https://github.com/iwasrobbed/Down"))
XCTAssertFalse(htmlString!.contains("<strong>I'm strong!</strong>"))

expect1.fulfill()
}
XCTAssertTrue(downView?.options == .default)
})

waitForExpectations(timeout: 10) { (error: Error?) in
if let error = error {
XCTFail("waitForExpectationsWithTimeout errored: \(error)")
}
}
}

func testUpdateMarkdownStringWithDownOptionsUnsafe() {
let expect1 = expectation(description: "DownView sets the html and validates the html is correct")
var downView: DownView?
downView = try? DownView(frame: .zero, markdownString: "")

try? downView?.update(markdownString: "## [Down](https://github.com/iwasrobbed/Down)\n\n<strong>I'm strong!</strong>", options: .unsafe, didLoadSuccessfully: {
self._pageContents(for: downView!) { (htmlString) in
XCTAssertTrue(htmlString!.contains("css/down.min.css"))
XCTAssertTrue(htmlString!.contains("https://github.com/iwasrobbed/Down"))
XCTAssertTrue(htmlString!.contains("<strong>I'm strong!</strong>"))

expect1.fulfill()
}
XCTAssertTrue(downView?.options == .default)
})

waitForExpectations(timeout: 10) { (error: Error?) in
if let error = error {
XCTFail("waitForExpectationsWithTimeout errored: \(error)")
}
}
}

@available(iOS 11.0, macOS 10.13, *)
func testCustomURLSchemeHandler() {
let mockURLScheme = "down"
Expand Down

0 comments on commit 2d64aae

Please sign in to comment.