diff --git a/src/components/Challenge/ChallengeCardOnGoing/ChallengeCardOnGoing.tsx b/src/components/Challenge/ChallengeCardOnGoing/ChallengeCardOnGoing.tsx index b6e1ba070fef77130c61540cb384265af4137e16..0caf08f963987cb860bdb8e540bfb135e6c96f80 100644 --- a/src/components/Challenge/ChallengeCardOnGoing/ChallengeCardOnGoing.tsx +++ b/src/components/Challenge/ChallengeCardOnGoing/ChallengeCardOnGoing.tsx @@ -117,7 +117,7 @@ const ChallengeCardOnGoing = ({ useEffect(() => { let subscribed = true async function setChallengeResult() { - const isChallengeDone = await challengeService.isChallengeDone( + const isChallengeDone = challengeService.isChallengeDone( userChallenge, currentDataload ) diff --git a/src/components/Duel/DuelOngoing/DuelOngoing.spec.tsx b/src/components/Duel/DuelOngoing/DuelOngoing.spec.tsx index caadee189cd8e0b9668787c3b9b32c6a82407cc5..5e94655971924fe66cf8e420851f53cec0072d66 100644 --- a/src/components/Duel/DuelOngoing/DuelOngoing.spec.tsx +++ b/src/components/Duel/DuelOngoing/DuelOngoing.spec.tsx @@ -22,10 +22,9 @@ jest.mock('components/Duel/DuelChart/DuelChart', () => 'mock-duelchart') describe('DuelOngoing component', () => { const store = createMockEcolyoStore() it('should be rendered correctly', async () => { - mockIsChallengeDone.mockResolvedValue({ + mockIsChallengeDone.mockReturnValue({ isDone: false, isWin: false, - isEmpty: false, }) const { container } = render( <Provider store={store}> @@ -44,7 +43,7 @@ describe('DuelOngoing component', () => { state: UserChallengeState.DUEL, startDate: DateTime.local().setZone('utc', { keepLocalTime: true }), } - mockIsChallengeDone.mockResolvedValue({ + mockIsChallengeDone.mockReturnValue({ isDone: true, isWin: true, }) diff --git a/src/components/Duel/DuelOngoing/DuelOngoing.tsx b/src/components/Duel/DuelOngoing/DuelOngoing.tsx index 8dc0b7cbb6e73d47f36bf00b3188dfe581ed3761..54cfc11db89d6ae3d9fe333d1d6b2772fdc7bbf2 100644 --- a/src/components/Duel/DuelOngoing/DuelOngoing.tsx +++ b/src/components/Duel/DuelOngoing/DuelOngoing.tsx @@ -39,14 +39,18 @@ const DuelOngoing = ({ userChallenge, isFinished }: DuelOngoingProps) => { const { currentDataload, userChallengeList } = useAppSelector( state => state.ecolyo.challenge ) - const [resultModal, setResultModal] = useState<boolean>(false) - const [winChallenge, setWinChallenge] = useState<boolean>(false) + const [showLastDuelModal, setShowLastDuelModal] = useState<boolean>(false) const [finishedDataLoad, setFinishedDataLoad] = useState<Dataload[]>() const chartContainer = useRef<HTMLDivElement>(null) const { height, width } = useChartResize(chartContainer, false) const challengeService = useMemo(() => new ChallengeService(client), [client]) + const { isDone, isWin } = challengeService.isChallengeDone( + userChallenge, + currentDataload + ) + const duel = userChallenge.duel const isLastDuel = @@ -60,7 +64,7 @@ const DuelOngoing = ({ userChallenge, isFinished }: DuelOngoingProps) => { const setResult = useCallback(async () => { const updatedChallenge = await challengeService.updateUserChallenge( userChallenge, - winChallenge + isWin ? UserChallengeUpdateFlag.DUEL_WIN : UserChallengeUpdateFlag.DUEL_LOSS ) @@ -73,32 +77,7 @@ const DuelOngoing = ({ userChallenge, isFinished }: DuelOngoingProps) => { } else { navigate('/challenges') } - }, [ - challengeService, - userChallenge, - winChallenge, - dispatch, - isLastDuel, - navigate, - ]) - - useEffect(() => { - let subscribed = true - async function setChallengeResult() { - const { isDone, isWin } = await challengeService.isChallengeDone( - userChallenge, - currentDataload - ) - if (subscribed) { - setResultModal(isDone) - setWinChallenge(isWin) - } - } - setChallengeResult() - return () => { - subscribed = false - } - }, [challengeService, client, currentDataload, userChallenge]) + }, [challengeService, userChallenge, isWin, dispatch, isLastDuel, navigate]) useEffect(() => { let subscribed = true @@ -178,9 +157,9 @@ const DuelOngoing = ({ userChallenge, isFinished }: DuelOngoingProps) => { </div> </div> <DuelResultModal - open={resultModal} + open={isDone} userChallenge={userChallenge} - win={winChallenge} + win={isWin} handleCloseClick={setResult} /> <LastDuelModal diff --git a/src/components/Quiz/QuizQuestion/QuizQuestionContent.tsx b/src/components/Quiz/QuizQuestion/QuizQuestionContent.tsx index fd9b2c8f40d9e793bbd58a3192760c361f29b1e3..5fc440334d3429f9b521f4f4a5f0e34abc22a996 100644 --- a/src/components/Quiz/QuizQuestion/QuizQuestionContent.tsx +++ b/src/components/Quiz/QuizQuestion/QuizQuestionContent.tsx @@ -48,7 +48,7 @@ const QuizQuestionContent = ({ ) setAnswerIndex(resultIndex) setOpenModal(true) - const quizUpdated = await quizService.updateUserQuiz( + const quizUpdated = quizService.updateUserQuiz( userChallenge.quiz, result[0].isTrue, questionIndex diff --git a/src/components/Quiz/QuizQuestion/QuizQuestionContentCustom.tsx b/src/components/Quiz/QuizQuestion/QuizQuestionContentCustom.tsx index e9fab89f39c7bc1c65796a7320df9c6f450dcc28..c4bfc61678efc9b819951b3bd4776d97328de965 100644 --- a/src/components/Quiz/QuizQuestion/QuizQuestionContentCustom.tsx +++ b/src/components/Quiz/QuizQuestion/QuizQuestionContentCustom.tsx @@ -44,7 +44,7 @@ const QuizQuestionContentCustom = ({ const result = customQuestion.answers.filter( answer => answer.answerLabel === userChoice ) - const quizUpdated = await quizService.updateUserQuiz( + const quizUpdated = quizService.updateUserQuiz( userChallenge.quiz, result[0].isTrue ) diff --git a/src/components/Splash/SplashRoot.tsx b/src/components/Splash/SplashRoot.tsx index b95191a5aeaad64a7cd28a7c5b562105d49bb4fd..0a4cd8b72bcd6eed32f8d10ab9843876844c627b 100644 --- a/src/components/Splash/SplashRoot.tsx +++ b/src/components/Splash/SplashRoot.tsx @@ -326,7 +326,7 @@ const SplashRoot = ({ fadeTimer = 1000, children }: SplashRootProps) => { ) // Check is duel is done and display notification const challengeService = new ChallengeService(client) - const { isDone } = await challengeService.isChallengeDone( + const { isDone } = challengeService.isChallengeDone( updatedUserChallenge, dataloads ) diff --git a/src/services/challenge.service.spec.ts b/src/services/challenge.service.spec.ts index d7da26cd4891d6bf4ff6a6971d61e08a62596384..68964b9d7545d9d1e87b41c2cddc69edbc920643 100644 --- a/src/services/challenge.service.spec.ts +++ b/src/services/challenge.service.spec.ts @@ -474,14 +474,14 @@ describe('Challenge service', () => { ], }, ] - it('should return isDone = true, isWin = true and isEmpty=false when userConsumption < threshold', async () => { - const result = await challengeService.isChallengeDone( + it('should return isDone = true, isWin = true and isEmpty=false when userConsumption < threshold', () => { + const result = challengeService.isChallengeDone( userChallenge, dataloads ) expect(result).toEqual({ isDone: true, isWin: true }) }) - it('should return isDone = true, isWin = false when userConsumption >= threshold', async () => { + it('should return isDone = true, isWin = false when userConsumption >= threshold', () => { const updatedUserChallenge = { ...userChallenge, duel: { @@ -490,7 +490,7 @@ describe('Challenge service', () => { userConsumption: 200, }, } - const result = await challengeService.isChallengeDone( + const result = challengeService.isChallengeDone( updatedUserChallenge, dataloads ) @@ -559,15 +559,15 @@ describe('Challenge service', () => { ], }, ] - it('should return isDone = true, isWin = true when all data are available and userConsumption < threshold', async () => { - const result = await challengeService.isChallengeDone( + it('should return isDone = true, isWin = true when all data are available and userConsumption < threshold', () => { + const result = challengeService.isChallengeDone( userChallenge, dataloads ) expect(result).toEqual({ isDone: true, isWin: true }) }) - it('should return isDone = true and isWin = false when all data are available and userConsumption >= threshold', async () => { + it('should return isDone = true and isWin = false when all data are available and userConsumption >= threshold', () => { const updatedUserChallenge = { ...userChallenge, duel: { @@ -576,30 +576,30 @@ describe('Challenge service', () => { userConsumption: 200, }, } - const result = await challengeService.isChallengeDone( + const result = challengeService.isChallengeDone( updatedUserChallenge, dataloads ) expect(result).toEqual({ isDone: true, isWin: false }) }) - it('should return isDone = false and isWin = false when last data is not available', async () => { + it('should return isDone = false and isWin = false when last data is not available', () => { const updatedDataloads = cloneDeep(dataloads) updatedDataloads[2].value = -1 updatedDataloads[2].valueDetail = null - const result = await challengeService.isChallengeDone( + const result = challengeService.isChallengeDone( userChallenge, updatedDataloads ) expect(result).toEqual({ isDone: false, isWin: false }) }) - it('should return isDone = false and isWin = false when data in the middle is not available', async () => { + it('should return isDone = false and isWin = false when data in the middle is not available', () => { const updatedDataloads = cloneDeep(dataloads) updatedDataloads[1].valueDetail = [ { value: 20.0, state: DataloadState.VALID }, { value: -1, state: DataloadState.MISSING }, { value: 10.0, state: DataloadState.VALID }, ] - const result = await challengeService.isChallengeDone( + const result = challengeService.isChallengeDone( userChallenge, updatedDataloads ) diff --git a/src/services/challenge.service.ts b/src/services/challenge.service.ts index fd8e7242b58d8a166140e971a13706755cbca0b1..d7ec39709a219a70a68148e4d140345db1c5b1b4 100644 --- a/src/services/challenge.service.ts +++ b/src/services/challenge.service.ts @@ -151,10 +151,10 @@ export default class ChallengeService { * @param {UserExploration | ExplorationEntity} exploration - Exploration to be tested * @returns {boolean} isValid */ - public async isExplorationConditionVerified( + public isExplorationConditionVerified( exploration: UserExploration | ExplorationEntity, fluidStatus: FluidStatus[] - ): Promise<boolean> { + ): boolean { let isValid = false const fluidCondition = exploration.fluid_condition // check if the fluid is connected @@ -192,17 +192,17 @@ export default class ChallengeService { /** * Get a UserChallenge from its Entity and verify the exploration if there is a condition and if it's verified */ - public async getUpdatedUserChallengeIfExplorationConditionIsValid( + public getUpdatedUserChallengeIfExplorationConditionIsValid( exploration: UserExploration, challenge: ChallengeEntity, duel: UserDuel, quiz: UserQuiz, fluidStatus: FluidStatus[] - ): Promise<UserChallenge | undefined> { + ): UserChallenge | undefined { let userChallenge: UserChallenge | null = null // Check if it's a conditional exploration if (exploration.fluid_condition.length > 0) { - const isConditionVerified = await this.isExplorationConditionVerified( + const isConditionVerified = this.isExplorationConditionVerified( exploration, fluidStatus ) @@ -232,7 +232,7 @@ export default class ChallengeService { * Exploration related to given challenge * @returns {UserChallenge[]} - buildList */ - public async processExploration( + public processExploration( explorationEntities: ExplorationEntity[] | undefined, explorationEntityRelation: Relation[], challenge: ChallengeEntity, @@ -240,7 +240,7 @@ export default class ChallengeService { quiz: UserQuiz, buildList: UserChallenge[], fluidStatus: FluidStatus[] - ): Promise<UserChallenge[]> { + ): UserChallenge[] { const explorationService = new ExplorationService(this._client) for (const explorationRelation of explorationEntityRelation) { const exploration = @@ -249,7 +249,7 @@ export default class ChallengeService { explorationRelation._id ) const userChallenge = - await this.getUpdatedUserChallengeIfExplorationConditionIsValid( + this.getUpdatedUserChallengeIfExplorationConditionIsValid( exploration, challenge, duel, @@ -286,7 +286,7 @@ export default class ChallengeService { newExploEntity ) if (newExploEntity.fluid_condition.length > 0) { - const isConditionValid = await this.isExplorationConditionVerified( + const isConditionValid = this.isExplorationConditionVerified( newExploEntity, fluidStatus ) @@ -369,7 +369,7 @@ export default class ChallengeService { } // Several explorations with fluid condition else { - await this.processExploration( + this.processExploration( explorationEntities, relationEntities.explorationEntityRelation, challenge, @@ -406,7 +406,7 @@ export default class ChallengeService { quizEntities || [], relationEntities.quizEntityRelation._id ) - await this.processExploration( + this.processExploration( explorationEntities, relationEntities.explorationEntityRelation, challenge, @@ -580,7 +580,7 @@ export default class ChallengeService { updatedUserChallenge = userChallenge break case UserChallengeUpdateFlag.DUEL_UNLOCK: - updatedDuel = await duelService.unlockUserDuel(userChallenge.duel) + updatedDuel = duelService.unlockUserDuel(userChallenge.duel) updatedUserChallenge = { ...userChallenge, state: UserChallengeState.DUEL, @@ -599,7 +599,7 @@ export default class ChallengeService { } break case UserChallengeUpdateFlag.DUEL_START: - updatedDuel = await duelService.startUserDuel(userChallenge.duel) + updatedDuel = duelService.startUserDuel(userChallenge.duel) updatedUserChallenge = { ...userChallenge, state: UserChallengeState.DUEL, @@ -607,7 +607,7 @@ export default class ChallengeService { } break case UserChallengeUpdateFlag.DUEL_WIN: - updatedDuel = await duelService.endUserDuel(userChallenge.duel) + updatedDuel = duelService.endUserDuel(userChallenge.duel) updatedUserChallenge = { ...userChallenge, state: UserChallengeState.DONE, @@ -621,7 +621,7 @@ export default class ChallengeService { } break case UserChallengeUpdateFlag.DUEL_LOSS: - updatedDuel = await duelService.endUserDuel(userChallenge.duel) + updatedDuel = duelService.endUserDuel(userChallenge.duel) updatedUserChallenge = { ...userChallenge, state: UserChallengeState.DONE, @@ -635,7 +635,7 @@ export default class ChallengeService { } break case UserChallengeUpdateFlag.DUEL_RESET: - updatedDuel = await duelService.resetUserDuel(userChallenge.duel) + updatedDuel = duelService.resetUserDuel(userChallenge.duel) updatedUserChallenge = { ...userChallenge, state: UserChallengeState.DUEL, @@ -644,7 +644,7 @@ export default class ChallengeService { } break case UserChallengeUpdateFlag.QUIZ_START: - updatedQuiz = await quizService.startUserQuiz(userChallenge.quiz) + updatedQuiz = quizService.startUserQuiz(userChallenge.quiz) updatedUserChallenge = { ...userChallenge, quiz: updatedQuiz, @@ -652,7 +652,7 @@ export default class ChallengeService { break case UserChallengeUpdateFlag.QUIZ_DONE: { const updateQuizProgress = Math.min(userChallenge.quiz.result, 5) - updatedQuiz = await quizService.endUserQuiz(userChallenge.quiz) + updatedQuiz = quizService.endUserQuiz(userChallenge.quiz) updatedUserChallenge = { ...userChallenge, quiz: updatedQuiz, @@ -672,14 +672,14 @@ export default class ChallengeService { } break case UserChallengeUpdateFlag.QUIZ_RESET: - updatedQuiz = await quizService.resetUserQuiz(userChallenge.quiz) + updatedQuiz = quizService.resetUserQuiz(userChallenge.quiz) updatedUserChallenge = { ...userChallenge, quiz: updatedQuiz, } break case UserChallengeUpdateFlag.EXPLORATION_START: - updatedExploration = await explorationService.startUserExploration( + updatedExploration = explorationService.startUserExploration( userChallenge.exploration ) updatedUserChallenge = { @@ -688,7 +688,7 @@ export default class ChallengeService { } break case UserChallengeUpdateFlag.EXPLORATION_UPDATE: - updatedExploration = await explorationService.updateUserExploration( + updatedExploration = explorationService.updateUserExploration( userChallenge.exploration ) updatedUserChallenge = { @@ -697,10 +697,9 @@ export default class ChallengeService { } break case UserChallengeUpdateFlag.EXPLORATION_NOTIFICATION: - updatedExploration = - await explorationService.setNotificationUserExploration( - userChallenge.exploration - ) + updatedExploration = explorationService.setNotificationUserExploration( + userChallenge.exploration + ) updatedUserChallenge = { ...userChallenge, exploration: updatedExploration, @@ -804,13 +803,13 @@ export default class ChallengeService { * @param {UserChallenge} userChallenge - current userChallenge * @param {Dataload[]} dataloads - dataloads of current challenge */ - public async isChallengeDone( + public isChallengeDone( userChallenge: UserChallenge, dataloads: Dataload[] - ): Promise<{ + ): { isDone: boolean isWin: boolean - }> { + } { let isDone = false let isWin = false if ( diff --git a/src/services/duel.service.spec.ts b/src/services/duel.service.spec.ts index 16936841cdfde122ece5f3342f72b4941ef0b809..bb67cbeab4b2bdf8ba5859952a5a9ef62124ad7b 100644 --- a/src/services/duel.service.spec.ts +++ b/src/services/duel.service.spec.ts @@ -96,13 +96,13 @@ describe('Duel service', () => { }) describe('startUserDuel method', () => { - it('should return the userDuel with onGoing state', async () => { + it('should return the userDuel with onGoing state', () => { jest .spyOn(DateTime, 'local') .mockReturnValue( DateTime.fromISO('2020-10-01T00:00:00.000Z', { zone: 'utc' }) ) - const result = await duelService.startUserDuel(duelData) + const result = duelService.startUserDuel(duelData) const mockUpdatedDuel: UserDuel = { ...duelData, @@ -116,8 +116,8 @@ describe('Duel service', () => { }) describe('resetUserDuel method', () => { - it('should return the userDuel with unlocked state', async () => { - const result = await duelService.resetUserDuel(duelData) + it('should return the userDuel with unlocked state', () => { + const result = duelService.resetUserDuel(duelData) const mockUpdatedDuel: UserDuel = { ...duelData, startDate: null, diff --git a/src/services/duel.service.ts b/src/services/duel.service.ts index 268257abdd20db4676fd139a9b5aac10aa0e2ecb..747f588f629f203f9d7971316c2b933ad7d96adb 100644 --- a/src/services/duel.service.ts +++ b/src/services/duel.service.ts @@ -144,13 +144,13 @@ export default class DuelService { * Return duel with updated state to UserDuelState.UNLOCKED * @param {UserDuel} userDuel - userDuel to unlock */ - public async unlockUserDuel(userDuel: UserDuel): Promise<UserDuel> { - const updatedUserDuel: UserDuel = { + public unlockUserDuel(userDuel: UserDuel): UserDuel { + return { ...userDuel, state: UserDuelState.UNLOCKED, } - return updatedUserDuel } + private getFluidTypesFromStatus(fluidStatus: FluidStatus[]): FluidType[] { const fluidTypes: FluidType[] = [] fluidStatus.forEach(fluid => { @@ -214,34 +214,32 @@ export default class DuelService { * Return duel with updated state to UserDuelState.ONGOING and startDate * @param {UserDuel} userDuel - userDuel to update */ - public async startUserDuel(userDuel: UserDuel): Promise<UserDuel> { - const updatedUserDuel: UserDuel = { + public startUserDuel(userDuel: UserDuel): UserDuel { + return { ...userDuel, state: UserDuelState.ONGOING, startDate: DateTime.local() .setZone('utc', { keepLocalTime: true }) .startOf('day'), } - return updatedUserDuel } /** * Return duel with updated state to UserDuelState.DONE * @param {UserDuel} userDuel - userDuel to update */ - public async endUserDuel(userDuel: UserDuel): Promise<UserDuel> { - const updatedUserDuel: UserDuel = { + public endUserDuel(userDuel: UserDuel): UserDuel { + return { ...userDuel, state: UserDuelState.DONE, } - return updatedUserDuel } /** * Return duel with updated state to UserDuelState.UNLOCKED * @param {UserDuel} userDuel - userDuel to reset */ - public async resetUserDuel(userDuel: UserDuel): Promise<UserDuel> { + public resetUserDuel(userDuel: UserDuel): UserDuel { return { ...userDuel, startDate: null, @@ -256,7 +254,7 @@ export default class DuelService { * @param {DuelEntity} duel - userDuel to update */ public parseDuelEntityToDuel(duel: DuelEntity): UserDuel { - const userDuel: UserDuel = { + return { id: duel.id, title: duel.title, description: duel.description, @@ -267,7 +265,6 @@ export default class DuelService { fluidTypes: [], userConsumption: 0, } - return userDuel } /** diff --git a/src/services/exploration.service.spec.ts b/src/services/exploration.service.spec.ts index 6307effc4aef9d291c42d50757d3cea92b049322..da61c3b29e4a2336430934e3faaaeb7a711c53c3 100644 --- a/src/services/exploration.service.spec.ts +++ b/src/services/exploration.service.spec.ts @@ -110,8 +110,8 @@ describe('Exploration service', () => { }) }) describe('startUserExploration Method', () => { - it('should return the started userExploration', async () => { - const result = await explorationService.startUserExploration( + it('should return the started userExploration', () => { + const result = explorationService.startUserExploration( userExplorationUnlocked ) expect(result).toEqual(userExplorationStarted) diff --git a/src/services/exploration.service.ts b/src/services/exploration.service.ts index f0b9eeab238ca23638db1c38e1f289e8593bd610..446f58adf11b6ecd49f4475dc7ce7f3cb1ccefeb 100644 --- a/src/services/exploration.service.ts +++ b/src/services/exploration.service.ts @@ -115,56 +115,51 @@ export default class ExplorationService { * Return exploration with updated state to UserExplorationState.ONGOING * @param {UserExploration} userExploration - userExploration to update */ - public async startUserExploration( + public startUserExploration( userExploration: UserExploration - ): Promise<UserExploration> { - const updatedUserExploration: UserExploration = { + ): UserExploration { + return { ...userExploration, state: UserExplorationState.ONGOING, } - return updatedUserExploration } /** * Return exploration with updated state to UserExplorationState.NOTIFICATION * @param {UserExploration} userExploration - userExploration to update */ - public async setNotificationUserExploration( + public setNotificationUserExploration( userExploration: UserExploration - ): Promise<UserExploration> { - const updatedUserExploration: UserExploration = { + ): UserExploration { + return { ...userExploration, state: UserExplorationState.NOTIFICATION, progress: 5, // userExploration.type === UserExplorationType.ACTION ? 3 : 1, } - return updatedUserExploration } /** * Return exploration with updated state to UserExplorationState.DONE * @param {UserExploration} userExploration - userExploration to update */ public endUserExploration(userExploration: UserExploration): UserExploration { - const updatedUserExploration: UserExploration = { + return { ...userExploration, state: UserExplorationState.DONE, progress: 5, // userExploration.type === UserExplorationType.ACTION ? 3 : 1, } - return updatedUserExploration } /** * Return updated exploration * @param {UserExploration} userExploration - userExploration to update */ - public async updateUserExploration( + public updateUserExploration( userExploration: UserExploration - ): Promise<UserExploration> { - const updatedProgress = userExploration.progress + 1 - const updatedUserExploration: UserExploration = { + ): UserExploration { + return { ...userExploration, - progress: updatedProgress, + progress: userExploration.progress + 1, } - return updatedUserExploration } /** diff --git a/src/services/initialization.service.spec.ts b/src/services/initialization.service.spec.ts index 2b9ddb68e79a1007a5e56e91d444ce3efb447239..894bd045119f03f9464183c764220878155abd0f 100644 --- a/src/services/initialization.service.spec.ts +++ b/src/services/initialization.service.spec.ts @@ -599,19 +599,18 @@ describe('Initialization service', () => { }) describe('initAnalysis method', () => { - it('should return monthlyAnalysisDate and haveSeenLastAnalysis when analysis is up to date', async () => { + it('should return monthlyAnalysisDate and haveSeenLastAnalysis when analysis is up to date', () => { const profile: Profile = { ...mockProfileState, monthlyAnalysisDate: getActualAnalysisDate(), } - await expect( - initializationService.initAnalysis(profile) - ).resolves.toEqual({ + const result = initializationService.initAnalysis(profile) + expect(result).toEqual({ monthlyAnalysisDate: getActualAnalysisDate(), haveSeenLastAnalysis: profile.haveSeenLastAnalysis, }) }) - it('should return updated monthlyAnalysisDate and haveSeenLastAnalysis=true when analysis is not up to date and isFirstConnection', async () => { + it('should return updated monthlyAnalysisDate and haveSeenLastAnalysis=true when analysis is not up to date and isFirstConnection', () => { const profile: Profile = { ...mockProfileState, monthlyAnalysisDate: DateTime.fromISO('2000-10-02T00:00:00.000Z', { @@ -620,14 +619,13 @@ describe('Initialization service', () => { haveSeenLastAnalysis: true, isFirstConnection: true, } - await expect( - initializationService.initAnalysis(profile) - ).resolves.toEqual({ + const result = initializationService.initAnalysis(profile) + expect(result).toEqual({ monthlyAnalysisDate: getActualAnalysisDate(), haveSeenLastAnalysis: true, }) }) - it('should return updated monthlyAnalysisDate and haveSeenLastAnalysis=false when analysis is not up to date and isFirstConnection is false', async () => { + it('should return updated monthlyAnalysisDate and haveSeenLastAnalysis=false when analysis is not up to date and isFirstConnection is false', () => { const profile: Profile = { ...mockProfileState, isFirstConnection: false, @@ -636,9 +634,8 @@ describe('Initialization service', () => { }), haveSeenLastAnalysis: true, } - await expect( - initializationService.initAnalysis(profile) - ).resolves.toEqual({ + const result = initializationService.initAnalysis(profile) + expect(result).toEqual({ monthlyAnalysisDate: getActualAnalysisDate(), haveSeenLastAnalysis: false, }) diff --git a/src/services/initialization.service.ts b/src/services/initialization.service.ts index 82030b760263f029a3a4097a8160fb60220e41df..09ee0f4a2baf2c9a6ffb919f39ffa809f9710255 100644 --- a/src/services/initialization.service.ts +++ b/src/services/initialization.service.ts @@ -467,10 +467,10 @@ export default class InitializationService { } } - public async initAnalysis(profile: Profile): Promise<{ + public initAnalysis(profile: Profile): { monthlyAnalysisDate: DateTime haveSeenLastAnalysis: boolean - }> { + } { const startTime = performance.now() try { const actualAnalysisDate = getActualAnalysisDate() @@ -510,8 +510,8 @@ export default class InitializationService { /** * Check if FluidTypes exist - * success return: FluidType[] - * failure throw error + * - success return: FluidType[] + * - failure throw error */ public async initFluidTypes(): Promise<FluidType[]> { const startTime = performance.now() @@ -540,8 +540,8 @@ export default class InitializationService { /** * For each fluid get the trigger status and the last data date - * success return: FluidStatus[] - * failure throw error + * - success return: FluidStatus[] + * - failure throw error */ public async initFluidStatus(): Promise<FluidStatus[]> { const startTime = performance.now() diff --git a/src/services/quiz.service.spec.ts b/src/services/quiz.service.spec.ts index 36d9fae2b69d555b6a8feb60841ffc3f8f5958e6..355120bcdfb0f6554ed3d3c84c12b2494a20af03 100644 --- a/src/services/quiz.service.spec.ts +++ b/src/services/quiz.service.spec.ts @@ -150,31 +150,31 @@ describe('Quiz service', () => { }) }) describe('endUserQuiz Method', () => { - it('should return the finished userQuiz', async () => { - const result = await quizService.endUserQuiz(quizDefault) + it('should return the finished userQuiz', () => { + const result = quizService.endUserQuiz(quizDefault) expect(result).toEqual(UserQuizDone) }) }) describe('startUserQuiz Method', () => { - it('should return the started userQuiz', async () => { + it('should return the started userQuiz', () => { jest .spyOn(DateTime, 'local') .mockReturnValueOnce( DateTime.fromISO('2021-01-01T00:00:00.000Z', { zone: 'utc' }) ) - const result = await quizService.startUserQuiz(quizDefault) + const result = quizService.startUserQuiz(quizDefault) expect(result).toEqual(UserQuizStarted) }) }) describe('resetUserQuiz Method', () => { - it('should return the reseted userQuiz', async () => { - const result = await quizService.resetUserQuiz(UserQuizDone) + it('should return the reseted userQuiz', () => { + const result = quizService.resetUserQuiz(UserQuizDone) expect(result).toEqual(UserQuizReseted) }) }) describe('updateUserQuiz Method', () => { - it('should return the userQuiz with updated result and correct answer to question', async () => { - const result = await quizService.updateUserQuiz(userQuiz, true, 0) + it('should return the userQuiz with updated result and correct answer to question', () => { + const result = quizService.updateUserQuiz(userQuiz, true, 0) const mockUpdatedQuestion: UserQuestion = { ...userQuiz.questions[0], result: UserQuestionState.CORRECT, @@ -187,8 +187,8 @@ describe('Quiz service', () => { } expect(result).toEqual(mockUpdatedQuiz) }) - it('should return the userQuiz with updated result and wrong answer to question', async () => { - const result = await quizService.updateUserQuiz(userQuiz, false, 2) + it('should return the userQuiz with updated result and wrong answer to question', () => { + const result = quizService.updateUserQuiz(userQuiz, false, 2) const mockUpdatedQuestion: UserQuestion = { ...userQuiz.questions[2], result: UserQuestionState.INCORRECT, @@ -201,8 +201,8 @@ describe('Quiz service', () => { } expect(result).toEqual(mockUpdatedQuiz) }) - it('should return the userQuiz with updated result and right answer to customQuestion', async () => { - const result = await quizService.updateUserQuiz(userQuiz, true) + it('should return the userQuiz with updated result and right answer to customQuestion', () => { + const result = quizService.updateUserQuiz(userQuiz, true) const mockUpdatedCustomQuestion: UserCustomQuestion = { ...userQuiz.customQuestion, result: UserQuestionState.CORRECT, diff --git a/src/services/quiz.service.ts b/src/services/quiz.service.ts index a97ecff9ad170507442e896b0bf1d70756869dc6..6dac376e5b60fed7502cb3afa8b1003505b52916 100644 --- a/src/services/quiz.service.ts +++ b/src/services/quiz.service.ts @@ -161,7 +161,7 @@ export default class QuizService { * Return quiz with updated state to UserQuizState.ONGOING and randomize question and answers * @param {UserQuiz} userQuiz - userQuiz to update */ - public async startUserQuiz(userQuiz: UserQuiz): Promise<UserQuiz> { + public startUserQuiz(userQuiz: UserQuiz): UserQuiz { const questions = userQuiz.questions.map(question => ({ ...question, answers: shuffle(question.answers), @@ -180,7 +180,7 @@ export default class QuizService { * Return quiz with updated state to UserQuizState.UNLOCKED and updated questions with false result * @param {UserQuiz} userQuiz - userQuiz to update */ - public async resetUserQuiz(userQuiz: UserQuiz): Promise<UserQuiz> { + public resetUserQuiz(userQuiz: UserQuiz): UserQuiz { const updatedQuestions = userQuiz.questions.map(question => ({ ...question, result: UserQuestionState.UNLOCKED, @@ -202,23 +202,22 @@ export default class QuizService { * Return quiz with updated state to UserQuizState.DONE * @param {UserQuiz} userQuiz - userQuiz to update */ - public async endUserQuiz(userQuiz: UserQuiz): Promise<UserQuiz> { - const updatedUserQuiz: UserQuiz = { + public endUserQuiz(userQuiz: UserQuiz): UserQuiz { + return { ...userQuiz, state: UserQuizState.DONE, } - return updatedUserQuiz } /** * Return quiz with result and updated question or customQuestion if no index is passed * @param {UserQuiz} userQuiz - userQuiz to update */ - public async updateUserQuiz( + public updateUserQuiz( userQuiz: UserQuiz, questionResult: boolean, questionIndex?: number - ): Promise<UserQuiz> { + ): UserQuiz { const result = questionResult ? UserQuestionState.CORRECT : UserQuestionState.INCORRECT