Skip to content

Commit

Permalink
Fixed CR issues, added stories and unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tolerants committed Sep 6, 2024
1 parent c5c5ed8 commit 8a65c3b
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 38 deletions.
71 changes: 70 additions & 1 deletion libs/domain/product/src/mocks/src/mock-product.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ export class MockProductService implements Partial<ProductService> {
availability: true,
},
categoryIds: ['category id 1'],
variants: {
'variant-1': { brand: 'Brand1', color: 'color1' },
'variant-2': { brand: 'Brand1', color: 'green' },
'variant-3': { brand: 'Brand1', color: 'blue' },
},
variantDefinition: {
brand: ['Brand1'],
color: ['color1', 'green', 'blue'],
},
},
{
sku: '2',
Expand Down Expand Up @@ -138,6 +147,14 @@ export class MockProductService implements Partial<ProductService> {
availability: false,
},
categoryIds: ['category id 2'],
variantDefinition: {
brand: ['Brand2'],
color: ['red', 'green', 'blue'],
},
variants: {
'variant-1': { brand: 'Brand2', color: 'color2' },
'variant-2': { brand: 'Brand3' },
}
},
{
sku: '3',
Expand Down Expand Up @@ -291,7 +308,6 @@ export class MockProductService implements Partial<ProductService> {
'Sample attribute lengthy name, Sample attribute lengthy name, Sample attribute lengthy name.',
},
},

{
sku: 'single-image',
name: 'Sample product with one image',
Expand Down Expand Up @@ -326,6 +342,59 @@ export class MockProductService implements Partial<ProductService> {
discontinued: true,
discontinuedNote: 'This product is discontinued...',
},
{
sku: 'variant-selector',
name: 'Product with Variants',
mediaSet,
description: 'This product has multiple variants to choose from.',
price: {
defaultPrice: {
currency: 'EUR',
value: 1999,
isNet: true,
},
originalPrice: {
currency: 'EUR',
value: 2499,
isNet: true,
},
},
averageRating: 4.5,
reviewCount: 10,
attributes: {
brand: 'Brand9',
color: 'red',
size: 'M',
},
attributeNames: {
brand: 'Brand',
color: 'Color',
size: 'Size',
},
labels: [newLabel, saleLabel],
availability: {
quantity: 5,
isNeverOutOfStock: false,
availability: true,
},
categoryIds: ['category id 9'],
variants: {
'variant-1': { brand: 'Brand9', color: 'red', size: 'S' },
'variant-2': { brand: 'Brand9', color: 'red', size: 'M' },
'variant-3': { brand: 'Brand9', color: 'red', size: 'L' },
'variant-4': { brand: 'Brand9', color: 'green', size: 'S' },
'variant-5': { brand: 'Brand9', color: 'green', size: 'M' },
'variant-6': { brand: 'Brand9', color: 'green', size: 'L' },
'variant-7': { brand: 'Brand9', color: 'blue', size: 'S' },
'variant-8': { brand: 'Brand9', color: 'blue', size: 'M' },
'variant-9': { brand: 'Brand9', color: 'blue', size: 'L' },
},
variantDefinition: {
brand: ['Brand9'],
color: ['red', 'green', 'blue'],
size: ['S', 'M', 'L'],
},
}
];

