|
| 1 | +import XCTest |
| 2 | +import TestKit |
| 3 | +import Fakes |
| 4 | + |
| 5 | +@testable import WooCommerce |
| 6 | +@testable import Yosemite |
| 7 | + |
| 8 | +class RemoteOrderSynchronizerTests: XCTestCase { |
| 9 | + |
| 10 | + let sampleSiteID: Int64 = 123 |
| 11 | + let sampleProductID: Int64 = 234 |
| 12 | + let sampleInputID: Int64 = 345 |
| 13 | + |
| 14 | + func test_sending_status_input_updates_local_order() throws { |
| 15 | + // Given |
| 16 | + let stores = MockStoresManager(sessionManager: .testingInstance) |
| 17 | + let synchronizer = RemoteOrderSynchronizer(siteID: sampleSiteID, stores: stores) |
| 18 | + |
| 19 | + // When |
| 20 | + synchronizer.setStatus.send(.completed) |
| 21 | + |
| 22 | + // Then |
| 23 | + XCTAssertEqual(synchronizer.order.status, .completed) |
| 24 | + } |
| 25 | + |
| 26 | + func test_sending_new_product_input_updates_local_order() throws { |
| 27 | + // Given |
| 28 | + let product = Product.fake().copy(productID: sampleProductID) |
| 29 | + let stores = MockStoresManager(sessionManager: .testingInstance) |
| 30 | + let synchronizer = RemoteOrderSynchronizer(siteID: sampleSiteID, stores: stores) |
| 31 | + |
| 32 | + // When |
| 33 | + let input = OrderSyncProductInput(product: .product(product), quantity: 1) |
| 34 | + synchronizer.setProduct.send(input) |
| 35 | + |
| 36 | + // Then |
| 37 | + let item = try XCTUnwrap(synchronizer.order.items.first) |
| 38 | + XCTAssertEqual(item.itemID, input.id) |
| 39 | + XCTAssertEqual(item.productID, product.productID) |
| 40 | + XCTAssertEqual(item.quantity, input.quantity) |
| 41 | + } |
| 42 | + |
| 43 | + func test_sending_update_product_input_updates_local_order() throws { |
| 44 | + // Given |
| 45 | + let product = Product.fake().copy(productID: sampleProductID) |
| 46 | + let stores = MockStoresManager(sessionManager: .testingInstance) |
| 47 | + let synchronizer = RemoteOrderSynchronizer(siteID: sampleSiteID, stores: stores) |
| 48 | + |
| 49 | + // When |
| 50 | + let input = OrderSyncProductInput(id: sampleInputID, product: .product(product), quantity: 1) |
| 51 | + let input2 = OrderSyncProductInput(id: sampleInputID, product: .product(product), quantity: 2) |
| 52 | + synchronizer.setProduct.send(input) |
| 53 | + synchronizer.setProduct.send(input2) |
| 54 | + |
| 55 | + // Then |
| 56 | + let item = try XCTUnwrap(synchronizer.order.items.first) |
| 57 | + XCTAssertEqual(item.itemID, input2.id) |
| 58 | + XCTAssertEqual(item.productID, product.productID) |
| 59 | + XCTAssertEqual(item.quantity, input2.quantity) |
| 60 | + } |
| 61 | + |
| 62 | + func test_sending_delete_product_input_updates_local_order() throws { |
| 63 | + // Given |
| 64 | + let product = Product.fake().copy(productID: sampleProductID) |
| 65 | + let stores = MockStoresManager(sessionManager: .testingInstance) |
| 66 | + let synchronizer = RemoteOrderSynchronizer(siteID: sampleSiteID, stores: stores) |
| 67 | + |
| 68 | + // When |
| 69 | + let input = OrderSyncProductInput(id: sampleInputID, product: .product(product), quantity: 1) |
| 70 | + let input2 = OrderSyncProductInput(id: sampleInputID, product: .product(product), quantity: 0) |
| 71 | + synchronizer.setProduct.send(input) |
| 72 | + synchronizer.setProduct.send(input2) |
| 73 | + |
| 74 | + // Then |
| 75 | + XCTAssertEqual(synchronizer.order.items.count, 0) |
| 76 | + } |
| 77 | + |
| 78 | + func test_sending_addresses_input_updates_local_order() throws { |
| 79 | + // Given |
| 80 | + let address = Address.fake().copy(firstName: "Woo", lastName: "Customer") |
| 81 | + let stores = MockStoresManager(sessionManager: .testingInstance) |
| 82 | + let synchronizer = RemoteOrderSynchronizer(siteID: sampleSiteID, stores: stores) |
| 83 | + |
| 84 | + // When |
| 85 | + let input = OrderSyncAddressesInput(billing: address, shipping: address) |
| 86 | + synchronizer.setAddresses.send(input) |
| 87 | + |
| 88 | + // Then |
| 89 | + XCTAssertEqual(synchronizer.order.billingAddress, address) |
| 90 | + XCTAssertEqual(synchronizer.order.shippingAddress, address) |
| 91 | + } |
| 92 | + |
| 93 | + func test_sending_nil_addresses_input_updates_local_order() throws { |
| 94 | + // Given |
| 95 | + let address = Address.fake().copy(firstName: "Woo", lastName: "Customer") |
| 96 | + let stores = MockStoresManager(sessionManager: .testingInstance) |
| 97 | + let synchronizer = RemoteOrderSynchronizer(siteID: sampleSiteID, stores: stores) |
| 98 | + |
| 99 | + // When |
| 100 | + let input = OrderSyncAddressesInput(billing: address, shipping: address) |
| 101 | + synchronizer.setAddresses.send(input) |
| 102 | + synchronizer.setAddresses.send(nil) |
| 103 | + |
| 104 | + |
| 105 | + // Then |
| 106 | + XCTAssertNil(synchronizer.order.billingAddress) |
| 107 | + XCTAssertNil(synchronizer.order.shippingAddress) |
| 108 | + } |
| 109 | +} |
0 commit comments