File size: 1,877 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
// @ts-nocheck - TODO: Fix TypeScript issues
import wpcom from 'calypso/lib/wp';
import * as Logger from 'calypso/server/lib/logger';
import { logError } from '../log-error';
jest.mock( 'calypso/lib/wp' );
jest.mock( 'calypso/server/lib/logger', () => {
const logger = jest.fn();
return {
getLogger: () => ( { error: logger } ),
};
} );
function setSsrContext() {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
globalThis.window = undefined;
}
const mockWindow = {
FormData: class {
append( key: string, value: unknown ) {
this[ key ] = value;
}
},
fetch: jest.fn(),
};
function setBrowserContext() {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
globalThis.window = mockWindow;
}
beforeEach( () => {
jest.resetAllMocks();
setBrowserContext();
} );
describe( 'logError', () => {
it( 'should log to the server in SSR', () => {
setSsrContext();
logError( { message: 'asdf', foo: 'bar' } );
expect( jest.mocked( Logger.getLogger().error ).mock.calls ).toMatchInlineSnapshot( `
Array [
Array [
Object {
"message": "asdf",
"properties": Object {
"context": "explat",
"explat_client": "calypso",
"foo": "bar",
"message": "asdf",
},
},
],
]
` );
} );
it( 'should log to the server in the browser', () => {
logError( { message: 'asdf', foo: 'bar' } );
expect( jest.mocked( wpcom.req.post ).mock.calls ).toMatchInlineSnapshot( `
Array [
Array [
Object {
"apiNamespace": "rest/v1.1",
"body": Object {
"error": "{\\"message\\":\\"asdf\\",\\"properties\\":{\\"foo\\":\\"bar\\",\\"context\\":\\"explat\\",\\"explat_client\\":\\"calypso\\",\\"message\\":\\"asdf\\"}}",
},
"path": "/js-error",
},
[Function],
],
]
` );
} );
} );
|