Skip to content

Commit b482a11

Browse files
committed
new tests
1 parent 8da207d commit b482a11

File tree

1 file changed

+122
-1
lines changed

1 file changed

+122
-1
lines changed

src/app/pages/product-details/product-details.component.spec.ts

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,31 @@ import { TranslateModule } from '@ngx-translate/core';
99
import { MarkdownModule } from 'ngx-markdown';
1010
import { oidcSecurityServiceMock } from 'src/testing/mocks/oidc-security.service.mock';
1111
import { OidcSecurityService } from 'angular-auth-oidc-client';
12+
import { ApiServiceService } from 'src/app/services/product-service.service';
1213

1314
describe('ProductDetailsComponent', () => {
1415
let component: ProductDetailsComponent;
1516
let fixture: ComponentFixture<ProductDetailsComponent>;
17+
const mockApi = {
18+
getProductSpecification: jest.fn(),
19+
getProductPrice: jest.fn(),
20+
};
1621

1722
beforeEach(async () => {
23+
mockApi.getProductSpecification.mockReset();
24+
mockApi.getProductPrice.mockReset();
25+
1826
await TestBed.configureTestingModule({
1927
providers: [
2028
{ provide: ActivatedRoute, useValue: { snapshot: { paramMap: convertToParamMap({ id: '123' }) } } },
2129
{ provide: OidcSecurityService, useValue: oidcSecurityServiceMock },
30+
{ provide: ApiServiceService, useValue: mockApi }
2231
],
2332
imports: [HttpClientTestingModule, TranslateModule.forRoot(), MarkdownModule.forRoot()],
2433
schemas: [NO_ERRORS_SCHEMA]
2534
})
2635
.compileComponents();
27-
36+
2837
fixture = TestBed.createComponent(ProductDetailsComponent);
2938
component = fixture.componentInstance;
3039
fixture.detectChanges();
@@ -33,4 +42,116 @@ describe('ProductDetailsComponent', () => {
3342
it('should create', () => {
3443
expect(component).toBeTruthy();
3544
});
45+
46+
it('should default category to none and return placeholder image when productOff is empty', () => {
47+
component.productOff = undefined as any;
48+
component.ngOnInit();
49+
expect(component.category).toBe('none');
50+
expect(component.getProductImage()).toContain('placehold.co');
51+
});
52+
53+
it('should slice categories when more than 5 and set checkMoreCats true', () => {
54+
component.productOff = {
55+
category: [{ name: 'c1' }, { name: 'c2' }, { name: 'c3' }, { name: 'c4' }, { name: 'c5' }, { name: 'c6' }]
56+
} as any;
57+
component.ngOnInit();
58+
expect(component.category).toBe('c1');
59+
expect(component.categories?.length).toBe(4);
60+
expect(component.categoriesMore?.length).toBe(2);
61+
expect(component.checkMoreCats).toBe(true);
62+
});
63+
64+
it('should select "Profile Picture" attachment when present', () => {
65+
component.productOff = {
66+
attachment: [
67+
{ name: 'Profile Picture', url: 'profile-url' },
68+
{ attachmentType: 'Picture', url: 'pic-url' }
69+
]
70+
} as any;
71+
component.ngOnInit();
72+
expect(component.images.length).toBe(1);
73+
expect(component.getProductImage()).toBe('profile-url');
74+
});
75+
76+
it('should fallback to Picture attachments when no profile picture', () => {
77+
component.productOff = {
78+
attachment: [
79+
{ attachmentType: 'Picture', url: 'pic-url' }
80+
]
81+
} as any;
82+
component.ngOnInit();
83+
expect(component.images.length).toBe(1);
84+
expect(component.getProductImage()).toBe('pic-url');
85+
});
86+
87+
it('loadMoreCategories should toggle flags appropriately', () => {
88+
component.productOff = {
89+
category: [{ name: 'c1' }, { name: 'c2' }, { name: 'c3' }, { name: 'c4' }, { name: 'c5' }, { name: 'c6' }]
90+
} as any;
91+
component.ngOnInit();
92+
expect(component.checkMoreCats).toBe(true);
93+
component.loadMoreCategories();
94+
expect(component.loadMoreCats).toBe(true);
95+
expect(component.checkMoreCats).toBe(false);
96+
expect(component.closeCats).toBe(true);
97+
});
98+
99+
it('closeCategories should reset flags and slice categories, toggling loadMoreCats', () => {
100+
component.productOff = {
101+
category: [{ name: 'c1' }, { name: 'c2' }, { name: 'c3' }, { name: 'c4' }, { name: 'c5' }, { name: 'c6' }]
102+
} as any;
103+
component.ngOnInit();
104+
component.loadMoreCats = true;
105+
component.closeCategories();
106+
expect(component.closeCats).toBe(false);
107+
expect(component.checkMoreCats).toBe(true);
108+
expect(component.categories?.length).toBe(4);
109+
expect(component.loadMoreCats).toBe(false);
110+
});
111+
112+
it('hideModal should emit close and reset flags', () => {
113+
let emitted = false;
114+
component.close.subscribe(() => emitted = true);
115+
component.loadMoreCats = true;
116+
component.checkMoreCats = false;
117+
component.hideModal();
118+
expect(emitted).toBe(true);
119+
expect(component.loadMoreCats).toBe(false);
120+
expect(component.checkMoreCats).toBe(true);
121+
});
122+
123+
it('should set checkCustom true when api price returns custom', async () => {
124+
component.productOff = {
125+
productSpecification: { id: 'spec1' },
126+
productOfferingPrice: [{ id: 'price1' }]
127+
} as any;
128+
129+
mockApi.getProductSpecification.mockResolvedValue({});
130+
mockApi.getProductPrice.mockResolvedValue({ priceType: 'custom' });
131+
132+
component.ngOnInit();
133+
await fixture.whenStable();
134+
135+
await Promise.resolve();
136+
137+
expect(mockApi.getProductSpecification).toHaveBeenCalledWith('spec1');
138+
expect(mockApi.getProductPrice).toHaveBeenCalledWith('price1');
139+
expect(component.checkCustom).toBe(true);
140+
});
141+
142+
it('should filter out compliance and certification characteristics when prodSpec provided before ngOnInit', () => {
143+
component.prodSpec = {
144+
productSpecCharacteristic: [
145+
{ name: 'Compliance:VC' },
146+
{ name: 'Compliance:SelfAtt' },
147+
{ name: 'SomeChar' },
148+
{ name: 'CertA' }
149+
]
150+
} as any;
151+
component.ngOnInit();
152+
expect(component.prodChars.find((c:any) => c.name === 'SomeChar')).toBeTruthy();
153+
expect(component.prodChars.find((c:any) => c.name === 'Compliance:VC')).toBeUndefined();
154+
expect(component.prodChars.find((c:any) => c.name === 'Compliance:SelfAtt')).toBeUndefined();
155+
});
156+
36157
});

0 commit comments

Comments
 (0)