BUYCheckoutTest.m 6.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
//
//  BUYCheckoutTest.m
//  Mobile Buy SDK
//
//  Created by Shopify.
//  Copyright (c) 2015 Shopify Inc. All rights reserved.
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//

@import UIKit;
@import XCTest;
29

30
#import <Buy/Buy.h>
31 32
#import "BUYCheckout.h"

33 34 35 36
@interface BUYCheckoutTest : XCTestCase
@end

@implementation BUYCheckoutTest {
37
	BUYModelManager *_modelManager;
38 39 40 41 42 43 44 45 46
	BUYCheckout *_checkout;
	BUYCart *_cart;
	BUYProductVariant *_variant;
	NSDictionary *_discountDictionary;
}

- (void)setUp
{
	[super setUp];
47 48 49 50
	_modelManager = [BUYModelManager modelManager];
	_cart = [_modelManager insertCartWithJSONDictionary:nil];
	_checkout = [_modelManager checkoutWithCart:_cart];
	_variant = [[BUYProductVariant alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @1 }];
51 52 53 54 55
	_discountDictionary = @{ @"code" : @"abcd1234", @"amount" : @"5.00", @"applicable" : @true };
}

- (void)testOrderStatusDeserializationWithInvalidURL
{
56
	BUYCheckout *checkout = [[BUYCheckout alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"order" : @{ @"status_url" : @"NOT REAL" } }];
57 58 59 60 61
	XCTAssertNil(checkout.order.statusURL);
}

- (void)testOrderStatusDeserializationWithValidURL
{
62
	BUYCheckout *checkout = [[BUYCheckout alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"order" : @{ @"status_url" : @"http://www.shopify.com/" } }];
63 64 65 66 67
	XCTAssertNotNil(checkout.order.statusURL);
}

- (void)testOrderStatusDeserializationWithNoURL
{
68
	BUYCheckout *checkout = [[BUYCheckout alloc] initWithModelManager:_modelManager JSONDictionary:@{}];
69 70 71 72 73 74
	XCTAssertNil(checkout.order.statusURL);
}

- (void)testInitWithCartAddsLineItems
{
	[_cart addVariant:_variant];
75
	BUYCheckout *checkout = [[BUYCheckout alloc] initWithModelManager:_modelManager cart:_cart];
76 77 78 79
	XCTAssertEqual([[checkout lineItems] count], [[_cart lineItems] count]);
	XCTAssertTrue([checkout isDirty]);
}

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
- (void)testCheckoutWithVariant
{
	BUYCheckout *checkout = [_modelManager checkoutWithVariant:_variant];
	XCTAssertNotNil(checkout);
	XCTAssertGreaterThanOrEqual([checkout.lineItems count], 1);
	BUYLineItem *lineItem = checkout.lineItems[0];
	XCTAssertEqual(_variant.identifier, lineItem.variantId);
}

- (void)testSettingAShippingRateMarksShippingRateIdAsDirty
{
	BUYShippingRate *shippingRate = [[BUYShippingRate alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @"banana" }];
	XCTAssertNil(_checkout.shippingRate);
	XCTAssertNil(_checkout.shippingRateId);
	_checkout.shippingRate = shippingRate;
	XCTAssertEqualObjects(@"banana", _checkout.shippingRateId);
	
	XCTAssertTrue([[_checkout dirtyProperties] containsObject:@"shippingRateId"]);
}

100 101
- (void)testDirtyPropertiesAreReturnedInJSON
{
102
	BUYShippingRate *shippingRate = [[BUYShippingRate alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @"banana" }];
103 104 105 106 107 108
	[_checkout markAsClean];
	
	_checkout.shippingRate = shippingRate;
	_checkout.currency = @"BANANA";
	NSSet *dirtyProperties = [_checkout dirtyProperties];
	XCTAssertTrue([dirtyProperties containsObject:@"currency"]);
109
	XCTAssertTrue([dirtyProperties containsObject:@"shippingRateId"]);
110 111 112 113
	XCTAssertTrue([dirtyProperties containsObject:@"shippingRate"]);
	
	NSDictionary *json = [_checkout jsonDictionaryForCheckout];
	XCTAssertEqualObjects(json[@"checkout"][@"currency"], @"BANANA");
114
	XCTAssertEqualObjects(json[@"checkout"][@"shipping_rate_id"], @"banana");
115 116 117 118
}

- (void)testRequiresShippingAndIncludesTaxesSerialization
{
119 120
	_checkout.requiresShippingValue = YES;
	_checkout.includesTaxesValue = YES;
121 122 123 124 125 126 127
	NSDictionary *jsonDictionary = [_checkout jsonDictionaryForCheckout][@"checkout"];
	XCTAssertEqualObjects(@YES, jsonDictionary[@"requires_shipping"]);
	XCTAssertEqualObjects(@YES, jsonDictionary[@"taxes_included"]);
}

- (void)testDiscountDeserialization
{
128
	BUYDiscount *discount = [[BUYDiscount alloc] initWithModelManager:_modelManager JSONDictionary: _discountDictionary];
129 130
	XCTAssertEqualObjects(@"abcd1234", discount.code);
	XCTAssertEqualObjects(@5.00, discount.amount);
131
	XCTAssertEqual(true, discount.applicableValue);
132 133 134 135 136
}

- (void)testDiscountSerialization
{
	NSDictionary *jsonDict = @{ @"code": @"abcd1234" };
137
	BUYDiscount *discount = [[BUYDiscount alloc] initWithModelManager:_modelManager JSONDictionary:_discountDictionary];
138 139 140 141 142
	XCTAssertEqualObjects(jsonDict, [discount jsonDictionaryForCheckout]);
}

- (void)testHasToken
{
143
	BUYCheckout *checkout = [[BUYCheckout alloc] initWithModelManager:_modelManager cart:_cart];
144 145 146 147 148 149 150 151 152 153
	checkout.token = nil;
	XCTAssertFalse([checkout hasToken]);
	checkout.token = @"";
	XCTAssertFalse([checkout hasToken]);
	checkout.token = @"banana";
	XCTAssertTrue([checkout hasToken]);
}

- (void)testEmptyCheckoutsDoNotRequireShipping
{
154
	_checkout = [[BUYCheckout alloc] initWithModelManager:_modelManager JSONDictionary:@{}];
155 156 157 158 159
	XCTAssertFalse([_checkout requiresShipping]);
}

- (void)testCheckoutsWithoutItemsThatRequireShipping
{
160
	_checkout = [[BUYCheckout alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"requires_shipping" : @1 }];
161 162 163 164 165
	XCTAssertTrue([_checkout requiresShipping]);
}

- (void)testTaxLineDeserialization
{
166 167 168
	BUYTaxLine *taxLine = [[BUYTaxLine alloc] initWithModelManager:_modelManager JSONDictionary:@{@"price": @"0.29",
																								  @"rate": @"0.13",
																								  @"title": @"HST"}];
169 170 171 172 173 174 175
	XCTAssertEqualObjects(@0.29, taxLine.price);
	XCTAssertEqualObjects(@0.13, taxLine.rate);
	XCTAssertEqualObjects(@"HST", taxLine.title);
	
}

@end