@@ -139,4 +139,90 @@ class PurchasesVirtualCurrenciesTests: BasePurchasesTests {
139139 expect ( self . mockVirtualCurrencyManager. cachedVirtualCurrenciesCallCount) . to ( equal ( 1 ) )
140140 expect ( Thread . isMainThread) . to ( beTrue ( ) )
141141 }
142+
143+ // MARK: - spendVirtualCurrencies() Tests
144+
145+ func testSpendVirtualCurrenciesThrowsUnsupportedErrorWhenIAMIsNotEnabled( ) async throws {
146+ // `self.tokenManager` defaults to disabled in `BasePurchasesTests.setUpWithError()`.
147+ var thrown : Error ?
148+ do {
149+ _ = try await self . purchases. spendVirtualCurrencies ( amounts: [ " GLD " : 10 ] , reference: nil )
150+ } catch {
151+ thrown = error
152+ }
153+
154+ expect ( thrown) . to ( matchError ( ErrorCode . unsupportedError) )
155+ expect ( self . mockVirtualCurrencyManager. spendVirtualCurrenciesCalled) . to ( beFalse ( ) )
156+ }
157+
158+ func testSpendVirtualCurrencyThrowsUnsupportedErrorWhenIAMIsNotEnabled( ) async throws {
159+ var thrown : Error ?
160+ do {
161+ _ = try await self . purchases. spendVirtualCurrency ( code: " GLD " , amount: 10 , reference: nil )
162+ } catch {
163+ thrown = error
164+ }
165+
166+ expect ( thrown) . to ( matchError ( ErrorCode . unsupportedError) )
167+ expect ( self . mockVirtualCurrencyManager. spendVirtualCurrenciesCalled) . to ( beFalse ( ) )
168+ }
169+
170+ func testSpendVirtualCurrenciesForwardsSuccessWhenIAMIsEnabled( ) async throws {
171+ self . tokenManager = MockTokenManager ( enabled: true )
172+ self . setupPurchases ( )
173+ self . mockVirtualCurrencyManager. stubbedSpendVirtualCurrenciesResult = . success( Self . mockVirtualCurrencies)
174+
175+ let vcs = try await self . purchases. spendVirtualCurrencies ( amounts: [ " GLD " : 10 ] , reference: " ref-1 " )
176+
177+ expect ( vcs) . to ( equal ( Self . mockVirtualCurrencies) )
178+ expect ( self . mockVirtualCurrencyManager. spendVirtualCurrenciesCalled) . to ( beTrue ( ) )
179+ expect ( self . mockVirtualCurrencyManager. spendVirtualCurrenciesCallCount) . to ( equal ( 1 ) )
180+ expect ( self . mockVirtualCurrencyManager. invokedSpendVirtualCurrenciesParametersList. first? . amounts)
181+ == [ " GLD " : 10 ]
182+ expect ( self . mockVirtualCurrencyManager. invokedSpendVirtualCurrenciesParametersList. first? . reference)
183+ == " ref-1 "
184+ }
185+
186+ func testSpendVirtualCurrenciesForwardsErrorWhenIAMIsEnabled( ) async throws {
187+ self . tokenManager = MockTokenManager ( enabled: true )
188+ self . setupPurchases ( )
189+ let backendError : BackendError = . networkError( . offlineConnection( ) )
190+ self . mockVirtualCurrencyManager. stubbedSpendVirtualCurrenciesResult = . failure( backendError)
191+
192+ do {
193+ _ = try await self . purchases. spendVirtualCurrencies ( amounts: [ " GLD " : 10 ] , reference: nil )
194+ fail ( " An error should have been thrown " )
195+ } catch {
196+ expect ( error) . to ( matchError ( backendError. asPurchasesError) )
197+ }
198+ }
199+
200+ func testSpendVirtualCurrencyConvenienceMethodForwardsSingleAmountWhenIAMIsEnabled( ) async throws {
201+ self . tokenManager = MockTokenManager ( enabled: true )
202+ self . setupPurchases ( )
203+ self . mockVirtualCurrencyManager. stubbedSpendVirtualCurrenciesResult = . success( Self . mockVirtualCurrencies)
204+
205+ let vcs = try await self . purchases. spendVirtualCurrency ( code: " GLD " , amount: 25 , reference: " ref-2 " )
206+
207+ expect ( vcs) . to ( equal ( Self . mockVirtualCurrencies) )
208+ expect ( self . mockVirtualCurrencyManager. spendVirtualCurrenciesCallCount) . to ( equal ( 1 ) )
209+ expect ( self . mockVirtualCurrencyManager. invokedSpendVirtualCurrenciesParametersList. first? . amounts)
210+ == [ " GLD " : 25 ]
211+ expect ( self . mockVirtualCurrencyManager. invokedSpendVirtualCurrenciesParametersList. first? . reference)
212+ == " ref-2 "
213+ }
214+
215+ func testSpendVirtualCurrencyConvenienceMethodForwardsErrorWhenIAMIsEnabled( ) async throws {
216+ self . tokenManager = MockTokenManager ( enabled: true )
217+ self . setupPurchases ( )
218+ let backendError : BackendError = . networkError( . offlineConnection( ) )
219+ self . mockVirtualCurrencyManager. stubbedSpendVirtualCurrenciesResult = . failure( backendError)
220+
221+ do {
222+ _ = try await self . purchases. spendVirtualCurrency ( code: " GLD " , amount: 25 , reference: nil )
223+ fail ( " An error should have been thrown " )
224+ } catch {
225+ expect ( error) . to ( matchError ( backendError. asPurchasesError) )
226+ }
227+ }
142228}
0 commit comments