@@ -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