@@ -3,6 +3,7 @@ import XCTest
33@testable import WooCommerce
44import Yosemite
55import TestKit
6+ import protocol Experiments. FeatureFlagService
67
78final class ProductFormViewModelTests : XCTestCase {
89
@@ -532,19 +533,136 @@ final class ProductFormViewModelTests: XCTestCase {
532533 let hasLinkedProducts = try XCTUnwrap ( analyticsProvider. receivedProperties. first ? [ " has_linked_products " ] as? Bool )
533534 XCTAssertTrue ( hasLinkedProducts)
534535 }
536+
537+ // MARK: Preview button tests (with enabled Product Onboarding feature flag)
538+
539+ func test_no_preview_button_for_new_blank_product_without_any_changes( ) {
540+ // Given
541+ let product = Product . fake ( ) . copy ( statusKey: ProductStatus . published. rawValue)
542+ let viewModel = createViewModel ( product: product, formType: . add, featureFlagService: MockFeatureFlagService ( isProductsOnboardingEnabled: true ) )
543+
544+ // When
545+ let actionButtons = viewModel. actionButtons
546+
547+ // Then
548+ XCTAssertEqual ( actionButtons, [ . publish, . more] )
549+ }
550+
551+ func test_preview_button_for_new_product_with_pending_changes( ) {
552+ // Given
553+ let product = Product . fake ( ) . copy ( statusKey: ProductStatus . published. rawValue)
554+ let viewModel = createViewModel ( product: product, formType: . add, featureFlagService: MockFeatureFlagService ( isProductsOnboardingEnabled: true ) )
555+ viewModel. updateName ( " new name " )
556+
557+ // When
558+ let actionButtons = viewModel. actionButtons
559+
560+ // Then
561+ XCTAssertEqual ( actionButtons, [ . preview, . publish, . more] )
562+ }
563+
564+ func test_no_preview_button_for_existing_published_product_without_any_changes( ) {
565+ // Given
566+ let product = Product . fake ( ) . copy ( productID: 123 , statusKey: ProductStatus . published. rawValue)
567+ let viewModel = createViewModel ( product: product, formType: . edit, featureFlagService: MockFeatureFlagService ( isProductsOnboardingEnabled: true ) )
568+ viewModel. updateName ( " new name " )
569+
570+ // When
571+ let actionButtons = viewModel. actionButtons
572+
573+ // Then
574+ XCTAssertEqual ( actionButtons, [ . save, . more] )
575+ }
576+
577+ func test_no_preview_button_for_existing_published_product_with_pending_changes( ) {
578+ // Given
579+ let product = Product . fake ( ) . copy ( productID: 123 , statusKey: ProductStatus . published. rawValue)
580+ let viewModel = createViewModel ( product: product, formType: . edit, featureFlagService: MockFeatureFlagService ( isProductsOnboardingEnabled: true ) )
581+
582+ // When
583+ let actionButtons = viewModel. actionButtons
584+
585+ // Then
586+ XCTAssertEqual ( actionButtons, [ . more] )
587+ }
588+
589+ func test_preview_button_for_existing_draft_product_without_any_changes( ) {
590+ // Given
591+ let product = Product . fake ( ) . copy ( productID: 123 , statusKey: ProductStatus . draft. rawValue)
592+ let viewModel = createViewModel ( product: product, formType: . edit, featureFlagService: MockFeatureFlagService ( isProductsOnboardingEnabled: true ) )
593+
594+ // When
595+ let actionButtons = viewModel. actionButtons
596+
597+ // Then
598+ XCTAssertEqual ( actionButtons, [ . preview, . publish, . more] )
599+ }
600+
601+ func test_preview_button_for_existing_draft_product_with_pending_changes( ) {
602+ // Given
603+ let product = Product . fake ( ) . copy ( productID: 123 , statusKey: ProductStatus . draft. rawValue)
604+ let viewModel = createViewModel ( product: product, formType: . edit, featureFlagService: MockFeatureFlagService ( isProductsOnboardingEnabled: true ) )
605+ viewModel. updateName ( " new name " )
606+
607+ // When
608+ let actionButtons = viewModel. actionButtons
609+
610+ // Then
611+ XCTAssertEqual ( actionButtons, [ . preview, . save, . more] )
612+ }
613+
614+ func test_no_preview_button_for_existing_product_with_other_status_and_without_any_changes( ) {
615+ // Given
616+ let product = Product . fake ( ) . copy ( productID: 123 , statusKey: " other " )
617+ let viewModel = createViewModel ( product: product, formType: . edit, featureFlagService: MockFeatureFlagService ( isProductsOnboardingEnabled: true ) )
618+
619+ // When
620+ let actionButtons = viewModel. actionButtons
621+
622+ // Then
623+ XCTAssertEqual ( actionButtons, [ . publish, . more] )
624+ }
625+
626+ func test_no_preview_button_for_existing_product_with_other_status_and_pending_changes( ) {
627+ // Given
628+ let product = Product . fake ( ) . copy ( productID: 123 , statusKey: " other " )
629+ let viewModel = createViewModel ( product: product, formType: . edit, featureFlagService: MockFeatureFlagService ( isProductsOnboardingEnabled: true ) )
630+ viewModel. updateName ( " new name " )
631+
632+ // When
633+ let actionButtons = viewModel. actionButtons
634+
635+ // Then
636+ XCTAssertEqual ( actionButtons, [ . save, . more] )
637+ }
638+
639+ func test_no_preview_button_for_any_product_in_read_only_mode( ) {
640+ // Given
641+ let product = Product . fake ( ) . copy ( productID: 123 , statusKey: ProductStatus . published. rawValue)
642+ let viewModel = createViewModel ( product: product, formType: . readonly, featureFlagService: MockFeatureFlagService ( isProductsOnboardingEnabled: true ) )
643+ viewModel. updateName ( " new name " )
644+
645+ // When
646+ let actionButtons = viewModel. actionButtons
647+
648+ // Then
649+ XCTAssertEqual ( actionButtons, [ . more] )
650+ }
535651}
536652
537653private extension ProductFormViewModelTests {
538654 func createViewModel( product: Product ,
539655 formType: ProductFormType ,
540656 stores: StoresManager = ServiceLocator . stores,
541- analytics: Analytics = ServiceLocator . analytics) -> ProductFormViewModel {
657+ analytics: Analytics = ServiceLocator . analytics,
658+ featureFlagService: FeatureFlagService = MockFeatureFlagService ( ) ) -> ProductFormViewModel {
542659 let model = EditableProductModel ( product: product)
543660 let productImageActionHandler = ProductImageActionHandler ( siteID: 0 , product: model)
544661 return ProductFormViewModel ( product: model,
545662 formType: formType,
546663 productImageActionHandler: productImageActionHandler,
547664 stores: stores,
548- analytics: analytics)
665+ analytics: analytics,
666+ featureFlagService: featureFlagService)
549667 }
550668}
0 commit comments