-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add CTA Scripts feature (#17991)
CXSPA-4532
- Loading branch information
1 parent
955ad0f
commit 44c1008
Showing
35 changed files
with
1,068 additions
and
82 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
8 changes: 8 additions & 0 deletions
8
integration-libs/opf/base/components/opf-cta/opf-cta-element/index.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,8 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2023 SAP Spartacus team <[email protected]> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export * from './opf-cta-element.component'; | ||
export * from './opf-cta-element.module'; |
1 change: 1 addition & 0 deletions
1
integration-libs/opf/base/components/opf-cta/opf-cta-element/opf-cta-element.component.html
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 @@ | ||
<div *ngIf="ctaScriptHtml" [innerHTML]="ctaScriptHtml | cxSafeHtml"></div> |
19 changes: 19 additions & 0 deletions
19
...ration-libs/opf/base/components/opf-cta/opf-cta-element/opf-cta-element.component.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,19 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { OpfCtaElementComponent } from './opf-cta-element.component'; | ||
|
||
describe('OpfCtaButton', () => { | ||
let component: OpfCtaElementComponent; | ||
let fixture: ComponentFixture<OpfCtaElementComponent>; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [OpfCtaElementComponent], | ||
}); | ||
fixture = TestBed.createComponent(OpfCtaElementComponent); | ||
component = fixture.componentInstance; | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
integration-libs/opf/base/components/opf-cta/opf-cta-element/opf-cta-element.component.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,16 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2023 SAP Spartacus team <[email protected]> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { ChangeDetectionStrategy, Component, Input } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'cx-opf-cta-element', | ||
templateUrl: './opf-cta-element.component.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class OpfCtaElementComponent { | ||
@Input() ctaScriptHtml: string; | ||
} |
17 changes: 17 additions & 0 deletions
17
integration-libs/opf/base/components/opf-cta/opf-cta-element/opf-cta-element.module.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,17 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2023 SAP Spartacus team <[email protected]> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { CommonModule } from '@angular/common'; | ||
import { NgModule } from '@angular/core'; | ||
import { SafeHtmlModule } from '@spartacus/storefront'; | ||
import { OpfCtaElementComponent } from './opf-cta-element.component'; | ||
|
||
@NgModule({ | ||
declarations: [OpfCtaElementComponent], | ||
imports: [CommonModule, SafeHtmlModule], | ||
exports: [OpfCtaElementComponent], | ||
}) | ||
export class OpfCtaElementModule {} |
9 changes: 9 additions & 0 deletions
9
integration-libs/opf/base/components/opf-cta/opf-cta-scripts/index.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,9 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2023 SAP Spartacus team <[email protected]> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export * from './opf-cta-scripts.component'; | ||
export * from './opf-cta-scripts.module'; | ||
export * from './opf-cta-scripts.service'; |
13 changes: 13 additions & 0 deletions
13
integration-libs/opf/base/components/opf-cta/opf-cta-scripts/opf-cta-scripts.component.html
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,13 @@ | ||
<ng-container *ngIf="ctaHtmls$ | async as ctaHtmls; else loading"> | ||
<ng-container *ngIf="ctaHtmls?.length"> | ||
<cx-opf-cta-element | ||
*ngFor="let ctaHtml of ctaHtmls" | ||
[ctaScriptHtml]="ctaHtml" | ||
></cx-opf-cta-element> | ||
</ng-container> | ||
</ng-container> | ||
<ng-template #loading> | ||
<div class="cx-spinner"> | ||
<cx-spinner></cx-spinner> | ||
</div> | ||
</ng-template> |
78 changes: 78 additions & 0 deletions
78
...ration-libs/opf/base/components/opf-cta/opf-cta-scripts/opf-cta-scripts.component.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,78 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { of, throwError } from 'rxjs'; | ||
import { OpfCtaScriptsComponent } from './opf-cta-scripts.component'; | ||
import { OpfCtaScriptsService } from './opf-cta-scripts.service'; | ||
import createSpy = jasmine.createSpy; | ||
|
||
const mockHtmlsList = [ | ||
'<div style="border-style: solid;text-align:center;border-radius:10px;align-content:center;background-color:yellow;color:black"><h2>Thanks for purchasing our great products</h2><h3>Please use promo code:<b>123abc</b> for your next purchase<h3></div><script>console.log(\'CTA Script #1 is running\')</script>', | ||
'<div style="border-style: solid;text-align:center;border-radius:10px;align-content:center;background-color:yellow;color:black"><h2>Thanks again for purchasing our great products</h2><h3>Please use promo code:<b>123abc</b> for your next purchase<h3></div><script>console.log(\'CTA Script #2 is running\')</script>', | ||
]; | ||
const ctaElementSelector = 'cx-opf-cta-element'; | ||
describe('OpfCtaScriptsComponent', () => { | ||
let component: OpfCtaScriptsComponent; | ||
let fixture: ComponentFixture<OpfCtaScriptsComponent>; | ||
let opfCtaScriptsService: jasmine.SpyObj<OpfCtaScriptsService>; | ||
|
||
const createComponentInstance = () => { | ||
fixture = TestBed.createComponent(OpfCtaScriptsComponent); | ||
component = fixture.componentInstance; | ||
}; | ||
beforeEach(() => { | ||
opfCtaScriptsService = jasmine.createSpyObj('OpfCtaScriptsService', [ | ||
'getCtaHtmlslList', | ||
]); | ||
|
||
TestBed.configureTestingModule({ | ||
declarations: [OpfCtaScriptsComponent], | ||
providers: [ | ||
{ provide: OpfCtaScriptsService, useValue: opfCtaScriptsService }, | ||
], | ||
}).compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
opfCtaScriptsService.getCtaHtmlslList.and.returnValue(of(mockHtmlsList)); | ||
createComponentInstance(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it('should return Htmls list and display ctaButton elements', (done) => { | ||
component.ctaHtmls$.subscribe((htmlList) => { | ||
expect(htmlList[0]).toBeTruthy(); | ||
fixture.detectChanges(); | ||
expect( | ||
fixture.nativeElement.querySelectorAll(ctaElementSelector).length | ||
).toEqual(2); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should isError be true when error is thrown', (done) => { | ||
opfCtaScriptsService.getCtaHtmlslList = createSpy().and.returnValue( | ||
throwError('error') | ||
); | ||
createComponentInstance(); | ||
component.ctaHtmls$.subscribe((htmlList) => { | ||
expect(htmlList).toEqual([]); | ||
fixture.detectChanges(); | ||
expect( | ||
fixture.nativeElement.querySelector(ctaElementSelector) | ||
).toBeFalsy(); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should display spinner when html list is undefined', (done) => { | ||
opfCtaScriptsService.getCtaHtmlslList = createSpy().and.returnValue( | ||
of(undefined) | ||
); | ||
createComponentInstance(); | ||
fixture.detectChanges(); | ||
expect(fixture.nativeElement.querySelector('cx-spinner')).toBeTruthy(); | ||
done(); | ||
}); | ||
}); |
25 changes: 25 additions & 0 deletions
25
integration-libs/opf/base/components/opf-cta/opf-cta-scripts/opf-cta-scripts.component.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,25 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2023 SAP Spartacus team <[email protected]> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { ChangeDetectionStrategy, Component, inject } from '@angular/core'; | ||
import { of } from 'rxjs'; | ||
import { catchError } from 'rxjs/operators'; | ||
import { OpfCtaScriptsService } from './opf-cta-scripts.service'; | ||
|
||
@Component({ | ||
selector: 'cx-opf-cta-scripts', | ||
templateUrl: './opf-cta-scripts.component.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class OpfCtaScriptsComponent { | ||
protected opfCtaScriptService = inject(OpfCtaScriptsService); | ||
|
||
ctaHtmls$ = this.opfCtaScriptService.getCtaHtmlslList().pipe( | ||
catchError(() => { | ||
return of([]); | ||
}) | ||
); | ||
} |
32 changes: 32 additions & 0 deletions
32
integration-libs/opf/base/components/opf-cta/opf-cta-scripts/opf-cta-scripts.module.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,32 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2023 SAP Spartacus team <[email protected]> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { CommonModule } from '@angular/common'; | ||
import { NgModule } from '@angular/core'; | ||
import { | ||
CmsConfig, | ||
FeaturesConfig, | ||
provideDefaultConfig, | ||
} from '@spartacus/core'; | ||
import { SpinnerModule } from '@spartacus/storefront'; | ||
import { OpfCtaElementModule } from '../opf-cta-element'; | ||
import { OpfCtaScriptsComponent } from './opf-cta-scripts.component'; | ||
|
||
@NgModule({ | ||
declarations: [OpfCtaScriptsComponent], | ||
providers: [ | ||
provideDefaultConfig(<CmsConfig | FeaturesConfig>{ | ||
cmsComponents: { | ||
OpfCtaScriptsComponent: { | ||
component: OpfCtaScriptsComponent, | ||
}, | ||
}, | ||
}), | ||
], | ||
exports: [OpfCtaScriptsComponent], | ||
imports: [CommonModule, OpfCtaElementModule, SpinnerModule], | ||
}) | ||
export class OpfCtaScriptsModule {} |
Oops, something went wrong.