/* eslint-env jest */ import { UNREACHABLE_MESSAGE, isUnreachable, readableFailure, recomazeHost, } from '../connection'; describe('recomazeHost', () => { it('names the Recomaze service a call went to', () => { expect( recomazeHost('https://agent.recomaze.ai/api/visibility/settings') ).toBe('agent.recomaze.ai'); expect(recomazeHost('/api/me', 'https://api.recomaze.ai')).toBe( 'api.recomaze.ai' ); }); it('leaves out calls to WordPress itself', () => { expect(recomazeHost('http://localhost:8080/wp-json/recomaze/v1/seo')).toBe( '' ); expect(recomazeHost(undefined)).toBe(''); }); }); describe('isUnreachable', () => { it('counts no answer and gateway errors as the service being down', () => { expect(isUnreachable({ message: 'Network Error' })).toBe(true); expect(isUnreachable({ response: { status: 503 } })).toBe(true); }); it('leaves answers from the service to the page', () => { expect(isUnreachable({ response: { status: 404 } })).toBe(false); expect(isUnreachable({ response: { status: 500 } })).toBe(false); expect(isUnreachable({ code: 'ERR_CANCELED' })).toBe(false); }); }); describe('readableFailure', () => { it("prints the service's own words when it gave some", () => { expect( readableFailure({ response: { status: 409, data: { detail: 'A scan is already running.' }, }, }) ).toBe('A scan is already running.'); }); it("never prints axios's status line or a server error's own text", () => { expect( readableFailure({ message: 'Request failed with status code 500', response: { status: 500, data: { detail: 'Internal Server Error' } }, }) ).toBe( 'Recomaze could not complete that (error 500). Try again in a moment.' ); expect(readableFailure({ message: 'Network Error' })).toBe( UNREACHABLE_MESSAGE ); }); });