@@ -404,6 +404,8 @@ final class NewOrderViewModelTests: XCTestCase {
404404 XCTAssertEqual ( paymentDataViewModel. orderTotal, " £30.00 " )
405405 }
406406
407+ // MARK: - Payment Section Tests
408+
407409 func test_payment_section_is_updated_when_products_update( ) {
408410 // Given
409411 let currencySettings = CurrencySettings ( currencyCode: . GBP, currencyPosition: . left, thousandSeparator: " " , decimalSeparator: " . " , numberOfDecimals: 2 )
@@ -497,6 +499,129 @@ final class NewOrderViewModelTests: XCTestCase {
497499 XCTAssertEqual ( viewModel. paymentDataViewModel. feesBaseAmountForPercentage, 8.50 )
498500 }
499501
502+ func test_payment_section_values_correct_when_shipping_line_is_negative( ) {
503+ // Given
504+ let currencySettings = CurrencySettings ( currencyCode: . GBP, currencyPosition: . left, thousandSeparator: " " , decimalSeparator: " . " , numberOfDecimals: 2 )
505+ let product = Product . fake ( ) . copy ( siteID: sampleSiteID, productID: sampleProductID, price: " 8.50 " , purchasable: true )
506+ let storageManager = MockStorageManager ( )
507+ storageManager. insertSampleProduct ( readOnlyProduct: product)
508+ let viewModel = NewOrderViewModel ( siteID: sampleSiteID, storageManager: storageManager, currencySettings: currencySettings)
509+
510+ // When
511+ viewModel. addProductViewModel. selectProduct ( product. productID)
512+ let testShippingLine = ShippingLine ( shippingID: 0 ,
513+ methodTitle: " Flat Rate " ,
514+ methodID: " other " ,
515+ total: " -5 " ,
516+ totalTax: " " ,
517+ taxes: [ ] )
518+ viewModel. saveShippingLine ( testShippingLine)
519+
520+ // Then
521+ XCTAssertTrue ( viewModel. paymentDataViewModel. shouldShowShippingTotal)
522+ XCTAssertEqual ( viewModel. paymentDataViewModel. itemsTotal, " £8.50 " )
523+ XCTAssertEqual ( viewModel. paymentDataViewModel. shippingTotal, " -£5.00 " )
524+ XCTAssertEqual ( viewModel. paymentDataViewModel. orderTotal, " £3.50 " )
525+ XCTAssertEqual ( viewModel. paymentDataViewModel. feesBaseAmountForPercentage, 3.50 )
526+
527+ // When
528+ viewModel. saveShippingLine ( nil )
529+
530+ // Then
531+ XCTAssertFalse ( viewModel. paymentDataViewModel. shouldShowShippingTotal)
532+ XCTAssertEqual ( viewModel. paymentDataViewModel. itemsTotal, " £8.50 " )
533+ XCTAssertEqual ( viewModel. paymentDataViewModel. shippingTotal, " £0.00 " )
534+ XCTAssertEqual ( viewModel. paymentDataViewModel. orderTotal, " £8.50 " )
535+ XCTAssertEqual ( viewModel. paymentDataViewModel. feesBaseAmountForPercentage, 8.50 )
536+ }
537+
538+ func test_payment_section_values_correct_when_fee_line_is_negative( ) {
539+ // Given
540+ let currencySettings = CurrencySettings ( currencyCode: . GBP, currencyPosition: . left, thousandSeparator: " " , decimalSeparator: " . " , numberOfDecimals: 2 )
541+ let product = Product . fake ( ) . copy ( siteID: sampleSiteID, productID: sampleProductID, price: " 8.50 " , purchasable: true )
542+ let storageManager = MockStorageManager ( )
543+ storageManager. insertSampleProduct ( readOnlyProduct: product)
544+ let viewModel = NewOrderViewModel ( siteID: sampleSiteID, storageManager: storageManager, currencySettings: currencySettings)
545+
546+ // When
547+ viewModel. addProductViewModel. selectProduct ( product. productID)
548+ let testFeeLine = OrderFeeLine ( feeID: 0 ,
549+ name: " Fee " ,
550+ taxClass: " " ,
551+ taxStatus: . none,
552+ total: " -5 " ,
553+ totalTax: " " ,
554+ taxes: [ ] ,
555+ attributes: [ ] )
556+ viewModel. saveFeeLine ( testFeeLine)
557+
558+ // Then
559+ XCTAssertTrue ( viewModel. paymentDataViewModel. shouldShowFees)
560+ XCTAssertEqual ( viewModel. paymentDataViewModel. itemsTotal, " £8.50 " )
561+ XCTAssertEqual ( viewModel. paymentDataViewModel. feesTotal, " -£5.00 " )
562+ XCTAssertEqual ( viewModel. paymentDataViewModel. orderTotal, " £3.50 " )
563+ XCTAssertEqual ( viewModel. paymentDataViewModel. feesBaseAmountForPercentage, 8.50 )
564+
565+ // When
566+ viewModel. saveFeeLine ( nil )
567+
568+ // Then
569+ XCTAssertFalse ( viewModel. paymentDataViewModel. shouldShowFees)
570+ XCTAssertEqual ( viewModel. paymentDataViewModel. itemsTotal, " £8.50 " )
571+ XCTAssertEqual ( viewModel. paymentDataViewModel. feesTotal, " £0.00 " )
572+ XCTAssertEqual ( viewModel. paymentDataViewModel. orderTotal, " £8.50 " )
573+ XCTAssertEqual ( viewModel. paymentDataViewModel. feesBaseAmountForPercentage, 8.50 )
574+ }
575+
576+ func test_payment_section_is_correct_when_shipping_line_and_fee_line_are_added( ) {
577+ // Given
578+ let currencySettings = CurrencySettings ( currencyCode: . GBP, currencyPosition: . left, thousandSeparator: " " , decimalSeparator: " . " , numberOfDecimals: 2 )
579+ let product = Product . fake ( ) . copy ( siteID: sampleSiteID, productID: sampleProductID, price: " 8.50 " , purchasable: true )
580+ let storageManager = MockStorageManager ( )
581+ storageManager. insertSampleProduct ( readOnlyProduct: product)
582+ let viewModel = NewOrderViewModel ( siteID: sampleSiteID, storageManager: storageManager, currencySettings: currencySettings)
583+
584+ // When
585+ viewModel. addProductViewModel. selectProduct ( product. productID)
586+
587+ let testShippingLine = ShippingLine ( shippingID: 0 ,
588+ methodTitle: " Flat Rate " ,
589+ methodID: " other " ,
590+ total: " -5 " ,
591+ totalTax: " " ,
592+ taxes: [ ] )
593+ viewModel. saveShippingLine ( testShippingLine)
594+
595+ let testFeeLine = OrderFeeLine ( feeID: 0 ,
596+ name: " Fee " ,
597+ taxClass: " " ,
598+ taxStatus: . none,
599+ total: " 10 " ,
600+ totalTax: " " ,
601+ taxes: [ ] ,
602+ attributes: [ ] )
603+ viewModel. saveFeeLine ( testFeeLine)
604+
605+ // Then
606+ XCTAssertTrue ( viewModel. paymentDataViewModel. shouldShowShippingTotal)
607+ XCTAssertEqual ( viewModel. paymentDataViewModel. itemsTotal, " £8.50 " )
608+ XCTAssertEqual ( viewModel. paymentDataViewModel. shippingTotal, " -£5.00 " )
609+ XCTAssertEqual ( viewModel. paymentDataViewModel. orderTotal, " £13.50 " )
610+ XCTAssertEqual ( viewModel. paymentDataViewModel. feesTotal, " £10.00 " )
611+ XCTAssertEqual ( viewModel. paymentDataViewModel. feesBaseAmountForPercentage, 3.50 )
612+
613+ // When
614+ viewModel. saveShippingLine ( nil )
615+
616+ // Then
617+ XCTAssertFalse ( viewModel. paymentDataViewModel. shouldShowShippingTotal)
618+ XCTAssertEqual ( viewModel. paymentDataViewModel. itemsTotal, " £8.50 " )
619+ XCTAssertEqual ( viewModel. paymentDataViewModel. shippingTotal, " £0.00 " )
620+ XCTAssertEqual ( viewModel. paymentDataViewModel. orderTotal, " £18.50 " )
621+ XCTAssertEqual ( viewModel. paymentDataViewModel. feesTotal, " £10.00 " )
622+ XCTAssertEqual ( viewModel. paymentDataViewModel. feesBaseAmountForPercentage, 8.50 )
623+ }
624+
500625 func test_payment_section_loading_indicator_is_enabled_while_order_syncs( ) {
501626 // Given
502627 let stores = MockStoresManager ( sessionManager: . testingInstance)
@@ -551,19 +676,7 @@ final class NewOrderViewModelTests: XCTestCase {
551676
552677 }
553678
554- func test_customer_note_section_is_updated_when_note_is_added_to_order( ) {
555- // Given
556- let storageManager = MockStorageManager ( )
557- let viewModel = NewOrderViewModel ( siteID: sampleSiteID, storageManager: storageManager)
558- let expectedCustomerNote = " Test "
559-
560- //When
561- viewModel. noteViewModel. newNote = expectedCustomerNote
562- viewModel. updateCustomerNote ( )
563-
564- //Then
565- XCTAssertEqual ( viewModel. customerNoteDataViewModel. customerNote, expectedCustomerNote)
566- }
679+ // MARK: - hasChanges Tests
567680
568681 func test_hasChanges_returns_false_initially( ) {
569682 // Given
@@ -630,6 +743,34 @@ final class NewOrderViewModelTests: XCTestCase {
630743 XCTAssertTrue ( viewModel. hasChanges)
631744 }
632745
746+ func test_hasChanges_returns_true_when_shipping_line_is_updated( ) {
747+ // Given
748+ let storageManager = MockStorageManager ( )
749+ let viewModel = NewOrderViewModel ( siteID: sampleSiteID, storageManager: storageManager)
750+ let shippingLine = ShippingLine . fake ( )
751+
752+ // When
753+ viewModel. saveShippingLine ( shippingLine)
754+
755+ // Then
756+ XCTAssertTrue ( viewModel. hasChanges)
757+ }
758+
759+ func test_hasChanges_returns_true_when_fee_line_is_updated( ) {
760+ // Given
761+ let storageManager = MockStorageManager ( )
762+ let viewModel = NewOrderViewModel ( siteID: sampleSiteID, storageManager: storageManager)
763+ let feeLine = OrderFeeLine . fake ( )
764+
765+ // When
766+ viewModel. saveFeeLine ( feeLine)
767+
768+ // Then
769+ XCTAssertTrue ( viewModel. hasChanges)
770+ }
771+
772+ // MARK: - Tracking Tests
773+
633774 func test_shipping_method_tracked_when_added( ) throws {
634775 // Given
635776 let analytics = MockAnalyticsProvider ( )
@@ -749,6 +890,22 @@ final class NewOrderViewModelTests: XCTestCase {
749890 XCTAssertTrue ( analytics. receivedEvents. isEmpty)
750891 }
751892
893+ // MARK: -
894+
895+ func test_customer_note_section_is_updated_when_note_is_added_to_order( ) {
896+ // Given
897+ let storageManager = MockStorageManager ( )
898+ let viewModel = NewOrderViewModel ( siteID: sampleSiteID, storageManager: storageManager)
899+ let expectedCustomerNote = " Test "
900+
901+ //When
902+ viewModel. noteViewModel. newNote = expectedCustomerNote
903+ viewModel. updateCustomerNote ( )
904+
905+ //Then
906+ XCTAssertEqual ( viewModel. customerNoteDataViewModel. customerNote, expectedCustomerNote)
907+ }
908+
752909 func test_discard_order_deletes_order_if_order_exists_remotely( ) {
753910 // Given
754911 let stores = MockStoresManager ( sessionManager: . testingInstance)
0 commit comments