Skip to content

Commit

Permalink
MARP-1449 Portal Guide does not match with Portal version (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
tvtphuc-axonivy authored Nov 19, 2024
1 parent bfc6224 commit 9061409
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ProductDetailInformationTabComponent } from './product-detail-information-tab.component';
import { of } from 'rxjs';
import { SimpleChanges } from '@angular/core';
import { SimpleChange, SimpleChanges } from '@angular/core';
import { ProductDetailService } from '../product-detail.service';
import { LanguageService } from '../../../../core/services/language/language.service';
import { ProductDetail } from '../../../../shared/models/product-detail.model';
Expand All @@ -20,7 +20,7 @@ describe('ProductDetailInformationTabComponent', () => {
let productDetailService: jasmine.SpyObj<ProductDetailService>;

beforeEach(async () => {
const productDetailServiceSpy = jasmine.createSpyObj('ProductDetailService', ['getExteralDocumentForProductByVersion']);
const productDetailServiceSpy = jasmine.createSpyObj('ProductDetailService', ['getExternalDocumentForProductByVersion']);

await TestBed.configureTestingModule({
imports: [ProductDetailInformationTabComponent,
Expand All @@ -43,7 +43,7 @@ describe('ProductDetailInformationTabComponent', () => {
});

it('should set externalDocumentLink and displayExternalDocName on valid version change', () => {
productDetailService.getExteralDocumentForProductByVersion.and.returnValue(of({ ...MOCK_EXTERNAL_DOCUMENT }));
productDetailService.getExternalDocumentForProductByVersion.and.returnValue(of({ ...MOCK_EXTERNAL_DOCUMENT }));

component.productDetail = { id: TEST_ID, newestReleaseVersion: TEST_VERSION } as ProductDetail;
component.selectedVersion = TEST_VERSION;
Expand All @@ -64,7 +64,7 @@ describe('ProductDetailInformationTabComponent', () => {

component.ngOnChanges(changes);

expect(productDetailService.getExteralDocumentForProductByVersion).toHaveBeenCalledWith(TEST_ID, TEST_VERSION);
expect(productDetailService.getExternalDocumentForProductByVersion).toHaveBeenCalledWith(TEST_ID, TEST_VERSION);
expect(component.externalDocumentLink).toBe(TEST_DOC_URL);
expect(component.displayExternalDocName).toBe(TEST_ARTIFACT_NAME);
});
Expand All @@ -89,7 +89,7 @@ describe('ProductDetailInformationTabComponent', () => {

component.ngOnChanges(changes);

expect(productDetailService.getExteralDocumentForProductByVersion).not.toHaveBeenCalled();
expect(productDetailService.getExternalDocumentForProductByVersion).not.toHaveBeenCalled();
expect(component.externalDocumentLink).toBe('');
expect(component.displayExternalDocName).toBe('');
});
Expand All @@ -99,4 +99,52 @@ describe('ProductDetailInformationTabComponent', () => {
const extractedValue = component.extractVersionValue(versionDisplayName);
expect(extractedValue).toBe(TEST_VERSION);
});

it('should check isProductChanged correct', () => {
component.productDetail = { id: TEST_ID, newestReleaseVersion: '11.3.0' } as ProductDetail;
const productChanged: SimpleChange = {
currentValue: component.productDetail,
previousValue: undefined,
firstChange: true,
isFirstChange: () => true
};

const result = component.isProductChanged(productChanged);
expect(result).toBe(false);
});

it('should check isProductChanged correct if the same value', () => {
component.productDetail = { id: TEST_ID, newestReleaseVersion: '11.3.0' } as ProductDetail;
const productChanged: SimpleChange = {
currentValue: component.productDetail,
previousValue: component.productDetail.newestReleaseVersion = '12.0.0-m266',
firstChange: true,
isFirstChange: () => true
};

const result = component.isProductChanged(productChanged);
expect(result).toBe(true);
});

it('should get correct externalDocumentLink by newestReleaseVersion', () => {
productDetailService.getExternalDocumentForProductByVersion.and.returnValue(of({ ...MOCK_EXTERNAL_DOCUMENT }));
component.productDetail = { id: TEST_ID, newestReleaseVersion: TEST_VERSION } as ProductDetail;
component.selectedVersion = TEST_VERSION;
const changes: SimpleChanges = {
selectedVersion: {
currentValue: TEST_VERSION,
previousValue: '8.0.0',
firstChange: false,
isFirstChange: () => false
},
productDetail: {
currentValue: component.productDetail,
previousValue: component.productDetail.newestReleaseVersion = '12.0.0-m266',
firstChange: true,
isFirstChange: () => true
}
};
component.ngOnChanges(changes);
expect(productDetailService.getExternalDocumentForProductByVersion).toHaveBeenCalledWith(TEST_ID, '12.0.0-m266');
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommonModule } from '@angular/common';
import { Component, inject, Input, OnChanges, SimpleChanges } from '@angular/core';
import { Component, inject, Input, OnChanges, SimpleChange, SimpleChanges } from '@angular/core';
import { TranslateModule } from '@ngx-translate/core';
import { ProductDetail } from '../../../../shared/models/product-detail.model';
import { LanguageService } from '../../../../core/services/language/language.service';
Expand Down Expand Up @@ -37,7 +37,7 @@ export class ProductDetailInformationTabComponent implements OnChanges {
return;
}
const changedProduct = changes[PRODUCT_DETAIL];
if (changedProduct && changedProduct.currentValue !== changedProduct.previousValue) {
if (this.isProductChanged(changedProduct)) {
version = this.productDetail.newestReleaseVersion;
} else {
version = this.selectedVersion;
Expand All @@ -47,7 +47,7 @@ export class ProductDetailInformationTabComponent implements OnChanges {
return;
}

this.productDetailService.getExteralDocumentForProductByVersion(this.productDetail.id, this.extractVersionValue(version))
this.productDetailService.getExternalDocumentForProductByVersion(this.productDetail.id, this.extractVersionValue(version))
.subscribe({
next: response => {
this.externalDocumentLink = response.relativeLink;
Expand All @@ -66,4 +66,13 @@ export class ProductDetailInformationTabComponent implements OnChanges {
extractVersionValue(versionDisplayName: string) {
return versionDisplayName.replace(VERSION.displayPrefix, '');
}

// To ensure the function always returns a boolean, you can explicitly coerce the result into a boolean using the !! operator or default it to false
// Adding !! in case of changedProduct is undefined, it will return false instead of returning undefined
isProductChanged(changedProduct: SimpleChange) {
return !!(changedProduct?.previousValue &&
Object.keys(changedProduct.previousValue).length > 0 &&
changedProduct.currentValue !== changedProduct.previousValue);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export class ProductDetailService {
httpClient = inject(HttpClient);
loadingService = inject(LoadingService);
ratingBtnLabel: WritableSignal<string> = signal('');
getExteralDocumentForProductByVersion(productId: string, version: string): Observable<ExternalDocument> {

getExternalDocumentForProductByVersion(productId: string, version: string): Observable<ExternalDocument> {
return this.httpClient.get<ExternalDocument>(
`${API_URI.EXTERNAL_DOCUMENT}/${productId}/${version}`, { context: new HttpContext().set(ForwardingError, true)}
);
Expand Down

0 comments on commit 9061409

Please sign in to comment.