-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preventing crash when assigning product to shipping rate (#5188)
* Fix reading id of undefined products * Add changeset
- Loading branch information
Showing
5 changed files
with
80 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"saleor-dashboard": patch | ||
--- | ||
|
||
Assigning product to shipping method weight rate no more cause error |
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
5 changes: 5 additions & 0 deletions
5
src/shipping/components/ShippingMethodProductsAddDialog/types.ts
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,5 @@ | ||
import { SearchProductsQuery } from "@dashboard/graphql"; | ||
import { RelayToFlat } from "@dashboard/types"; | ||
|
||
export type Products = NonNullable<RelayToFlat<SearchProductsQuery["search"]>>; | ||
export type Product = Products[0]; |
52 changes: 52 additions & 0 deletions
52
src/shipping/components/ShippingMethodProductsAddDialog/utils.test.ts
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,52 @@ | ||
import { Products } from "./types"; | ||
import { isProductSelected } from "./utils"; | ||
|
||
describe("isProductSelected", () => { | ||
it("should return false if product id is not provided", () => { | ||
// Arrange | ||
const selectedProducts = [{ id: "1" }, { id: "2" }] as Products; | ||
const productId = undefined; | ||
|
||
// Act | ||
const result = isProductSelected(selectedProducts, productId); | ||
|
||
// Assert | ||
expect(result).toEqual(false); | ||
}); | ||
|
||
it("should return false if product id is not in selected products", () => { | ||
// Arrange | ||
const selectedProducts = [{ id: "1" }, { id: "2" }] as Products; | ||
const productId = "3"; | ||
|
||
// Act | ||
const result = isProductSelected(selectedProducts, productId); | ||
|
||
// Assert | ||
expect(result).toEqual(false); | ||
}); | ||
|
||
it("should return false if no selected product", () => { | ||
// Arrange | ||
const selectedProducts = [] as Products; | ||
const productId = "1"; | ||
|
||
// Act | ||
const result = isProductSelected(selectedProducts, productId); | ||
|
||
// Assert | ||
expect(result).toEqual(false); | ||
}); | ||
|
||
it("should return true if product id is in selected products", () => { | ||
// Arrange | ||
const selectedProducts = [{ id: "1" }, { id: "2" }] as Products; | ||
const productId = "2"; | ||
|
||
// Act | ||
const result = isProductSelected(selectedProducts, productId); | ||
|
||
// Assert | ||
expect(result).toEqual(true); | ||
}); | ||
}); |
7 changes: 7 additions & 0 deletions
7
src/shipping/components/ShippingMethodProductsAddDialog/utils.ts
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,7 @@ | ||
import { Products } from "./types"; | ||
|
||
export const isProductSelected = (selectedProducts: Products, productId?: string) => { | ||
if (!productId) return false; | ||
|
||
return selectedProducts.some(selectedProduct => selectedProduct.id === productId); | ||
}; |