Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions libs/category/state/src/reducers/product-reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ export const daffCategoryProductReducers: ActionReducerMap<DaffProductReducersSt
product: daffCategoryProductReducer,
productGrid: daffIdentityReducer,
products: daffIdentityReducer,
customAttributes: daffIdentityReducer,
customAttributesOperation: daffIdentityReducer,
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DaffModelFactory } from '@daffodil/core/testing';
import {
DaffProductImageFactory,
MockProduct,
DaffProductCustomAttributeValueFactory,
} from '@daffodil/product/testing';
import { DaffCompositeProductItemOption } from '@daffodil/product-composite';

Expand All @@ -22,10 +23,11 @@ export class MockCompositeProductItemOption extends MockProduct implements DaffC
@Injectable({
providedIn: 'root',
})
export class DaffCompositeProductItemOptionFactory extends DaffModelFactory<DaffCompositeProductItemOption>{
export class DaffCompositeProductItemOptionFactory extends DaffModelFactory<DaffCompositeProductItemOption, typeof MockCompositeProductItemOption>{
constructor(
imageFactory: DaffProductImageFactory,
customAttributeFactory: DaffProductCustomAttributeValueFactory,
) {
super(MockCompositeProductItemOption, imageFactory);
super(MockCompositeProductItemOption, imageFactory, customAttributeFactory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@ import { DaffCompositeProductItem } from '@daffodil/product-composite';
import { DaffCompositeProductItemFactory } from './composite-product-item.factory';

describe('@daffodil/product-composite/testing | DaffCompositeProductItemFactory', () => {

let compositeProductFactory;
let compositeProductFactory: DaffCompositeProductItemFactory;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [DaffCompositeProductItemFactory],
});

compositeProductFactory = TestBed.inject(DaffCompositeProductItemFactory);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class MockCompositeProductItem implements DaffCompositeProductItem {
@Injectable({
providedIn: 'root',
})
export class DaffCompositeProductItemFactory extends DaffModelFactory<DaffCompositeProductItem>{
export class DaffCompositeProductItemFactory extends DaffModelFactory<DaffCompositeProductItem, typeof MockCompositeProductItem>{
constructor(
optionFactory: DaffCompositeProductItemOptionFactory,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DaffModelFactory } from '@daffodil/core/testing';
import { DaffProductTypeEnum } from '@daffodil/product';
import {
DaffProductImageFactory,
DaffProductCustomAttributeValueFactory,
MockProduct,
} from '@daffodil/product/testing';
import { DaffCompositeProduct } from '@daffodil/product-composite';
Expand All @@ -20,8 +21,9 @@ export class MockCompositeProduct extends MockProduct implements DaffCompositePr
constructor(
protected itemFactory: DaffCompositeProductItemFactory,
imageFactory: DaffProductImageFactory,
customAttributeFactory: DaffProductCustomAttributeValueFactory,
) {
super(imageFactory);
super(imageFactory, customAttributeFactory);
}
}

Expand All @@ -31,11 +33,12 @@ export class MockCompositeProduct extends MockProduct implements DaffCompositePr
@Injectable({
providedIn: 'root',
})
export class DaffCompositeProductFactory extends DaffModelFactory<DaffCompositeProduct>{
export class DaffCompositeProductFactory extends DaffModelFactory<DaffCompositeProduct, typeof MockCompositeProduct>{
constructor(
itemFactory: DaffCompositeProductItemFactory,
imageFactory: DaffProductImageFactory,
customAttributeFactory: DaffProductCustomAttributeValueFactory,
) {
super(MockCompositeProduct, itemFactory, imageFactory);
super(MockCompositeProduct, itemFactory, imageFactory, customAttributeFactory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DaffModelFactory } from '@daffodil/core/testing';
import { DaffProductTypeEnum } from '@daffodil/product';
import {
DaffProductImageFactory,
DaffProductCustomAttributeValueFactory,
MockProduct,
} from '@daffodil/product/testing';
import { DaffConfigurableProduct } from '@daffodil/product-configurable';
Expand Down Expand Up @@ -32,23 +33,23 @@ export class MockConfigurableProduct extends MockProduct implements DaffConfigur
label: 'Blue',
swatch: {
value: '#0000FF',
thumbnail: null,
thumbnail: undefined,
},
},
{
value: '1',
label: 'Yellow',
swatch: {
value: '#FFFF00',
thumbnail: null,
thumbnail: undefined,
},
},
{
value: '2',
label: 'Red',
swatch: {
value: '#FF0000',
thumbnail: null,
thumbnail: undefined,
},
},
],
Expand All @@ -61,17 +62,17 @@ export class MockConfigurableProduct extends MockProduct implements DaffConfigur
{
value: '0',
label: 'Small',
swatch: null,
swatch: undefined,
},
{
value: '1',
label: 'Medium',
swatch: null,
swatch: undefined,
},
{
value: '2',
label: 'Large',
swatch: null,
swatch: undefined,
},
],
},
Expand All @@ -83,17 +84,17 @@ export class MockConfigurableProduct extends MockProduct implements DaffConfigur
{
value: '0',
label: 'Cotton',
swatch: null,
swatch: undefined,
},
{
value: '1',
label: 'Polyester',
swatch: null,
swatch: undefined,
},
{
value: '2',
label: 'Spandex',
swatch: null,
swatch: undefined,
},
],
},
Expand Down Expand Up @@ -248,10 +249,11 @@ export class MockConfigurableProduct extends MockProduct implements DaffConfigur
@Injectable({
providedIn: 'root',
})
export class DaffConfigurableProductFactory extends DaffModelFactory<DaffConfigurableProduct>{
export class DaffConfigurableProductFactory extends DaffModelFactory<DaffConfigurableProduct, typeof MockConfigurableProduct>{
constructor(
imageFactory: DaffProductImageFactory,
customAttributeFactory: DaffProductCustomAttributeValueFactory,
) {
super(MockConfigurableProduct, imageFactory);
super(MockConfigurableProduct, imageFactory, customAttributeFactory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { makeStateKey } from '@angular/core';

import { DaffProductCustomAttribute } from '@daffodil/product';

export const CUSTOM_ATTRIBUTE_TRANSFER_STATE_KEY = makeStateKey<DaffProductCustomAttribute[]>('DAFF_PRODUCT_CUSTOM_ATTRIBUTES_INMEMORY_DATA');
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import {
PLATFORM_ID,
TransferState,
} from '@angular/core';
import { TestBed } from '@angular/core/testing';

import {
DaffProductCustomAttribute,
DaffProductCustomAttributeKind,
} from '@daffodil/product';

import { CUSTOM_ATTRIBUTE_TRANSFER_STATE_KEY } from './custom-attribute-transfer-state-key';
import { DaffInMemoryBackendProductCustomAttributeService } from './custom-attribute.service';

describe('Driver | InMemory | Product | DaffInMemoryBackendProductCustomAttributeService', () => {
let customAttributeTestingService: DaffInMemoryBackendProductCustomAttributeService;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
DaffInMemoryBackendProductCustomAttributeService,
],
});

customAttributeTestingService = TestBed.inject(DaffInMemoryBackendProductCustomAttributeService);
});

it('should be created', () => {
expect(customAttributeTestingService).toBeTruthy();
});

describe('transferring state from the server to the browser', () => {
describe('on browser platform', () => {
let transferState: TransferState;
let mockCustomAttributes: DaffProductCustomAttribute[];

beforeEach(() => {
mockCustomAttributes = [
{ id: '1', kind: DaffProductCustomAttributeKind.SCALAR, label: 'Attribute 1' },
{ id: '2', kind: DaffProductCustomAttributeKind.SCALAR, label: 'Attribute 2' },
];

TestBed.resetTestingModule();
TestBed.configureTestingModule({
providers: [
DaffInMemoryBackendProductCustomAttributeService,
{ provide: PLATFORM_ID, useValue: 'browser' },
],
});

transferState = TestBed.inject(TransferState);
transferState.set(CUSTOM_ATTRIBUTE_TRANSFER_STATE_KEY, mockCustomAttributes);

customAttributeTestingService = TestBed.inject(DaffInMemoryBackendProductCustomAttributeService);
});

it('should load custom attributes from transfer state', () => {
expect(customAttributeTestingService.customAttributes).toEqual(mockCustomAttributes);
});

it('should not create new custom attributes', () => {
expect(customAttributeTestingService.customAttributes.length).toEqual(2);
});
});

describe('on browser platform without transfer state', () => {
beforeEach(() => {
TestBed.resetTestingModule();
TestBed.configureTestingModule({
providers: [
DaffInMemoryBackendProductCustomAttributeService,
{ provide: PLATFORM_ID, useValue: 'browser' },
],
});

customAttributeTestingService = TestBed.inject(DaffInMemoryBackendProductCustomAttributeService);
});

it('should create default custom attributes from factory', () => {
expect(customAttributeTestingService.customAttributes.length).toEqual(10);
});
});

describe('on server platform', () => {
let transferState: TransferState;

beforeEach(() => {
TestBed.resetTestingModule();
TestBed.configureTestingModule({
providers: [
DaffInMemoryBackendProductCustomAttributeService,
{ provide: PLATFORM_ID, useValue: 'server' },
],
});

transferState = TestBed.inject(TransferState);
customAttributeTestingService = TestBed.inject(DaffInMemoryBackendProductCustomAttributeService);
});

it('should set custom attributes in transfer state', () => {
const transferStateData = transferState.get(CUSTOM_ATTRIBUTE_TRANSFER_STATE_KEY, null);
expect(transferStateData).toBeTruthy();
expect(Array.isArray(transferStateData)).toBe(true);
});
});
});

describe('createDb', () => {
let result: { customAttributes: DaffProductCustomAttribute[] };

beforeEach(() => {
result = customAttributeTestingService.createDb();
});

it('should return an object with an array of custom attributes', () => {
expect(Array.isArray(result.customAttributes)).toEqual(true);
expect(result.customAttributes.length).toEqual(10);
});
});

describe('get', () => {
let reqInfoStub: any;
let result: any;

beforeEach(() => {
reqInfoStub = {
utils: {
createResponse$: (func: () => any) => func(),
},
};

result = customAttributeTestingService.get(reqInfoStub);
});

it('should return the full list of custom attributes', () => {
expect(result.body).toEqual(customAttributeTestingService.customAttributes);
expect(result.status).toEqual(200);
});
});
});
Loading
Loading