Skip to content

Commit

Permalink
[MBL-670] Saved Prelaunch Projects on Backer Dashboard -- Merge After…
Browse files Browse the repository at this point in the history
… MBL-671 (#1805)

* corrected search state of prelaunch pages

* prelaunch project search state tests

* added a new gql call to replace the existing fetchUserSelf v1 call. New gql call is useful keeping track of saved projects.

* fixed tests

* removed unneeded comment
  • Loading branch information
msadoon committed Mar 27, 2023
1 parent d1cd72e commit 938b714
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 6 deletions.
16 changes: 16 additions & 0 deletions KsApi/MockService.swift
Expand Up @@ -78,6 +78,7 @@
fileprivate let fetchDraftError: ErrorEnvelope?

fileprivate let fetchGraphUserResult: Result<UserEnvelope<GraphUser>, ErrorEnvelope>?
fileprivate let fetchGraphUserSelfResult: Result<UserEnvelope<User>, ErrorEnvelope>?
fileprivate let fetchGraphUserEmailResult: Result<UserEnvelope<GraphUserEmail>, ErrorEnvelope>?
fileprivate let fetchErroredUserBackingsResult: Result<ErroredBackingsEnvelope, ErrorEnvelope>?

Expand Down Expand Up @@ -249,6 +250,7 @@
fetchDraftResponse: UpdateDraft? = nil,
fetchDraftError: ErrorEnvelope? = nil,
fetchGraphUserResult: Result<UserEnvelope<GraphUser>, ErrorEnvelope>? = nil,
fetchGraphUserSelfResult: Result<UserEnvelope<User>, ErrorEnvelope>? = nil,
fetchGraphUserEmailResult: Result<UserEnvelope<GraphUserEmail>, ErrorEnvelope>? = nil,
fetchErroredUserBackingsResult: Result<ErroredBackingsEnvelope, ErrorEnvelope>? = nil,
addAttachmentResponse: UpdateDraft.Image? = nil,
Expand Down Expand Up @@ -362,6 +364,7 @@
self.fetchGraphCategoriesResult = fetchGraphCategoriesResult

self.fetchGraphUserResult = fetchGraphUserResult
self.fetchGraphUserSelfResult = fetchGraphUserSelfResult
self.fetchGraphUserEmailResult = fetchGraphUserEmailResult

self.fetchErroredUserBackingsResult = fetchErroredUserBackingsResult
Expand Down Expand Up @@ -775,6 +778,17 @@
return client.fetchWithResult(query: fetchGraphCategoryQuery, result: self.fetchGraphCategoryResult)
}

internal func fetchGraphUserSelf()
-> SignalProducer<UserEnvelope<User>, ErrorEnvelope> {
guard let client = self.apolloClient else {
return .empty
}

let fetchGraphUserQuery = GraphAPI.FetchUserQuery(withStoredCards: false)

return client.fetchWithResult(query: fetchGraphUserQuery, result: self.fetchGraphUserSelfResult)
}

