Skip to content

Commit

Permalink
✅ Update tests to verify error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaatttt committed Oct 26, 2023
1 parent 36c9c05 commit c826eb0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
8 changes: 8 additions & 0 deletions Tests/AppcuesKitTests/Actions/ActionRegistryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ class ActionRegistryTests: XCTestCase {

var appcues: MockAppcues!
var actionRegistry: ActionRegistry!
var logger: DebugLogger!

override func setUpWithError() throws {
logger = DebugLogger(previousLogger: nil)
appcues = MockAppcues()
appcues.config.logger = logger
actionRegistry = ActionRegistry(container: appcues.container)
}

Expand Down Expand Up @@ -91,6 +94,11 @@ class ActionRegistryTests: XCTestCase {
interactionType: "Button Tapped",
viewDescription: "My Button")
waitForExpectations(timeout: 1)

XCTAssertEqual(logger.log.count, 1)
let log = try XCTUnwrap(logger.log.first)
XCTAssertEqual(log.level, .error)
XCTAssertEqual(log.message, "Action of type @test/action is already registered.")
}

func testQueueExecution() throws {
Expand Down
15 changes: 11 additions & 4 deletions Tests/AppcuesKitTests/Networking/DynamicCodingKeysTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
import XCTest
@testable import AppcuesKit

@available(iOS 13.0, *)
class DynamicCodingKeysTests: XCTestCase {

var encoder: JSONEncoder = {
let encoder = JSONEncoder()
if #available(iOS 11.0, *) {
encoder.outputFormatting = .sortedKeys
}
encoder.outputFormatting = .sortedKeys
return encoder
}()

Expand All @@ -29,6 +28,7 @@ class DynamicCodingKeysTests: XCTestCase {
// Assert
let asString = try XCTUnwrap(String(data: data, encoding: .utf8))
XCTAssertEqual(asString, #"{"dict":{"bool":true,"double":3.14,"int":42,"number":1,"string":"value"}}"#)
XCTAssertEqual(testData.logger.log.count, 0)
}

func testEncodeDictAnyInvalid() throws {
Expand All @@ -42,11 +42,18 @@ class DynamicCodingKeysTests: XCTestCase {
// Assert
let asString = try XCTUnwrap(String(data: data, encoding: .utf8))
XCTAssertEqual(asString, #"{"dict":{"before":1,"valid":"Valid value"}}"#)
XCTAssertEqual(testData.logger.log.count, 1)
let log = try XCTUnwrap(testData.logger.log.first)
XCTAssertEqual(log.level, .error)
XCTAssertEqual(log.message, #"Unsupported value(s) included in dict when encoding key(s): ["anotherInvalid", "invalid"].\#nThese keys have been omitted. Only String, Number, Date, URL and Bool types allowed."#)
}
}

@available(iOS 13.0, *)
extension DynamicCodingKeysTests {
struct TestData: Encodable {
let logger = DebugLogger(previousLogger: nil)

let dict: [String: Any]

enum CodingKeys: CodingKey {
Expand All @@ -56,7 +63,7 @@ extension DynamicCodingKeysTests {
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
var dictContainer = container.nestedContainer(keyedBy: DynamicCodingKeys.self, forKey: .dict)
try dictContainer.encodeSkippingInvalid(dict)
try dictContainer.encodeSkippingInvalid(dict, logger: logger)
}
}
}
8 changes: 8 additions & 0 deletions Tests/AppcuesKitTests/Traits/TraitRegistryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ class TraitRegistryTests: XCTestCase {

var appcues: MockAppcues!
var traitRegistry: TraitRegistry!
var logger: DebugLogger!

override func setUpWithError() throws {
logger = DebugLogger(previousLogger: nil)
appcues = MockAppcues()
appcues.config.logger = logger
traitRegistry = TraitRegistry(container: appcues.container)
}

Expand Down Expand Up @@ -57,6 +60,11 @@ class TraitRegistryTests: XCTestCase {
XCTAssertFalse(successfullyRegisteredTrait2)
let traitInstances = traitRegistry.instances(for: [traitModel], level: .group, renderContext: .modal)
XCTAssertEqual(traitInstances.count, 1)

XCTAssertEqual(logger.log.count, 1)
let log = try XCTUnwrap(logger.log.first)
XCTAssertEqual(log.level, .error)
XCTAssertEqual(log.message, "Trait of type @test/trait is already registered.")
}
}

Expand Down

0 comments on commit c826eb0

Please sign in to comment.