|
| 1 | +/* |
| 2 | + MIT License |
| 3 | + |
| 4 | + Copyright 2023 - Present, Shopify Inc. |
| 5 | + |
| 6 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + of this software and associated documentation files (the "Software"), to deal |
| 8 | + in the Software without restriction, including without limitation the rights |
| 9 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + copies of the Software, and to permit persons to whom the Software is |
| 11 | + furnished to do so, subject to the following conditions: |
| 12 | + |
| 13 | + The above copyright notice and this permission notice shall be included in all |
| 14 | + copies or substantial portions of the Software. |
| 15 | + |
| 16 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +import Foundation |
| 25 | +import XCTest |
| 26 | +@testable import ShopifyCheckoutKit |
| 27 | +@testable import react_native_shopify_checkout_kit |
| 28 | + |
| 29 | +class ShopifyCheckoutKitTests: XCTestCase { |
| 30 | + private var shopifyCheckoutKit: RCTShopifyCheckoutKit! |
| 31 | + |
| 32 | + override func setUp() { |
| 33 | + super.setUp() |
| 34 | + shopifyCheckoutKit = getShopifyCheckoutKit() |
| 35 | + resetShopifyCheckoutKitDefaults() |
| 36 | + } |
| 37 | + |
| 38 | + override func tearDown() { |
| 39 | + shopifyCheckoutKit = nil |
| 40 | + super.tearDown() |
| 41 | + } |
| 42 | + |
| 43 | + private func resetShopifyCheckoutKitDefaults() { |
| 44 | + ShopifyCheckoutKit.configuration.preloading = Configuration.Preloading(enabled: true) |
| 45 | + ShopifyCheckoutKit.configuration.colorScheme = .automatic |
| 46 | + } |
| 47 | + |
| 48 | + private func getShopifyCheckoutKit() -> RCTShopifyCheckoutKit { |
| 49 | + return RCTShopifyCheckoutKit() |
| 50 | + } |
| 51 | + |
| 52 | + /// getConfig |
| 53 | + func testReturnsDefaultConfig() { |
| 54 | + // Call getConfig and capture the result |
| 55 | + var result: [String: Any]? |
| 56 | + shopifyCheckoutKit.getConfig({ config in result = config as? [String: Any] }, reject: { _, _, _ in }) |
| 57 | + |
| 58 | + // Verify that getConfig returned the expected result |
| 59 | + XCTAssertEqual(result?["preloading"] as? Bool, true) |
| 60 | + XCTAssertEqual(result?["colorScheme"] as? String, "automatic") |
| 61 | + } |
| 62 | + |
| 63 | + /// configure |
| 64 | + func testConfigure() { |
| 65 | + let configuration: [AnyHashable: Any] = [ |
| 66 | + "preloading": true, |
| 67 | + "colorScheme": "dark", |
| 68 | + "colors": [ |
| 69 | + "ios": [ |
| 70 | + "spinnerColor": "#FF0000", |
| 71 | + "backgroundColor": "#0000FF" |
| 72 | + ] |
| 73 | + ] |
| 74 | + ] |
| 75 | + |
| 76 | + shopifyCheckoutKit.configure(configuration) |
| 77 | + |
| 78 | + XCTAssertTrue(ShopifyCheckoutKit.configuration.preloading.enabled) |
| 79 | + XCTAssertEqual(ShopifyCheckoutKit.configuration.colorScheme, .dark) |
| 80 | + XCTAssertEqual(ShopifyCheckoutKit.configuration.spinnerColor, UIColor(hex: "#FF0000")) |
| 81 | + XCTAssertEqual(ShopifyCheckoutKit.configuration.backgroundColor, UIColor(hex: "#0000FF")) |
| 82 | + } |
| 83 | + |
| 84 | + func testConfigureWithPartialConfig() { |
| 85 | + let configuration: [AnyHashable: Any] = [ |
| 86 | + "preloading": false |
| 87 | + ] |
| 88 | + |
| 89 | + shopifyCheckoutKit.configure(configuration) |
| 90 | + |
| 91 | + XCTAssertFalse(ShopifyCheckoutKit.configuration.preloading.enabled) |
| 92 | + } |
| 93 | + |
| 94 | + func testConfigureWithInvalidColors() { |
| 95 | + let configuration: [AnyHashable: Any] = [ |
| 96 | + "colors": [ |
| 97 | + "ios": [ |
| 98 | + "spinnerColor": "invalid" |
| 99 | + ] |
| 100 | + ] |
| 101 | + ] |
| 102 | + |
| 103 | + let defaultColorFallback = UIColor(red: 0, green: 0, blue: 0, alpha: 1) |
| 104 | + shopifyCheckoutKit.configure(configuration) |
| 105 | + |
| 106 | + XCTAssertEqual(ShopifyCheckoutKit.configuration.spinnerColor, defaultColorFallback) |
| 107 | + } |
| 108 | + |
| 109 | + /// checkoutDidComplete |
| 110 | + func testCheckoutDidCompleteSendsEvent() { |
| 111 | + let mock = mockSendEvent(eventName: "completed") |
| 112 | + |
| 113 | + mock.startObserving() |
| 114 | + mock.checkoutDidComplete() |
| 115 | + |
| 116 | + XCTAssertTrue(mock.didSendEvent) |
| 117 | + } |
| 118 | + |
| 119 | + /// checkoutDidCancel |
| 120 | + func testCheckoutDidCancelSendsEvent() { |
| 121 | + let mock = mockAsyncSendEvent(eventName: "close") |
| 122 | + |
| 123 | + let expectation = self.expectation(description: "CheckoutDidCancel") |
| 124 | + |
| 125 | + mock.sendEventImplementation = { name, _ in |
| 126 | + if name == "close" { |
| 127 | + mock.didSendEvent = true |
| 128 | + expectation.fulfill() |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + mock.startObserving() |
| 133 | + mock.checkoutDidCancel() |
| 134 | + |
| 135 | + // Wait for the expectation to be fulfilled |
| 136 | + waitForExpectations(timeout: 1, handler: nil) |
| 137 | + |
| 138 | + XCTAssertTrue(mock.didSendEvent) |
| 139 | + } |
| 140 | + |
| 141 | + /// checkoutDidFail |
| 142 | + func testCheckoutDidFailSendsEvent() { |
| 143 | + let mock = mockSendEvent(eventName: "error") |
| 144 | + |
| 145 | + mock.startObserving() |
| 146 | + let error = CheckoutError.checkoutExpired(message: "Checkout expired") |
| 147 | + mock.checkoutDidFail(error: error) |
| 148 | + |
| 149 | + XCTAssertTrue(mock.didSendEvent) |
| 150 | + if let eventBody = mock.eventBody as? [String: Any], let message = eventBody["message"] as? String { |
| 151 | + XCTAssertEqual(message, error.localizedDescription) |
| 152 | + } else { |
| 153 | + XCTFail("Failed to get the message from eventBody") |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private func mockSendEvent(eventName: String) -> RCTShopifyCheckoutKitMock { |
| 158 | + let mock = RCTShopifyCheckoutKitMock() |
| 159 | + mock.eventName = eventName |
| 160 | + return mock |
| 161 | + } |
| 162 | + |
| 163 | + private func mockAsyncSendEvent(eventName: String) -> AsyncRCTShopifyCheckoutKitMock { |
| 164 | + let mock = AsyncRCTShopifyCheckoutKitMock() |
| 165 | + mock.eventName = eventName |
| 166 | + return mock |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +class RCTShopifyCheckoutKitMock: RCTShopifyCheckoutKit { |
| 171 | + var didSendEvent = false |
| 172 | + var eventName: String? |
| 173 | + var eventBody: Any! |
| 174 | + |
| 175 | + override func sendEvent(withName name: String!, body: Any!) { |
| 176 | + if name == self.eventName { |
| 177 | + didSendEvent = true |
| 178 | + eventBody = body |
| 179 | + } |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +class AsyncRCTShopifyCheckoutKitMock: RCTShopifyCheckoutKit { |
| 184 | + var didSendEvent = false |
| 185 | + var eventName: String? |
| 186 | + var sendEventImplementation: ((String?, Any?) -> Void)? |
| 187 | + |
| 188 | + override func sendEvent(withName name: String!, body: Any!) { |
| 189 | + sendEventImplementation?(name, body) |
| 190 | + } |
| 191 | +} |
0 commit comments