BUYLineItemTest.m 3.67 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 29 30 31 32 33 34
//
//  BUYLineItemTest.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;
#import <Buy/Buy.h>

@interface BUYLineItemTest : XCTestCase
@end

@implementation BUYLineItemTest {
35
	BUYProductVariant *_variant;
36
	BUYLineItem *_lineItem;
37
	BUYModelManager *_modelManager;
38 39 40 41 42
}

- (void)setUp
{
	[super setUp];
43 44 45 46 47 48 49 50 51

	_modelManager = [BUYModelManager modelManager];
	_variant = [[BUYProductVariant alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @1, @"requires_shipping" : @YES }];
	_lineItem = [[BUYLineItem alloc] initWithVariant:_variant];
}

- (void)tearDown
{
	_modelManager = nil;
52 53 54 55 56
}

- (void)testInitRespectsVariantShippingFlag
{
	XCTAssertTrue([[_lineItem requiresShipping] boolValue]);
57 58
	_variant.requiresShipping = @NO;
	XCTAssertTrue([[_lineItem requiresShipping] boolValue]);
59 60 61 62 63 64 65 66
}

#pragma mark - Serialization Tests

- (void)testJsonDictionaryShouldHaveSaneDefaults
{
	NSDictionary *json = [_lineItem jsonDictionaryForCheckout];
	XCTAssertNotNil(json);
67 68
	XCTAssertEqualObjects(@"0", json[@"price"]);
	XCTAssertEqualObjects(@"1", json[@"quantity"]);
69 70 71 72
}

- (void)testJsonDictionaryDoesntIncludeVariantsWithoutIds
{
73
	_lineItem = [_modelManager lineItemWithVariant:nil];
74 75 76 77 78 79 80
	NSDictionary *json = [_lineItem jsonDictionaryForCheckout];
	XCTAssertNotNil(json);
	XCTAssertNil(json[@"variant_id"]);
}

- (void)testJsonDictionaryShouldShowAllProperties
{
81
	BUYProductVariant *variant = [[BUYProductVariant alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @5 }];
82 83 84 85 86 87 88
	_lineItem = [[BUYLineItem alloc] initWithVariant:variant];
	_lineItem.quantity = [NSDecimalNumber decimalNumberWithString:@"3"];
	_lineItem.price = [NSDecimalNumber decimalNumberWithString:@"5.55"];
	_lineItem.title = @"banana";
	
	NSDictionary *json = [_lineItem jsonDictionaryForCheckout];
	XCTAssertEqualObjects(@5, json[@"variant_id"]);
89 90
	XCTAssertEqualObjects(@"3", json[@"quantity"]);
	XCTAssertEqualObjects(@"5.55", json[@"price"]);
91 92 93 94 95
	XCTAssertEqualObjects(@"banana", json[@"title"]);
}

- (void)testUpdatingFromJsonShouldUpdateAllValues
{
96
	BUYLineItem *lineItem = [[BUYLineItem alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @"5", @"price" : @"5.99", @"quantity" : @5, @"requires_shipping" : @YES, @"title" : @"banana" }];
97
	XCTAssertEqualObjects(@"5", lineItem.identifier);
98 99 100 101 102 103 104
	XCTAssertEqualObjects([NSDecimalNumber decimalNumberWithString:@"5.99"], lineItem.price);
	XCTAssertEqualObjects([NSDecimalNumber decimalNumberWithString:@"5"], lineItem.quantity);
	XCTAssertEqualObjects(@"banana", lineItem.title);
	XCTAssertTrue([[lineItem requiresShipping] boolValue]);
}

@end