internal func fetchGraphUser(withStoredCards: Bool)
-> SignalProducer<UserEnvelope<GraphUser>, ErrorEnvelope> {
guard let client = self.apolloClient else {
Expand Down Expand Up @@ -1642,6 +1656,8 @@
exportDataError: $1.exportDataError,
fetchDraftResponse: $1.fetchDraftResponse,
fetchDraftError: $1.fetchDraftError,
fetchGraphUserResult: $1.fetchGraphUserResult,
fetchGraphUserSelfResult: $1.fetchGraphUserSelfResult,
addAttachmentResponse: $1.addAttachmentResponse,
addAttachmentError: $1.addAttachmentError,
removeAttachmentResponse: $1.removeAttachmentResponse,
Expand Down
7 changes: 7 additions & 0 deletions KsApi/Service.swift
Expand Up @@ -344,6 +344,13 @@ public struct Service: ServiceType {
.flatMap(UserEnvelope<GraphUserEmail>.envelopeProducer(from:))
}

public func fetchGraphUserSelf()
-> SignalProducer<UserEnvelope<User>, ErrorEnvelope> {
return GraphQL.shared.client
.fetch(query: GraphAPI.FetchUserQuery(withStoredCards: false))
.flatMap(UserEnvelope<User>.envelopeProducer(from:))
}

public func fetchErroredUserBackings(status: BackingState)
-> SignalProducer<ErroredBackingsEnvelope, ErrorEnvelope> {
guard let status = GraphAPI.BackingState.from(status) else
Expand Down
4 changes: 4 additions & 0 deletions KsApi/ServiceType.swift
Expand Up @@ -169,6 +169,10 @@ public protocol ServiceType {
func fetchGraphUserEmail()
-> SignalProducer<UserEnvelope<GraphUserEmail>, ErrorEnvelope>

/// Fetches GraphQL user fragment and returns User instance.
func fetchGraphUserSelf()
-> SignalProducer<UserEnvelope<User>, ErrorEnvelope>

/// Fetch errored User's backings with a specific status.
func fetchErroredUserBackings(status: BackingState)
-> SignalProducer<ErroredBackingsEnvelope, ErrorEnvelope>
Expand Down
8 changes: 8 additions & 0 deletions KsApi/models/UserEnvelope.swift
Expand Up @@ -23,4 +23,12 @@ extension UserEnvelope {
}
return SignalProducer(value: envelope)
}

static func envelopeProducer(from data: GraphAPI.FetchUserQuery.Data)
-> SignalProducer<UserEnvelope<User>, ErrorEnvelope> {
guard let envelope = UserEnvelope.user(from: data) else {
return .empty
}
return SignalProducer(value: envelope)
}
}
10 changes: 10 additions & 0 deletions KsApi/models/graphql/adapters/UserEnvelope+GraphUserEnvelope.swift
Expand Up @@ -29,6 +29,16 @@ extension UserEnvelope {
return UserEnvelope<GraphUser>(me: graphUser)
}

/**
Returns a `User` from a `FetchUserQuery.Data` object if possible.
*/
static func user(from data: GraphAPI.FetchUserQuery.Data) -> UserEnvelope<User>? {
guard let userFragment = data.me?.fragments.userFragment,
let user = User.user(from: userFragment) else { return nil }

return UserEnvelope<User>(me: user)
}

/**
Returns a `UserEnvelope<GraphUser>` from a `FetchUserQuery.Data` object.
*/
Expand Down
Expand Up @@ -3,7 +3,7 @@ import Foundation
import XCTest

final class UserEnvelope_GraphUserEnvelopeTests: XCTestCase {
func testFetchUser() {
func testFetchUserEvelope_GraphUser_Success() {
let fetchUserQueryData = GraphAPI.FetchUserQuery
.Data(unsafeResultMap: GraphUserEnvelopeTemplates.userJSONDict)

Expand Down Expand Up @@ -35,6 +35,38 @@ final class UserEnvelope_GraphUserEnvelopeTests: XCTestCase {
XCTAssertEqual(envelope.me.uid, "1470952545")
}

func testFetchUserEnvelope_User_Success() {
let fetchUserQueryData = GraphAPI.FetchUserQuery
.Data(unsafeResultMap: GraphUserEnvelopeTemplates.userJSONDict)

guard let envelope = UserEnvelope<User>.userEnvelope(from: fetchUserQueryData) else {
XCTFail()
return
}

XCTAssertNil(envelope.me.chosenCurrency)
XCTAssertEqual(envelope.me.email, "nativesquad@ksr.com")
XCTAssertEqual(envelope.me.hasPassword, true)
XCTAssertEqual(envelope.me.id, "VXNlci0xNDcwOTUyNTQ1")
XCTAssertEqual(
envelope.me.imageUrl,
"https://ksr-qa-ugc.imgix.net/missing_user_avatar.png?ixlib=rb-4.0.2&blur=false&w=1024&h=1024&fit=crop&v=&auto=format&frame=1&q=92&s=e17a7b6f853aa6320cfe67ee783eb3d8"
)
XCTAssertEqual(envelope.me.isAppleConnected, false)
XCTAssertEqual(envelope.me.isCreator, false)
XCTAssertEqual(envelope.me.isDeliverable, true)
XCTAssertEqual(envelope.me.isEmailVerified, true)
XCTAssertEqual(envelope.me.name, "Hari Singh")
XCTAssertEqual(
envelope.me.storedCards,
UserCreditCards(storedCards: [
UserCreditCards
.CreditCard(expirationDate: "2023-01-01", id: "6", lastFour: "4242", type: .visa)
])
)
XCTAssertEqual(envelope.me.uid, "1470952545")
}

func testFetchUserEmail() {
let fetchUserEmailQueryData = GraphAPI.FetchUserEmailQuery
.Data(unsafeResultMap: GraphUserEnvelopeTemplates.userJSONDict)
Expand Down
5 changes: 4 additions & 1 deletion Library/ViewModels/BackerDashboardViewModel.swift
Expand Up @@ -112,7 +112,10 @@ public final class BackerDashboardViewModel: BackerDashboardViewModelType, Backe
self.viewWillAppearProperty.signal.ignoreValues()
)
.switchMap { _ in
AppEnvironment.current.apiService.fetchUserSelf()
AppEnvironment.current.apiService.fetchGraphUserSelf()
.map { envelope in
envelope.me
}
.ksr_delay(AppEnvironment.current.apiDelayInterval, on: AppEnvironment.current.scheduler)
.prefix(SignalProducer([AppEnvironment.current.currentUser].compact()))
.materialize()
Expand Down
16 changes: 12 additions & 4 deletions Library/ViewModels/BackerDashboardViewModelTests.swift
Expand Up @@ -59,7 +59,9 @@ internal final class BackerDashboardViewModelTests: TestCase {
|> \.stats.starredProjectsCount .~ 58
|> \.avatar.large .~ "http://cats.com/furball.jpg"

withEnvironment(apiService: MockService(fetchUserSelfResponse: user)) {
let userEnvelope = UserEnvelope(me: user)

withEnvironment(apiService: MockService(fetchGraphUserSelfResult: .success(userEnvelope))) {
AppEnvironment.login(AccessTokenEnvelope(accessToken: "deadbeef", user: user))

self.avatarURL.assertValueCount(0)
Expand Down Expand Up @@ -104,7 +106,9 @@ internal final class BackerDashboardViewModelTests: TestCase {
|> \.name .~ "user"
|> \.stats.starredProjectsCount .~ 60

withEnvironment(apiService: MockService(fetchUserSelfResponse: user)) {
let userEnvelope = UserEnvelope(me: user)

withEnvironment(apiService: MockService(fetchGraphUserSelfResult: .success(userEnvelope))) {
AppEnvironment.login(AccessTokenEnvelope(accessToken: "deadbeef", user: user))
self.vm.inputs.viewWillAppear(false)

Expand All @@ -115,7 +119,9 @@ internal final class BackerDashboardViewModelTests: TestCase {
let user2 = user
|> \.name .~ "Updated user"

withEnvironment(apiService: MockService(fetchUserSelfResponse: user2)) {
let user2Envelope = UserEnvelope(me: user2)

withEnvironment(apiService: MockService(fetchGraphUserSelfResult: .success(user2Envelope))) {
self.vm.inputs.projectSaved()

self.scheduler.advance()
Expand All @@ -136,7 +142,9 @@ internal final class BackerDashboardViewModelTests: TestCase {
}

func testTabNavigation() {
withEnvironment(apiService: MockService(fetchUserSelfResponse: .template)) {
let userEnvelope = UserEnvelope(me: User.template)

withEnvironment(apiService: MockService(fetchGraphUserSelfResult: .success(userEnvelope))) {
AppEnvironment.login(AccessTokenEnvelope(accessToken: "deadbeef", user: .template))

self.vm.inputs.viewDidLoad()
Expand Down

0 comments on commit 938b714

Please sign in to comment.