From e10e50efbeb3df292165ada917d7bc34d6c04e46 Mon Sep 17 00:00:00 2001 From: "Alyssa.Yu" Date: Fri, 23 Feb 2024 09:52:26 -0800 Subject: [PATCH] test: add unit test for plugin --- Tests/AmplitudeTests/AmplitudeTests.swift | 35 +++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/Tests/AmplitudeTests/AmplitudeTests.swift b/Tests/AmplitudeTests/AmplitudeTests.swift index de7cc66f..8e106abd 100644 --- a/Tests/AmplitudeTests/AmplitudeTests.swift +++ b/Tests/AmplitudeTests/AmplitudeTests.swift @@ -82,6 +82,41 @@ final class AmplitudeTests: XCTestCase { XCTAssertEqual(lastEvent?.language!.isEmpty, false) } + func testFilterAndEnrichmentPlugin() { + let apiKey = "testFilterAndEnrichmentPlugin" + let enrichedEventType = "Enriched Event" + storageTest = TestPersistentStorage(storagePrefix: "storage-\(apiKey)") + let amplitude = Amplitude(configuration: Configuration( + apiKey: apiKey, + storageProvider: storageTest + )) + + class TestFilterAndEnrichmentPlugin: EnrichmentPlugin { + override func execute(event: BaseEvent) -> BaseEvent? { + if event.eventType == "Enriched Event" { + if event.eventProperties == nil { + event.eventProperties = [:] + } + event.eventProperties!["testPropertyKey"] = "testPropertyValue" + return event + } + return nil + } + } + let testPlugin = TestFilterAndEnrichmentPlugin() + amplitude.add(plugin: testPlugin) + amplitude.track(event: BaseEvent(eventType: enrichedEventType)) + amplitude.track(event: BaseEvent(eventType: "Other Event")) + + let events = storageTest.events() + XCTAssertEqual(events[0].eventType, enrichedEventType) + XCTAssertEqual(getDictionary(events[0].eventProperties!), [ + "testPropertyKey": "testPropertyValue" + ]) + + XCTAssertEqual(events.count, 1) + } + func testContextWithDisableTrackingOptions() { let apiKey = "testApiKeyForDisableTrackingOptions" let trackingOptions = TrackingOptions()