Skip to content

Commit 0118cee

Browse files
authored
Merge pull request #6555 from woocommerce/issue/6508-order-creation-yosemite-tests
Order Creation: Add test coverage for order-related gaps in the Yosemite layer
2 parents 95e7e6c + 605bde0 commit 0118cee

File tree

3 files changed

+106
-2
lines changed

3 files changed

+106
-2
lines changed

WooCommerce/Classes/ViewRelated/Orders/Order Creation/Synchronizer/ShippingInputTransformer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct ShippingInputTransformer {
1111
// If input is `nil`, then we remove any existing shipping line.
1212
// We remove a shipping like by setting its `methodID` to nil.
1313
guard let input = input else {
14-
let linesToRemove = order.shippingLines.map { $0.copy(methodID: .some(nil), total: "0") }
14+
let linesToRemove = order.shippingLines.map { OrderFactory.deletedShippingLine($0) }
1515
return order.copy(shippingTotal: "0", shippingLines: linesToRemove)
1616
}
1717

Yosemite/Yosemite/Stores/Order/OrderFactory.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public enum OrderFactory {
7474
/// Creates a shipping line suitable to delete a shipping line already saved remotely in an order.
7575
///
7676
public static func deletedShippingLine(_ shippingLine: ShippingLine) -> ShippingLine {
77-
shippingLine.copy(methodID: .some(nil))
77+
shippingLine.copy(methodID: .some(nil), total: "0")
7878
}
7979

8080
/// References a new empty order with constants `Date` values.

Yosemite/YosemiteTests/Stores/OrderNoteStoreTests.swift

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,102 @@ class OrderNoteStoreTests: XCTestCase {
232232
orderNoteStore.onAction(action)
233233
wait(for: [expectation], timeout: Constants.expectationTimeout)
234234
}
235+
236+
/// Verifies that `OrderNoteAction.addOrderNote` returns the expected OrderNote.
237+
///
238+
func test_add_order_note_returns_expected_note() {
239+
// Given
240+
let orderNoteStore = OrderNoteStore(dispatcher: dispatcher, storageManager: storageManager, network: network)
241+
network.simulateResponse(requestUrlSuffix: "orders/\(sampleOrderID)/notes", filename: "new-order-note")
242+
243+
// When
244+
let (orderNote, error): (Networking.OrderNote?, Error?) = waitFor { promise in
245+
let action = OrderNoteAction.addOrderNote(siteID: self.sampleSiteID,
246+
orderID: self.sampleOrderID,
247+
isCustomerNote: true,
248+
note: "This order would be so much better with ketchup.") { (orderNote, error) in
249+
promise((orderNote, error))
250+
}
251+
orderNoteStore.onAction(action)
252+
}
253+
254+
// Then
255+
XCTAssertNotNil(orderNote)
256+
XCTAssertEqual(orderNote, self.sampleNewNote())
257+
XCTAssertNil(error)
258+
}
259+
260+
/// Verifies that `OrderNoteAction.addOrderNote` effectively persists the new order note.
261+
///
262+
func test_add_order_note_effectively_persists_new_order_note() {
263+
// Given
264+
let orderStore = OrderStore(dispatcher: dispatcher, storageManager: storageManager, network: network)
265+
let orderNoteStore = OrderNoteStore(dispatcher: dispatcher, storageManager: storageManager, network: network)
266+
orderStore.upsertStoredOrder(readOnlyOrder: sampleOrder(), in: viewStorage)
267+
network.simulateResponse(requestUrlSuffix: "orders/\(sampleOrderID)/notes", filename: "new-order-note")
268+
XCTAssertEqual(viewStorage.countObjects(ofType: Storage.OrderNote.self), 0)
269+
270+
// When
271+
let (orderNote, error): (Networking.OrderNote?, Error?) = waitFor { promise in
272+
let action = OrderNoteAction.addOrderNote(siteID: self.sampleSiteID,
273+
orderID: self.sampleOrderID,
274+
isCustomerNote: true,
275+
note: "") { (orderNote, error) in
276+
promise((orderNote, error))
277+
}
278+
orderNoteStore.onAction(action)
279+
}
280+
281+
// Then
282+
XCTAssertNotNil(orderNote)
283+
XCTAssertEqual(self.viewStorage.countObjects(ofType: Storage.OrderNote.self), 1)
284+
XCTAssertNil(error)
285+
}
286+
287+
/// Verifies that `OrderNoteAction.addOrderNote` returns an error whenever there is an error response from the backend.
288+
///
289+
func test_add_order_note_returns_error_upon_response_error() {
290+
// Given
291+
let orderNoteStore = OrderNoteStore(dispatcher: dispatcher, storageManager: storageManager, network: network)
292+
network.simulateResponse(requestUrlSuffix: "orders/\(sampleOrderID)/notes", filename: "generic_error")
293+
294+
// When
295+
let (orderNote, error): (Networking.OrderNote?, Error?) = waitFor { promise in
296+
let action = OrderNoteAction.addOrderNote(siteID: self.sampleSiteID,
297+
orderID: self.sampleOrderID,
298+
isCustomerNote: true,
299+
note: "") { (orderNote, error) in
300+
promise((orderNote, error))
301+
}
302+
orderNoteStore.onAction(action)
303+
}
304+
305+
// Then
306+
XCTAssertNotNil(error)
307+
XCTAssertNil(orderNote)
308+
}
309+
310+
/// Verifies that `OrderNoteAction.addOrderNote` returns an error whenever there is no backend response.
311+
///
312+
func test_add_order_note_returns_error_upon_empty_response() {
313+
// Given
314+
let orderNoteStore = OrderNoteStore(dispatcher: dispatcher, storageManager: storageManager, network: network)
315+
316+
// When
317+
let (orderNote, error): (Networking.OrderNote?, Error?) = waitFor { promise in
318+
let action = OrderNoteAction.addOrderNote(siteID: self.sampleSiteID,
319+
orderID: self.sampleOrderID,
320+
isCustomerNote: true,
321+
note: "") { (orderNote, error) in
322+
promise((orderNote, error))
323+
}
324+
orderNoteStore.onAction(action)
325+
}
326+
327+
// Then
328+
XCTAssertNotNil(error)
329+
XCTAssertNil(orderNote)
330+
}
235331
}
236332

237333
// MARK: - Private Methods
@@ -269,6 +365,14 @@ private extension OrderNoteStoreTests {
269365
author: sampleSystemAuthor)
270366
}
271367

368+
func sampleNewNote() -> Networking.OrderNote {
369+
return OrderNote(noteID: 2235,
370+
dateCreated: date(with: "2018-06-22T15:36:20"),
371+
note: "This order would be so much better with ketchup.",
372+
isCustomerNote: true,
373+
author: sampleAdminAuthor)
374+
}
375+
272376
func sampleOrder() -> Networking.Order {
273377
return Order.fake().copy(siteID: sampleSiteID,
274378
orderID: sampleOrderID,

0 commit comments

Comments
 (0)