Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yuliacech committed Apr 4, 2024
1 parent d57a527 commit c4e984a
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
42 changes: 42 additions & 0 deletions packages/kbn-monaco/src/console/parser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { createParser } from './parser';
import { ConsoleParserResult } from './types';

const parser = createParser();
describe('console parser', () => {
it('returns errors if input is not correct', () => {
const input = 'Incorrect input';
const parserResult = parser(input) as ConsoleParserResult;
// the parser logs 2 errors: for the unexpected method and a general syntax error
expect(parserResult.errors.length).toBe(2);
// the parser logs a beginning of the request that it's trying to parse
expect(parserResult.requests.length).toBe(1);
});

it('returns parsedRequests if the input is correct', () => {
const input = 'GET _search';
const { requests, errors } = parser(input) as ConsoleParserResult;
expect(requests.length).toBe(1);
expect(errors.length).toBe(0);
const { method, url, startOffset, endOffset } = requests[0];
expect(method).toBe('GET');
expect(url).toBe('_search');
// the start offset of the request is the beginning of the string
expect(startOffset).toBe(0);
// the end offset of the request is the end of the string
expect(endOffset).toBe(11);
});

it('parses several requests', () => {
const input = 'GET _search\nPOST _test_index';
const { requests } = parser(input) as ConsoleParserResult;
expect(requests.length).toBe(2);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

/*
* Mock kbn/monaco to provide the console parser code directly without a web worker
*/
const mockGetParsedRequests = jest.fn();
jest.mock('@kbn/monaco', () => {
const original = jest.requireActual('@kbn/monaco');
return {
...original,
getParsedRequestsProvider: () => {
return {
getRequests: mockGetParsedRequests,
};
},
};
});

jest.mock('../../../../services', () => {
return {
getStorage: () => ({
get: () => [],
}),
StorageKeys: {
VARIABLES: 'test',
},
};
});

import { MonacoEditorActionsProvider } from './monaco_editor_actions_provider';
import { monaco } from '@kbn/monaco';

describe('Editor actions provider', () => {
let editorActionsProvider: MonacoEditorActionsProvider;
let editor: jest.Mocked<monaco.editor.IStandaloneCodeEditor>;
beforeEach(() => {
editor = {
getModel: jest.fn(),
createDecorationsCollection: () => ({
clear: jest.fn(),
set: jest.fn(),
}),
focus: jest.fn(),
onDidChangeCursorPosition: jest.fn(),
onDidScrollChange: jest.fn(),
onDidChangeCursorSelection: jest.fn(),
onDidContentSizeChange: jest.fn(),
getSelection: jest.fn(),
getTopForLineNumber: jest.fn(),
getScrollTop: jest.fn(),
} as unknown as jest.Mocked<monaco.editor.IStandaloneCodeEditor>;

editor.getModel.mockReturnValue({
getLineMaxColumn: () => 10,
getPositionAt: () => ({ lineNumber: 1 }),
getLineContent: () => 'GET _search',
} as unknown as monaco.editor.ITextModel);
editor.getSelection.mockReturnValue({
startLineNumber: 1,
endLineNumber: 1,
} as unknown as monaco.Selection);
mockGetParsedRequests.mockResolvedValue([
{
startOffset: 0,
endOffset: 11,
method: 'GET',
url: '_search',
},
]);

const setEditorActionsCssMock = jest.fn();

editorActionsProvider = new MonacoEditorActionsProvider(editor, setEditorActionsCssMock);
});

describe('getCurl', () => {
it('returns an empty string if no requests', async () => {
mockGetParsedRequests.mockResolvedValue([]);
const curl = await editorActionsProvider.getCurl('http://localhost');
expect(curl).toBe('');
});

it('returns an empty string if there is a request but not in the selection range', async () => {
editor.getSelection.mockReturnValue({
// the request is on line 1, the user selected line 2
startLineNumber: 2,
endLineNumber: 2,
} as unknown as monaco.Selection);
const curl = await editorActionsProvider.getCurl('http://localhost');
expect(curl).toBe('');
});

it('returns the correct string if there is a request in the selection range', async () => {
const curl = await editorActionsProvider.getCurl('http://localhost');
expect(curl).toBe('curl -XGET "http://localhost/_search" -H "kbn-xsrf: reporting"');
});
});
});

0 comments on commit c4e984a

Please sign in to comment.