-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: [PE-796] CGN hide percentage badge if value not in range
1-100
(…
…#6454) ## Short description This pull is removing the discount badge from cgn opportunities when this value is not in range `1-100` ## List of changes proposed in this pull request - Replaced the inline discount validation and normalization logic with the `isValidDiscount` and `normalizedDiscountPercentage` utility functions - Added `normalizedDiscountPercentage` and `isValidDiscount` utility functions to handle discount validation and normalization - Added unit tests for utils functions ## How to test - From dev-server mock some discount value equals 0 or higher than 100 - Ensure that badges for this values are not rendering --------- Co-authored-by: Cristiano Tofani <[email protected]>
- Loading branch information
1 parent
f9625e9
commit b151329
Showing
7 changed files
with
179 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
ts/features/bonus/cgn/components/merchants/___tests___/CgnModuleDiscount.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { NonEmptyString } from "@pagopa/ts-commons/lib/strings"; | ||
import { fireEvent, render } from "@testing-library/react-native"; | ||
import React from "react"; | ||
import { Discount } from "../../../../../../../definitions/cgn/merchants/Discount"; | ||
import { ProductCategoryEnum } from "../../../../../../../definitions/cgn/merchants/ProductCategory"; | ||
import I18n from "../../../../../../i18n"; | ||
import { CgnModuleDiscount } from "../CgnModuleDiscount"; | ||
|
||
describe("CgnModuleDiscount", () => { | ||
const discount: Discount = { | ||
name: "Small Rubber Chips" as NonEmptyString, | ||
id: "28201" as NonEmptyString, | ||
description: undefined, | ||
discount: 25, | ||
discountUrl: "https://localhost", | ||
endDate: new Date(), | ||
isNew: true, | ||
productCategories: [ProductCategoryEnum.cultureAndEntertainment], | ||
startDate: new Date() | ||
}; | ||
|
||
const onPressMock = jest.fn(); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should render correctly", () => { | ||
const { getByText } = render( | ||
<CgnModuleDiscount onPress={onPressMock} discount={discount} /> | ||
); | ||
|
||
expect(getByText(I18n.t("bonus.cgn.merchantsList.news"))).toBeTruthy(); | ||
expect(getByText("-25%")).toBeTruthy(); | ||
expect(getByText("Small Rubber Chips")).toBeTruthy(); | ||
}); | ||
|
||
it("should call onPress when pressed", () => { | ||
const { getByRole } = render( | ||
<CgnModuleDiscount onPress={onPressMock} discount={discount} /> | ||
); | ||
fireEvent.press(getByRole("button")); | ||
expect(onPressMock).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
ts/features/bonus/cgn/components/merchants/discount/___tests___/CgnDiscountHeader.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { NonEmptyString } from "@pagopa/ts-commons/lib/strings"; | ||
import { render } from "@testing-library/react-native"; | ||
import React from "react"; | ||
import { Discount } from "../../../../../../../../definitions/cgn/merchants/Discount"; | ||
import { ProductCategoryEnum } from "../../../../../../../../definitions/cgn/merchants/ProductCategory"; | ||
import I18n from "../../../../../../../i18n"; | ||
import { CgnDiscountHeader } from "../CgnDiscountHeader"; | ||
|
||
const mockDiscount: Discount = { | ||
name: "Small Rubber Chips" as NonEmptyString, | ||
id: "28201" as NonEmptyString, | ||
description: undefined, | ||
discount: 25, | ||
discountUrl: "https://localhost", | ||
endDate: new Date(), | ||
isNew: true, | ||
productCategories: [ProductCategoryEnum.cultureAndEntertainment], | ||
startDate: new Date() | ||
}; | ||
|
||
jest.mock("@react-navigation/elements", () => ({ | ||
useHeaderHeight: jest.fn().mockImplementation(() => 200) | ||
})); | ||
|
||
describe("CgnDiscountHeader", () => { | ||
it("should render correctly with new discount", () => { | ||
const { getByText } = render( | ||
<CgnDiscountHeader onLayout={jest.fn()} discountDetails={mockDiscount} /> | ||
); | ||
|
||
expect(getByText(I18n.t("bonus.cgn.merchantsList.news"))).toBeTruthy(); | ||
expect(getByText("-25%")).toBeTruthy(); | ||
}); | ||
|
||
it("should render correctly without new discount", () => { | ||
const discountDetails = { ...mockDiscount, isNew: false }; | ||
const { queryByText } = render( | ||
<CgnDiscountHeader | ||
onLayout={jest.fn()} | ||
discountDetails={discountDetails} | ||
/> | ||
); | ||
|
||
expect(queryByText(I18n.t("bonus.cgn.merchantsList.news"))).toBeNull(); | ||
expect(queryByText("-25%")).toBeTruthy(); | ||
}); | ||
|
||
it("should render correctly without discount", () => { | ||
const discountDetails = { ...mockDiscount, discount: 0 }; | ||
const { queryByText } = render( | ||
<CgnDiscountHeader | ||
onLayout={jest.fn()} | ||
discountDetails={discountDetails} | ||
/> | ||
); | ||
|
||
expect(queryByText(I18n.t("bonus.cgn.merchantsList.news"))).toBeTruthy(); | ||
expect(queryByText("-0%")).toBeNull(); | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
ts/features/bonus/cgn/components/merchants/utils/___tests___/index.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { normalizedDiscountPercentage, isValidDiscount } from "../index"; | ||
|
||
describe("normalizedDiscountPercentage", () => { | ||
it("should return the discount as a string if within range", () => { | ||
expect(normalizedDiscountPercentage(50)).toBe("50"); | ||
}); | ||
|
||
it("should return '-' if the discount is out of range", () => { | ||
expect(normalizedDiscountPercentage(150)).toBe("-"); | ||
expect(isValidDiscount(0)).toBe(false); | ||
}); | ||
|
||
it("should return '-' if the discount is undefined", () => { | ||
expect(normalizedDiscountPercentage()).toBe("-"); | ||
}); | ||
}); | ||
|
||
describe("isValidDiscount", () => { | ||
it("should return true if the discount is within range", () => { | ||
expect(isValidDiscount(50)).toBe(true); | ||
}); | ||
|
||
it("should return false if the discount is out of range", () => { | ||
expect(isValidDiscount(150)).toBe(false); | ||
expect(isValidDiscount(0)).toBe(false); | ||
}); | ||
|
||
it("should return false if the discount is undefined", () => { | ||
expect(isValidDiscount()).toBe(false); | ||
}); | ||
}); |
13 changes: 13 additions & 0 deletions
13
ts/features/bonus/cgn/components/merchants/utils/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { WithinRangeInteger } from "@pagopa/ts-commons/lib/numbers"; | ||
import * as E from "fp-ts/lib/Either"; | ||
import { pipe } from "fp-ts/lib/function"; | ||
|
||
export const normalizedDiscountPercentage = (discount?: number) => | ||
pipe( | ||
WithinRangeInteger(1, 100).decode(discount), | ||
E.map(v => v.toString()), | ||
E.getOrElse(() => "-") | ||
); | ||
|
||
export const isValidDiscount = (discount?: number) => | ||
pipe(WithinRangeInteger(1, 100).decode(discount), E.isRight); |