/** @jest-environment jsdom */ import { requestKey } from '../../integrations/request-key'; describe('[journey] requestKey', () => { // Do not redefine window.location — jsdom's property is non-configurable. const pageOrigin = window.location.origin; describe('with a same-origin absolute url', () => { const subject = () => requestKey(`${pageOrigin}/api/jobs`, undefined, undefined); test('returns the pathname', () => { expect(subject()).toBe('/api/jobs'); }); }); describe('with a relative url', () => { const subject = () => requestKey('/CallScreen/BookJob', undefined, undefined); test('returns the pathname against the page origin', () => { expect(subject()).toBe('/CallScreen/BookJob'); }); }); describe('with a cross-origin url', () => { const subject = () => requestKey('https://pdf.vendor.com/render', undefined, undefined); test('returns host/path', () => { expect(subject()).toBe('pdf.vendor.com/render'); }); }); describe('with configBaseURL and instanceBaseURL', () => { const subject = () => requestKey('/invoices', 'https://api.example.com', 'https://other.example.com'); test('prefers configBaseURL for resolution', () => { expect(subject()).toBe('api.example.com/invoices'); }); }); describe('with only instanceBaseURL', () => { const subject = () => requestKey('/invoices', undefined, 'https://api.example.com'); test('uses the instance home', () => { expect(subject()).toBe('/invoices'); }); }); describe('with a cross-origin url against a different instance home', () => { const subject = () => requestKey('https://pdf.vendor.com/render', undefined, 'https://api.example.com'); test('returns host/path', () => { expect(subject()).toBe('pdf.vendor.com/render'); }); }); describe('when URL construction fails', () => { const subject = () => requestKey('http://[', undefined, undefined); test('returns the raw url', () => { expect(subject()).toBe('http://['); }); }); describe('when url is undefined and construction fails', () => { const subject = () => requestKey(undefined, '::not-a-url::', undefined); test('returns an empty string', () => { expect(subject()).toBe(''); }); }); describe('when instanceBaseURL is invalid', () => { const subject = () => requestKey(`${pageOrigin}/ok`, pageOrigin, '::bad::'); test('falls back to the page origin', () => { expect(subject()).toBe('/ok'); }); }); });