Skip to content

Commit

Permalink
[CVW-025] Feat, ViewModel구조 변경으로 인한 Test코드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
J0onYEong committed Jan 23, 2025
1 parent cf6ece1 commit e887a3e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class AllMarketTickerViewModel: UDFObservableObject, AllMarketTickerViewMo


// State
@Published var state: State = .init(tickerDisplayType: .list)
@Published var state: State = .init()
private var isFirstAppear: Bool = true


Expand Down Expand Up @@ -224,13 +224,33 @@ extension AllMarketTickerViewModel {
var currencyType: CurrencyType?
var exchangeRate: Double?


var isLoaded: Bool {
!tickerCellRO.isEmpty &&
languageType != nil &&
currencyType != nil &&
exchangeRate != nil
}

init(
sortSelectionCellROs: [SortSelectionCellType: SortSelectionCellRO] = [:],
sortComparator: TickerSortComparator=TickerNoneComparator(),
tickerCellVO: [Twenty4HourTickerForSymbolVO] = [],
tickerDisplayType: GridType = .list,
tickerCellRO: [TickerCellRO] = [],
languageType: LanguageType? = nil,
currencyType: CurrencyType? = nil,
exchangeRate: Double? = nil
) {
self.sortSelectionCellROs = sortSelectionCellROs
self.sortComparator = sortComparator
self.tickerCellVO = tickerCellVO
self.tickerDisplayType = tickerDisplayType
self.tickerCellRO = tickerCellRO
self.languageType = languageType
self.currencyType = currencyType
self.exchangeRate = exchangeRate
}
}

enum Action {
Expand Down
25 changes: 0 additions & 25 deletions Projects/Features/AllMarketTicker/Testing/StubSortComparator.swift

This file was deleted.

58 changes: 29 additions & 29 deletions Projects/Features/AllMarketTicker/Tests/ViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import Combine
import Testing
import Foundation

@testable import AllMarketTickerFeatureTesting
@testable import AllMarketTickerFeature
Expand All @@ -27,7 +28,7 @@ struct AllMarketTickerViewModelTests {
}

@Test
func testSortingComparator() {
func checkPriceSortingComparator() async {

// Given
let viewModel = AllMarketTickerViewModel(
Expand All @@ -38,40 +39,39 @@ struct AllMarketTickerViewModelTests {
exchangeUseCase: StubExchangeUseCase(),
userConfigurationRepository: FakeUserConfigurationRepository()
)
let givenTickerCellViewModels: [TickerCellViewModel] = [
Twenty4HourTickerForSymbolVO(pairSymbol: "test1USDT", price: 400.0, totalTradedQuoteAssetVolume: 1.0, changedPercent: 1.0),
Twenty4HourTickerForSymbolVO(pairSymbol: "test2USDT", price: 100.0, totalTradedQuoteAssetVolume: 1.0, changedPercent: 1.0),
Twenty4HourTickerForSymbolVO(pairSymbol: "test3USDT", price: 200.0, totalTradedQuoteAssetVolume: 1.0, changedPercent: 1.0),
Twenty4HourTickerForSymbolVO(pairSymbol: "test4USDT", price: 300.0, totalTradedQuoteAssetVolume: 1.0, changedPercent: 1.0),
].map { ticker in
TickerCellViewModel(config: .init(
tickerVO: ticker,
currencyConfig: .init(type: .dollar, rate: 1.0))
)
let givenTickerVOs: [Twenty4HourTickerForSymbolVO] = [
Twenty4HourTickerForSymbolVO(pairSymbol: "test1USDT", price: 100.0, totalTradedQuoteAssetVolume: 1.0, changedPercent: 1.0),
Twenty4HourTickerForSymbolVO(pairSymbol: "test2USDT", price: 200.0, totalTradedQuoteAssetVolume: 1.0, changedPercent: 2.0),
Twenty4HourTickerForSymbolVO(pairSymbol: "test3USDT", price: 300.0, totalTradedQuoteAssetVolume: 1.0, changedPercent: 3.0),
Twenty4HourTickerForSymbolVO(pairSymbol: "test4USDT", price: 400.0, totalTradedQuoteAssetVolume: 1.0, changedPercent: 4.0),
].map { vo in
var newVO = vo
newVO.setSymbols(closure: { pairSymbol in
let firstSymbol = pairSymbol.replacingOccurrences(of: "USDT", with: "")
let secondSymbol = "USDT"
return (firstSymbol, secondSymbol)
})
return newVO
}
let initialState: AllMarketTickerViewModel.State = .init(
sortComparator: AscendingPriceSortComparator(),
tickerDisplayType: .list,
tickerCellViewModels: givenTickerCellViewModels
)
let ascendingPriceSortComparator = AscendingPriceSortComparator()
viewModel.action.send(.currencyTypeUpdated(type: .dollar, rate: 1.0))
viewModel.action.send(.tickerListFetched(list: givenTickerVOs))


// When
// #1. When
// - 정렬 기준을 가격에 의한 오름차순으로 설정
let resultState = viewModel.reduce(
.changeSortingCriteria(comparator: ascendingPriceSortComparator),
state: initialState
)
viewModel.action.send(.sortSelectionButtonTapped(type: .price))


// Then
// - 정렬이 올바르게 진행됬는지 확인
let expectedResult = initialState.tickerCellViewModels
.map { $0.tickerVO.price }
.sorted(by: { $0.wrappedNumber < $01.wrappedNumber })
let priceList = resultState.tickerCellViewModels.map { $0.tickerVO.price }
#expect(expectedResult == priceList)
// #1. Then
// - 정렬이 올바르게 진행됬는지 확인, descending price sort
let expectedSymbolArray = givenTickerVOs.sorted { lhs, rhs in
lhs.price > rhs.price
}.map({ $0.pairSymbol.uppercased() })
for await state in viewModel.$state.dropFirst(3).values {
let sortedSymbols = state.tickerCellRO.map({ $0.symbolText.uppercased() })
#expect(sortedSymbols == expectedSymbolArray)
break
}
}
}

0 comments on commit e887a3e

Please # to comment.