Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheFlo committed Oct 25, 2024
1 parent dab76e4 commit a1757df
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,25 @@ describe('OpfResourceLoaderService', () => {
opfResourceLoaderService = TestBed.inject(OpfResourceLoaderService);
});

it('should embed styles with SSR when platform is set to server', fakeAsync(() => {
it('should not loadStyles with SSR when platform is set to server', fakeAsync(() => {
const mockStyleResource = {
url: 'style-url',
type: OpfDynamicScriptResourceType.STYLES,
};

spyOn<any>(opfResourceLoaderService, 'embedStyles').and.callThrough();

spyOn<any>(opfResourceLoaderService, 'loadStyles').and.callThrough();
opfResourceLoaderService.loadProviderResources([], [mockStyleResource]);
expect(opfResourceLoaderService['loadStyles']).not.toHaveBeenCalled();
}));

expect(opfResourceLoaderService['embedStyles']).toHaveBeenCalled();
it('should not loadScript with SSR when platform is set to server', fakeAsync(() => {
const mockScriptResource = {
url: 'script-url',
type: OpfDynamicScriptResourceType.SCRIPT,
};
spyOn<any>(opfResourceLoaderService, 'loadScript').and.callThrough();
opfResourceLoaderService.loadProviderResources([], [mockScriptResource]);
expect(opfResourceLoaderService['loadScript']).not.toHaveBeenCalled();
}));
});

Expand Down Expand Up @@ -369,4 +377,22 @@ describe('OpfResourceLoaderService', () => {
expect(console.log).toHaveBeenCalledWith('Script executed');
});
});

describe('executeHtml in SSR', () => {
it('should not execute script with SSR when platform is set to server', () => {
TestBed.overrideProvider(PLATFORM_ID, { useValue: 'server' });
opfResourceLoaderService = TestBed.inject(OpfResourceLoaderService);

const mockScript = document.createElement('script');
mockScript.innerText = 'console.log("Script executed");';
spyOn(document, 'createElement').and.returnValue(mockScript);
spyOn(console, 'log');

opfResourceLoaderService.executeScriptFromHtml(
'<script>console.log("Script executed");</script>'
);

expect(console.log).not.toHaveBeenCalledWith('Script executed');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ export class OpfResourceLoaderService extends ScriptLoader {
}): void {
const { src, callback, errorCallback } = embedOptions;

const isSSR = isPlatformServer(this.platformId);

if (isSSR) {
return;
}

const link: HTMLLinkElement = this.document.createElement('link');
link.href = src;
link.rel = 'stylesheet';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,34 @@ describe('OpfGlobalFunctionsService', () => {
expect(service).toBeTruthy();
});

describe('Global Functions in SSR', () => {
const mockPaymentSessionId = 'mockSessionId';
let windowOpf: any;

it('should not register global functions for CHECKOUT in SSR', () => {
spyOn<any>(service, 'registerSubmit').and.callThrough();
spyOn(windowRef, 'isBrowser').and.returnValue(false);
service.registerGlobalFunctions({
domain: GlobalFunctionsDomain.CHECKOUT,
paymentSessionId: mockPaymentSessionId,
vcr: {} as ViewContainerRef,
});
expect(service['registerSubmit']).not.toHaveBeenCalled();
});

it('should not remove global functions for CHECKOUT in SSR', () => {
service.registerGlobalFunctions({
domain: GlobalFunctionsDomain.CHECKOUT,
paymentSessionId: mockPaymentSessionId,
vcr: {} as ViewContainerRef,
});
windowOpf = windowRef.nativeWindow['Opf'];
spyOn(windowRef, 'isBrowser').and.returnValue(false);
service.removeGlobalFunctions(GlobalFunctionsDomain.CHECKOUT);
expect(windowOpf['payments']['checkout']['submit']).toBeDefined();
});
});

describe('should register global functions for CHECKOUT domain', () => {
const mockPaymentSessionId = 'mockSessionId';
let windowOpf: any;
Expand Down

0 comments on commit a1757df

Please sign in to comment.