get(qualifier: ProductQualifier): Observable<Product> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ export function productVariantNormalizer(
const variantMap = abstract.attributeMap?.attributeVariantMap;
const variantDefinition = abstract.attributeMap?.superAttributes;

if (!variantDefinition) {
return {};
}

if (variantMap) {
const variantMapEntries = Object.entries(variantMap);

Expand Down
19 changes: 19 additions & 0 deletions libs/domain/product/variant-selector/stories/demo.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { MockProductService } from '@oryx-frontend/product/mocks';
import { Meta, Story } from '@storybook/web-components';
import { TemplateResult, html } from 'lit';
import { storybookPrefix } from '../../.constants';

export default {
title: `${storybookPrefix}/Variant selector`,
parameters: {
chromatic: {
disableSnapshot: true,
},
},
} as Meta;

const Template: Story = (): TemplateResult => {
return html`<oryx-product-variant-selector sku="variant-selector"></oryx-product-variant-selector>`;
};

export const Demo = Template.bind({});
26 changes: 26 additions & 0 deletions libs/domain/product/variant-selector/stories/static.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { MockProductService } from '@oryx-frontend/product/mocks';
import { Meta, Story } from '@storybook/web-components';
import { TemplateResult, html } from 'lit';
import { storybookPrefix } from '../../.constants';

export default {
title: `${storybookPrefix}/Variant selector/Static`,
} as unknown as Meta;

const Template: Story<unknown> = (): TemplateResult => {
return html`
<section>
<h3>3 variants:</h3>
<oryx-product-variant-selector sku="variant-selector"></oryx-product-variant-selector>
</section>
<section>
<h3>2 variants:</h3>
<oryx-product-variant-selector sku="1"></oryx-product-variant-selector>
</section>
<section>
<h3>Color variants disabled:</h3>
<oryx-product-variant-selector sku="2"></oryx-product-variant-selector>
</section>
`;
}
export const Variations = Template.bind({});
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { fixture } from '@open-wc/testing-helpers';
import { createInjector, destroyInjector } from '@oryx-frontend/di';
import { mockProductProviders, MockProductService } from '@oryx-frontend/product/mocks';
import { useComponent } from '@oryx-frontend/utilities';
import { html } from 'lit';
import { ProductVariantSelectorComponent } from './variant-selector.component';
import { productVariantSelectorComponent } from './variant-selector.def';
import { ProductService } from '@oryx-frontend/product';
import { ContextService, DefaultContextService } from '@oryx-frontend/core';
import { beforeEach } from 'vitest';

describe('ProductVariantSelectorComponent', () => {
let element: ProductVariantSelectorComponent;
let productService: MockProductService;

beforeAll(async () => {
await useComponent(productVariantSelectorComponent);
});

beforeEach(async () => {
const injector = createInjector({
providers: [
{
provide: ProductService,
useClass: MockProductService
},
{
provide: ContextService,
useClass: DefaultContextService,
},
],
});

productService = injector.inject<MockProductService>(ProductService);


});

afterEach(() => {
destroyInjector();
});

describe('when the component is created', () => {
beforeEach(async () => {
element = await fixture(
html`
<oryx-product-variant-selector sku="variant-selector"></oryx-product-variant-selector>`
);
});

it('should be defined', () => {
expect(element).toBeInstanceOf(ProductVariantSelectorComponent);
});

it('passes the a11y audit', async () => {
expect(element).shadowDom.to.be.accessible();
});

it('should render a toggle variants', () => {
expect(element).toContainElement('oryx-toggle-icon');
});
});
});
43 changes: 6 additions & 37 deletions libs/domain/product/variant-selector/variant-selector.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ export class ProductVariantSelectorComponent extends ProductMixin(
) {
static styles = variantListStyle;

protected productListService = resolve(ProductListService);
protected productListPageService = resolve(ProductListPageService);

protected routerService = resolve(RouterService);
protected linkService = resolve(LinkService);

Expand All @@ -48,6 +45,9 @@ export class ProductVariantSelectorComponent extends ProductMixin(
});

protected render(): TemplateResult | void {
console.log('this.$product():');


const variants = this.$product()?.variants;

if (!variants || Object.keys(variants).length < 2) return;
Expand All @@ -57,6 +57,7 @@ export class ProductVariantSelectorComponent extends ProductMixin(

protected renderAttributeSelectors() {
const { variantDefinition, attributeNames } = this.$product() ?? {};

if (!variantDefinition) return;
const keys = Object.keys(variantDefinition);

Expand All @@ -71,7 +72,7 @@ export class ProductVariantSelectorComponent extends ProductMixin(
<oryx-toggle-icon>
<input
type="radio"
placeholder="make a11y happy"
.placeholder=${key}
.name=${key}
.value=${value}
?checked=${this.isChecked(key, value)}
Expand All @@ -80,7 +81,7 @@ export class ProductVariantSelectorComponent extends ProductMixin(
<span>${value}</span>
</oryx-toggle-icon>
`
)}</form>`
)}`
)}
</form>`;
}
Expand Down Expand Up @@ -123,38 +124,6 @@ export class ProductVariantSelectorComponent extends ProductMixin(
return isVariantDisabled;
}

// protected isDisabled(
// attributeKey: string,
// attributeValue: string,
// index: number
// ): boolean {
// const product = this.$product();

// if (!product?.variants) {
// return false;
// }

// // Extract active variant attribute keys up to the given index
// const activeKeys = Object.keys(product.variantDefinition || {}).slice(
// 0,
// index
// );

// // Check if any variant matches the selected attribute values
// const isVariantDisabled = !Object.values(product.variants).some(
// (variant) => {
// // Check if all selected attribute values match the variant
// return (
// activeKeys.every(
// (key) => variant[key] === this.$product()?.attributes?.[key]
// ) && variant[attributeKey] === attributeValue
// );
// }
// );

// return isVariantDisabled;
// }

protected handleVariantChange(e: Event) {
const form = e.currentTarget as HTMLFormElement;
const formData = new FormData(form);
Expand Down

0 comments on commit 8a65c3b

Please sign in to comment.