users store unit tests
This commit is contained in:
parent
24b0f98ef0
commit
975b338586
16 changed files with 1804 additions and 727 deletions
|
|
@ -1,67 +0,0 @@
|
|||
import { useOAuthStore } from 'src/stores/oauth.js'
|
||||
|
||||
import {
|
||||
fetchUserRelationship,
|
||||
followUser,
|
||||
unfollowUser,
|
||||
} from 'src/api/user.js'
|
||||
|
||||
const fetchRelationship = (attempt, userId, store) =>
|
||||
new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
fetchUserRelationship({
|
||||
id: userId,
|
||||
credentials: useOAuthStore().token,
|
||||
})
|
||||
.then(({ data: relationship }) => {
|
||||
store.commit('updateUserRelationship', [relationship])
|
||||
return relationship
|
||||
})
|
||||
.then((relationship) =>
|
||||
resolve([
|
||||
relationship.following,
|
||||
relationship.requested,
|
||||
relationship.locked,
|
||||
attempt,
|
||||
]),
|
||||
)
|
||||
.catch((e) => reject(e))
|
||||
}, 500)
|
||||
}).then(([following, sent, locked, attempt]) => {
|
||||
if (!following && !(locked && sent) && attempt <= 3) {
|
||||
// If we BE reports that we still not following that user - retry,
|
||||
// increment attempts by one
|
||||
fetchRelationship(++attempt, userId, store)
|
||||
}
|
||||
})
|
||||
|
||||
export const requestFollow = async (userId, store) => {
|
||||
const { data: updated } = await followUser({
|
||||
id: userId,
|
||||
credentials: useOAuthStore().token,
|
||||
})
|
||||
|
||||
store.commit('updateUserRelationship', [updated])
|
||||
|
||||
if (updated.following || (updated.locked && updated.requested)) {
|
||||
// If we get result immediately or the account is locked, just stop.
|
||||
return
|
||||
}
|
||||
|
||||
// But usually we don't get result immediately, so we ask server
|
||||
// for updated user profile to confirm if we are following them
|
||||
// Sometimes it takes several tries. Sometimes we end up not following
|
||||
// user anyway, probably because they locked themselves and we
|
||||
// don't know that yet.
|
||||
// Recursive Promise, it will call itself up to 3 times.
|
||||
return await fetchRelationship(1, updated, store)
|
||||
}
|
||||
|
||||
export const requestUnfollow = async (userId, store) => {
|
||||
const { data: updated } = await unfollowUser({
|
||||
id: userId,
|
||||
credentials: useOAuthStore().token,
|
||||
})
|
||||
|
||||
return await store.commit('updateUserRelationship', [updated])
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue