Skip to content

Commit

Permalink
time test added
Browse files Browse the repository at this point in the history
  • Loading branch information
akashsoni01 committed May 29, 2023
1 parent 43d9fd4 commit 47d37ff
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified currency_exchange_app/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ struct CurrencyExchangeFeature: Reducer {
var items: IdentifiedArrayOf<ItemModel> = []
@BindingState var model: CurrencyExchange
var shouldShowView: Bool { return (self.model.rates?.count ?? 0) > 0 && self.model.selectedCurrency.count > 0 }

init(
model: CurrencyExchange
) {
let refreshTime = 60

init(model: CurrencyExchange) {
do {
@Dependency(\.dataManager.load) var load
self.model = try JSONDecoder().decode(CurrencyExchange.self, from: load(.currencyLocalStorageUrl))
Expand Down Expand Up @@ -93,14 +92,14 @@ struct CurrencyExchangeFeature: Reducer {
let endDate = self.now
let components = self.calendar.dateComponents([.minute], from: startDate, to: endDate)
let distenceInMin = components.minute ?? 0
if distenceInMin < 60 {
if distenceInMin < state.refreshTime {
return .run { [model = state.model] send in
await send(.receiveCurrency(
TaskResult { model }
))
}
} else {
// if api didn't call from last 60 minutes then call api again with USD and change base currency
// if api didn't call from last refreshTime minutes then call api again with USD and change base currency
state.model.selectedCurrency = "USD"
return .run { [previouslySelectedCurrency = state.model.oldSelectedCurrency] send in
await send(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,15 @@ struct CurrencyExchange: Codable, Equatable {
let license: String?
var base: String?
let timestamp: Int
var rates: [String: Double]?

var lastFetchedTime: Date?
var currencyValue = 1.0
var currencyExchangeValue = 1.0
var selectedCurrency: String = "USD"
var oldSelectedCurrency: String = "USD"

var rates: [String: Double]?
var sortedKeys: [String] {
rates?.keys.sorted() ?? []
}

var sortedKeys: [String] { rates?.keys.sorted() ?? [] }

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.disclaimer = try container.decodeIfPresent(String.self, forKey: .disclaimer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,39 @@ final class currency_exchange_appTests: XCTestCase {

}

func test_refreshTime_changed() async {
let currencyExchange = CurrencyExchange(
disclaimer: "disclaimer",
license: "license",
base: "USD",
timestamp: 0,
lastFetchedTime: Date(timeIntervalSince1970: 1_234_567_890),
selectedCurrency: "USD",
oldSelectedCurrency: "USD",
rates: mockRates
)

let testClock = TestClock()
let testStore = TestStore(
initialState: CurrencyExchangeFeature.State(
model: currencyExchange)
) {
CurrencyExchangeFeature()
} withDependencies: {
$0.uuid = .incrementing
$0.date.now = Date(timeIntervalSince1970: 1_234_567_890)
$0.continuousClock = testClock
$0.currencyApiClient = .default()
$0.currencyApiClient.getCurrencyExchangeRates = { _ in currencyExchange }
$0.dataManager = .mock(
initialData: try! JSONEncoder().encode(currencyExchange)
)
$0.calendar = .current
}
XCTAssertTrue(testStore.state.refreshTime == 60)

}

func test_fetchCurrencies_fromApiAfter60Minutes() async {
// Create a starting date
let startingDate = Date(timeIntervalSince1970: 1_234_567_890) // Use the current date and time as an example
Expand Down

0 comments on commit 47d37ff

Please sign in to comment.