This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: product variants' names, show cms hydrated content
- Loading branch information
Maciej Kucmus
committed
Jan 18, 2020
1 parent
960ed22
commit f7bc717
Showing
9 changed files
with
163 additions
and
9 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { getProductName } from "@shopware-pwa/helpers"; | ||
|
||
describe("Shopware helpers - getProductName", () => { | ||
it("should return empty string if argument wasn't provided", () => { | ||
const label = getProductName(); | ||
expect(label).toBeNull(); | ||
}); | ||
|
||
it("should return default value if product was null", () => { | ||
const argument: any = { product: null }; | ||
const label = getProductName(argument); | ||
expect(label).toBeNull(); | ||
}); | ||
|
||
it("should return translated name if the base one does not exist", () => { | ||
const argument: any = { | ||
product: { | ||
name: null, | ||
translated: { | ||
name: "Existing" | ||
} | ||
} | ||
}; | ||
const productName = getProductName(argument); | ||
expect(productName).toBe("Existing"); | ||
}); | ||
it("should return name enriched with variant label if options are included", () => { | ||
const argument: any = { | ||
product: { | ||
name: "T-Shirt", | ||
options: [ | ||
{ | ||
name: "XL" | ||
}, | ||
{ | ||
name: "yellow" | ||
} | ||
] | ||
} | ||
}; | ||
const productName = getProductName(argument); | ||
expect(productName).toBe("T-Shirt - XL yellow"); | ||
}); | ||
}); |
55 changes: 55 additions & 0 deletions
55
packages/helpers/__tests__/product/getVariantOptionsLabel.spec.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,55 @@ | ||
import { getVariantOptionsLabel } from "@shopware-pwa/helpers"; | ||
|
||
describe("Shopware helpers - getVariantOptionsLabel", () => { | ||
it("should return empty string if argument wasn't provided", () => { | ||
const label = getVariantOptionsLabel(); | ||
expect(label).toBeNull(); | ||
}); | ||
|
||
it("should return default value if product was null", () => { | ||
const argument: any = { product: null }; | ||
const label = getVariantOptionsLabel(argument); | ||
expect(label).toBeNull(); | ||
}); | ||
|
||
it("should return empty string if empty options array is provided", () => { | ||
const argument: any = { | ||
product: { | ||
options: [] | ||
} | ||
}; | ||
const label = getVariantOptionsLabel(argument); | ||
expect(label).toBeNull(); | ||
}); | ||
|
||
it("should return label made of one option name", () => { | ||
const argument: any = { | ||
product: { | ||
options: [ | ||
{ | ||
name: "XL" | ||
} | ||
] | ||
} | ||
}; | ||
const label = getVariantOptionsLabel(argument); | ||
expect(label).toBe("XL"); | ||
}); | ||
|
||
it("should return label made of two option names", () => { | ||
const argument: any = { | ||
product: { | ||
options: [ | ||
{ | ||
name: "L" | ||
}, | ||
{ | ||
name: "black" | ||
} | ||
] | ||
} | ||
}; | ||
const label = getVariantOptionsLabel(argument); | ||
expect(label).toBe("L black"); | ||
}); | ||
}); |
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,14 @@ | ||
import { Product } from "@shopware-pwa/shopware-6-client/src/interfaces/models/content/product/Product"; | ||
import { getVariantOptionsLabel } from "./getVariantOptionsLabel"; | ||
|
||
export function getProductName({ product }: { product?: Product } = {}): | ||
| string | ||
| null { | ||
if (!product) { | ||
return null; | ||
} | ||
const variantLabel = getVariantOptionsLabel({ product: product }); | ||
return `${product.name || product.translated.name}${ | ||
variantLabel ? " - " + variantLabel : "" | ||
}`; | ||
} |
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,19 @@ | ||
import { Product } from "@shopware-pwa/shopware-6-client/src/interfaces/models/content/product/Product"; | ||
|
||
export function getVariantOptionsLabel({ | ||
product | ||
}: { product?: Product } = {}): string | null { | ||
if ( | ||
!product || | ||
!product.options || | ||
(product.options && !product.options.length) | ||
) { | ||
return null; | ||
} | ||
let variantLabel = ""; | ||
for (let { name } of product.options) { | ||
variantLabel += `${name} `; | ||
} | ||
|
||
return variantLabel.trim(); | ||
} |
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