import { AxiosRequestConfig } from 'axios'; import { isSameOrigin } from '../is-same-origin'; describe(isSameOrigin.name, () => { const originalLocation = window.location; let config: AxiosRequestConfig; function mockLocation(location: any) { Object.defineProperty(window, 'location', { configurable: true, value: location }); } beforeEach(() => { mockLocation(originalLocation); config = {}; }); const subject = () => isSameOrigin(config); function itReturns(value: boolean) { test(`returns ${value}`, () => { expect(subject()).toBe(value); }); } describe('with no url', () => { beforeEach(() => delete config.url); itReturns(false); }); describe('with relative url', () => { beforeEach(() => (config.url = '/foo')); itReturns(true); describe('with baseURL', () => { beforeEach(() => (config.baseURL = 'https://www.example.com/bar')); itReturns(false); describe('when window.location.origin matches baseURL', () => { beforeEach(() => mockLocation(new URL(config.baseURL!))); itReturns(true); }); describe('when baseURL is relative', () => { beforeEach(() => (config.baseURL = '/bar')); itReturns(true); }); }); }); describe('with absolute url', () => { beforeEach(() => (config.url = 'https://www.example.com/foo')); itReturns(false); describe('when absolute url matches window.location.origin', () => { beforeEach(() => (config.url = `${window.location.origin}/foo`)); itReturns(true); }); }); });