From 671fa574bf94484788d6d56e76425384bb05d3cf Mon Sep 17 00:00:00 2001 From: benguedj <71835422+benguedj@users.noreply.github.com> Date: Tue, 13 Sep 2022 16:29:27 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Corrige=20une=20erreur=20?= =?UTF-8?q?sentry=20'b.includes=20is=20not=20a=20function'=20(#1433)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 🐛 Corrige une erreur sentry 'b.includes is not a function' ✅ Closes: #1426 * fix: 🐛 PR erreur sentry 'b.includes is not a function' --- .../notifications/notification.util.test.ts | 97 ++++++++++++++++--- .../utils/notifications/notification.util.ts | 32 ++++-- 2 files changed, 109 insertions(+), 20 deletions(-) diff --git a/front/src/utils/notifications/notification.util.test.ts b/front/src/utils/notifications/notification.util.test.ts index e643518a6..b935fc963 100644 --- a/front/src/utils/notifications/notification.util.test.ts +++ b/front/src/utils/notifications/notification.util.test.ts @@ -8,11 +8,7 @@ import { } from "../../constants"; import type { Step } from "../../types"; import { NotificationUtils, StorageUtils } from ".."; -import { - getNotificationTrigger, - NotificationType, - saveStepForCongratNotifScheduled, -} from "./notification.util"; +import { NotificationType } from "./notification.util"; describe("Notification utils", () => { describe("Build Articles Notification Content", () => { @@ -107,7 +103,7 @@ describe("Notification utils", () => { }); it("getNotificationTrigger with 0 article and no trigger", async () => { - const result = await getNotificationTrigger(0, null); + const result = await NotificationUtils.getNotificationTrigger(0, null); const expected = NotificationConstants.MIN_TRIGGER; expect(result).toEqual(expected); @@ -115,14 +111,17 @@ describe("Notification utils", () => { it("getNotificationTrigger with articles", async () => { const notifTrigger = { seconds: 20 }; - const result = await getNotificationTrigger(5, notifTrigger); + const result = await NotificationUtils.getNotificationTrigger( + 5, + notifTrigger + ); const expected = { seconds: 20 }; expect(result).toEqual(expected); }); it("getNotificationTrigger with articles and no trigger", async () => { - const result = await getNotificationTrigger(5, null); + const result = await NotificationUtils.getNotificationTrigger(5, null); const expected = new Date( addDays( new Date(), @@ -139,13 +138,83 @@ describe("Notification utils", () => { }); }); + describe("hasBeenAlreadyNotifiedForArticles", () => { + afterEach(() => { + void AsyncStorage.clear(); + }); + + it("hasBeenAlreadyNotifiedForArticles with currentStep is null", () => { + const CURRENT_STEP = null; + const stepsAlreadyCongratulatedForArticles = ["6"]; + const data = NotificationUtils.hasBeenAlreadyNotifiedForArticles( + CURRENT_STEP, + stepsAlreadyCongratulatedForArticles + ); + expect(data).toBeFalsy(); + }); + + it("hasBeenAlreadyNotifiedForArticles with stepsAlreadyCongratulatedForArticles is null", () => { + const CURRENT_STEP = { + active: true, + debut: 0, + description: null, + fin: 90, + id: 6, + nom: "De 0 à 3 mois", + ordre: 6, + }; + const stepsAlreadyCongratulatedForArticles = null; + const data = NotificationUtils.hasBeenAlreadyNotifiedForArticles( + CURRENT_STEP, + stepsAlreadyCongratulatedForArticles + ); + expect(data).toBeFalsy(); + }); + + it("hasBeenAlreadyNotifiedForArticles is false", () => { + const CURRENT_STEP = { + active: true, + debut: 0, + description: null, + fin: 90, + id: 6, + nom: "De 0 à 3 mois", + ordre: 6, + }; + const stepsAlreadyCongratulatedForArticles = ["1"]; + const data = NotificationUtils.hasBeenAlreadyNotifiedForArticles( + CURRENT_STEP, + stepsAlreadyCongratulatedForArticles + ); + expect(data).toBeFalsy(); + }); + + it("hasBeenAlreadyNotifiedForArticles is true", () => { + const CURRENT_STEP = { + active: true, + debut: 0, + description: null, + fin: 90, + id: 6, + nom: "De 0 à 3 mois", + ordre: 6, + }; + const stepsAlreadyCongratulatedForArticles = ["6"]; + const data = NotificationUtils.hasBeenAlreadyNotifiedForArticles( + CURRENT_STEP, + stepsAlreadyCongratulatedForArticles + ); + expect(data).toBeTruthy(); + }); + }); + describe("saveStepForCongratNotifScheduled", () => { afterEach(() => { void AsyncStorage.clear(); }); it("saveStepForCongratNotifScheduled with no article and currentStep is null", async () => { - await saveStepForCongratNotifScheduled(0, null, null); + await NotificationUtils.saveStepForCongratNotifScheduled(0, null, null); await StorageUtils.getObjectValue( StorageKeysConstants.stepsAlreadyCongratulatedForArticles ).then((data) => { @@ -165,7 +234,11 @@ describe("Notification utils", () => { }; const expected = ["6"]; - await saveStepForCongratNotifScheduled(0, currentStep, null); + await NotificationUtils.saveStepForCongratNotifScheduled( + 0, + currentStep, + null + ); await StorageUtils.getObjectValue( StorageKeysConstants.stepsAlreadyCongratulatedForArticles ).then((data) => { @@ -184,9 +257,9 @@ describe("Notification utils", () => { ordre: 6, }; const stepsAlreadyCongratulatedForArticles = ["2", "5"]; - const expected = stepsAlreadyCongratulatedForArticles.length + 1; + const expected = ["2", "5", "6"]; - await saveStepForCongratNotifScheduled( + await NotificationUtils.saveStepForCongratNotifScheduled( 0, currentStep, stepsAlreadyCongratulatedForArticles diff --git a/front/src/utils/notifications/notification.util.ts b/front/src/utils/notifications/notification.util.ts index dd4cbe066..54f5856f0 100644 --- a/front/src/utils/notifications/notification.util.ts +++ b/front/src/utils/notifications/notification.util.ts @@ -390,9 +390,10 @@ export const saveStepForCongratNotifScheduled = async ( ): Promise => { if (nbArticlesToRead === 0 && currentStep) { const currentStepId = currentStep.id.toString(); - const newValue = stepsAlreadyCongratulatedForArticles - ? stepsAlreadyCongratulatedForArticles.push(currentStepId) - : [currentStepId]; + if (stepsAlreadyCongratulatedForArticles) { + stepsAlreadyCongratulatedForArticles.push(currentStepId); + } + const newValue = stepsAlreadyCongratulatedForArticles ?? [currentStepId]; await StorageUtils.storeObjectValue( StorageKeysConstants.stepsAlreadyCongratulatedForArticles, newValue @@ -400,6 +401,19 @@ export const saveStepForCongratNotifScheduled = async ( } }; +export const hasBeenAlreadyNotifiedForArticles = ( + currentStep: Step | null, + stepsAlreadyCongratulatedForArticles: string[] | null +): boolean => { + let hasBeenAlreadyNotified = false; + if (Array.isArray(stepsAlreadyCongratulatedForArticles)) { + hasBeenAlreadyNotified = stepsAlreadyCongratulatedForArticles.includes( + currentStep ? currentStep.id.toString() : "" + ); + } + return hasBeenAlreadyNotified; +}; + export const scheduleArticlesNotification = async ( notifTrigger?: NotificationTriggerInput ): Promise => { @@ -426,11 +440,13 @@ export const scheduleArticlesNotification = async ( if (content) { await cancelAllNotificationsByType(NotificationType.articles); - const hasBeenAlreadyNotified = - stepsAlreadyCongratulatedForArticles?.includes( - currentStep ? currentStep.id.toString() : "" - ); - if (!hasBeenAlreadyNotified) { + + if ( + !hasBeenAlreadyNotifiedForArticles( + currentStep, + stepsAlreadyCongratulatedForArticles + ) + ) { await sendNotificationReminder(content, trigger); await saveStepForCongratNotifScheduled( nbArticlesToRead,