|
| 1 | +@testable import WooCommerce |
| 2 | +import Testing |
| 3 | +import Foundation |
| 4 | + |
| 5 | +import protocol Yosemite.PointOfSaleItemServiceProtocol |
| 6 | +import enum Yosemite.POSItem |
| 7 | +import struct Yosemite.POSCoupon |
| 8 | +import struct Yosemite.PagedItems |
| 9 | +import struct Yosemite.POSVariableParentProduct |
| 10 | + |
| 11 | +final class MockPointOfSaleCouponService: PointOfSaleItemServiceProtocol { |
| 12 | + var shouldReturnZeroItems = false |
| 13 | + |
| 14 | + func providePointOfSaleItems(pageNumber: Int) async throws -> PagedItems<POSItem> { |
| 15 | + if shouldReturnZeroItems { |
| 16 | + return .init(items: [], hasMorePages: false) |
| 17 | + } else { |
| 18 | + return .init(items: Self.makeInitialCoupons(), |
| 19 | + hasMorePages: false) |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + func providePointOfSaleVariationItems(for parentProduct: POSVariableParentProduct, pageNumber: Int) async throws -> PagedItems<POSItem> { |
| 24 | + return .init(items: [], hasMorePages: false) |
| 25 | + } |
| 26 | + |
| 27 | + static func makeInitialCoupons() -> [POSItem] { |
| 28 | + let coupon1 = POSItem.coupon(POSCoupon(id: UUID(uuidString: ("DC55E3B9-9D83-4C07-82A7-4C300A50E84A")) ?? UUID(), code: "VALID1")) |
| 29 | + let coupon2 = POSItem.coupon(POSCoupon(id: UUID(uuidString: ("DC55E3B9-9D83-4C07-82A7-4C300A50E84B")) ?? UUID(), code: "VALID2")) |
| 30 | + let coupon3 = POSItem.coupon(POSCoupon(id: UUID(uuidString: ("DC55E3B9-9D83-4C07-82A7-4C300A50E84C")) ?? UUID(), code: "VALID3")) |
| 31 | + return [coupon1, coupon2, coupon3] |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +struct PointOfSaleCouponsControllerTests { |
| 36 | + @available(iOS 17.0, *) |
| 37 | + @Test func loadItems_when_empty_coupons_then_results_in_empty_loaded_state() async throws { |
| 38 | + // Given |
| 39 | + let couponProvider = MockPointOfSaleCouponService() |
| 40 | + couponProvider.shouldReturnZeroItems = true |
| 41 | + let sut = PointOfSaleCouponsController(itemProvider: couponProvider) |
| 42 | + |
| 43 | + let expectedItemStackState = ItemsStackState(root: .loaded([], hasMoreItems: false), itemStates: [:]) |
| 44 | + let expectedViewState = ItemsViewState(containerState: .content, itemsStack: expectedItemStackState) |
| 45 | + |
| 46 | + // When |
| 47 | + await sut.loadItems(base: .root) |
| 48 | + |
| 49 | + // Then |
| 50 | + #expect(sut.itemsViewState == expectedViewState) |
| 51 | + } |
| 52 | + |
| 53 | + @available(iOS 17.0, *) |
| 54 | + @Test func loadItems_when_some_coupons_then_results_in_coupons_loaded_state() async throws { |
| 55 | + // Given |
| 56 | + let couponProvider = MockPointOfSaleCouponService() |
| 57 | + let expectedCoupons = MockPointOfSaleCouponService.makeInitialCoupons() |
| 58 | + let sut = PointOfSaleCouponsController(itemProvider: couponProvider) |
| 59 | + |
| 60 | + let expectedItemStackState = ItemsStackState(root: .loaded(expectedCoupons, hasMoreItems: false), itemStates: [:]) |
| 61 | + let expectedViewState = ItemsViewState(containerState: .content, itemsStack: expectedItemStackState) |
| 62 | + |
| 63 | + // When |
| 64 | + await sut.loadItems(base: .root) |
| 65 | + |
| 66 | + // Then |
| 67 | + #expect(sut.itemsViewState == expectedViewState) |
| 68 | + } |
| 69 | + |
| 70 | + @available(iOS 17.0, *) |
| 71 | + @Test func refreshItems_when_empty_coupons_then_results_in_empty_loaded_state() async throws { |
| 72 | + // Given |
| 73 | + let couponProvider = MockPointOfSaleCouponService() |
| 74 | + couponProvider.shouldReturnZeroItems = true |
| 75 | + let sut = PointOfSaleCouponsController(itemProvider: couponProvider) |
| 76 | + |
| 77 | + let expectedItemStackState = ItemsStackState(root: .loaded([], hasMoreItems: false), itemStates: [:]) |
| 78 | + let expectedViewState = ItemsViewState(containerState: .content, itemsStack: expectedItemStackState) |
| 79 | + |
| 80 | + // When |
| 81 | + await sut.refreshItems(base: .root) |
| 82 | + |
| 83 | + // Then |
| 84 | + #expect(sut.itemsViewState == expectedViewState) |
| 85 | + } |
| 86 | + |
| 87 | + @available(iOS 17.0, *) |
| 88 | + @Test func refreshItems_when_some_coupons_then_results_in_coupons_loaded_state() async throws { |
| 89 | + // Given |
| 90 | + let couponProvider = MockPointOfSaleCouponService() |
| 91 | + let expectedCoupons = MockPointOfSaleCouponService.makeInitialCoupons() |
| 92 | + let sut = PointOfSaleCouponsController(itemProvider: couponProvider) |
| 93 | + |
| 94 | + let expectedItemStackState = ItemsStackState(root: .loaded(expectedCoupons, hasMoreItems: false), itemStates: [:]) |
| 95 | + let expectedViewState = ItemsViewState(containerState: .content, itemsStack: expectedItemStackState) |
| 96 | + |
| 97 | + // When |
| 98 | + await sut.refreshItems(base: .root) |
| 99 | + |
| 100 | + // Then |
| 101 | + #expect(sut.itemsViewState == expectedViewState) |
| 102 | + } |
| 103 | + |
| 104 | + @available(iOS 17.0, *) |
| 105 | + @Test func loadNextItems_when_empty_coupons_then_results_in_empty_loaded_state() async throws { |
| 106 | + // Given |
| 107 | + let couponProvider = MockPointOfSaleCouponService() |
| 108 | + couponProvider.shouldReturnZeroItems = true |
| 109 | + let sut = PointOfSaleCouponsController(itemProvider: couponProvider) |
| 110 | + |
| 111 | + let expectedItemStackState = ItemsStackState(root: .loaded([], hasMoreItems: false), itemStates: [:]) |
| 112 | + let expectedViewState = ItemsViewState(containerState: .content, itemsStack: expectedItemStackState) |
| 113 | + |
| 114 | + // When |
| 115 | + await sut.loadNextItems(base: .root) |
| 116 | + |
| 117 | + // Then |
| 118 | + #expect(sut.itemsViewState == expectedViewState) |
| 119 | + } |
| 120 | + |
| 121 | + @available(iOS 17.0, *) |
| 122 | + @Test func loadNextItems_when_some_coupons_then_results_in_coupons_loaded_state() async throws { |
| 123 | + // Given |
| 124 | + let couponProvider = MockPointOfSaleCouponService() |
| 125 | + let expectedCoupons = MockPointOfSaleCouponService.makeInitialCoupons() |
| 126 | + let sut = PointOfSaleCouponsController(itemProvider: couponProvider) |
| 127 | + |
| 128 | + let expectedItemStackState = ItemsStackState(root: .loaded(expectedCoupons, hasMoreItems: false), itemStates: [:]) |
| 129 | + let expectedViewState = ItemsViewState(containerState: .content, itemsStack: expectedItemStackState) |
| 130 | + |
| 131 | + // When |
| 132 | + await sut.loadNextItems(base: .root) |
| 133 | + |
| 134 | + // Then |
| 135 | + #expect(sut.itemsViewState == expectedViewState) |
| 136 | + } |
| 137 | +} |
0 commit comments