n8n/packages/nodes-base/test/utils/utilities.test.ts
Iván Ovejero beedfb609c
feat(editor): SQL editor overhaul (#6282)
* Draft setup
*  Implemented expression evaluation in Postgres node, minor SQL editor UI improvements, minor refacring
*  Added initial version of expression preview for SQL editor
*  Linking npm package for codemirror sql grammar instead of a local file
*  Moving expression editor wrapper elements to the component
*  Using expression preview in SQL editor
* Use SQL parser skipping whitespace
*  Added support for custom skipped segments specification
*  Fixing highlight problems with dots and expressions that resolve to zero
* 👕 Fixing linting error
*  Added current item support
*  Added expression support to more nodes with sql editor
*  Added expression support for other nodes
*  Implemented different SQL dialect support
* 🐛 Fixing hard-coded parameter names for editors
*  Fixing preview for nested queries, updating query when input data changes, adding keyboard shortcut to toggle comments
*  Adding a custom automcomplete notice for different editors
*  Updating SQL autocomplete notice
*  Added unit tests for SQL editor
*  Using latest grammar
* 🐛 Fixing code node editor rendering
* 💄 SQL preview dropdown matches editor width. Removing unnecessary css
*  Addressing PR review feedback
* 👌 Addressing PR review feedback pt2
* 👌 Added path alias for utils in nodes-base package
* 👌 Addressing more PR review feedback
*  Adding tests for `getResolvables` utility function
* Fixing lodash imports
* 👌 Better focus handling, adding more plugins to the editor, other minor imrovements
*  Not showing SQL autocomplete suggestions inside expressions
*  Using npm package for sql grammar
*  Removing autocomplete notice, adding line highlight on syntax error
* 👌 Addressing code review feedback
---------
Co-authored-by: Milorad Filipovic <milorad@n8n.io>
2023-06-22 16:47:28 +02:00

140 lines
3.6 KiB
TypeScript

import { fuzzyCompare, getResolvables, keysToLowercase, wrapData } from '@utils/utilities';
//most test cases for fuzzyCompare are done in Compare Datasets node tests
describe('Test fuzzyCompare', () => {
it('should do strict comparison', () => {
const compareFunction = fuzzyCompare(false);
expect(compareFunction(1, '1')).toEqual(false);
});
it('should do fuzzy comparison', () => {
const compareFunction = fuzzyCompare(true);
expect(compareFunction(1, '1')).toEqual(true);
});
it('should treat null, 0 and "0" as equal', () => {
const compareFunction = fuzzyCompare(true, 2);
expect(compareFunction(null, null)).toEqual(true);
expect(compareFunction(null, 0)).toEqual(true);
expect(compareFunction(null, '0')).toEqual(true);
});
it('should not treat null, 0 and "0" as equal', () => {
const compareFunction = fuzzyCompare(true);
expect(compareFunction(null, 0)).toEqual(false);
expect(compareFunction(null, '0')).toEqual(false);
});
});
describe('Test wrapData', () => {
it('should wrap object in json', () => {
const data = {
id: 1,
name: 'Name',
};
const wrappedData = wrapData(data);
expect(wrappedData).toBeDefined();
expect(wrappedData).toEqual([{ json: data }]);
});
it('should wrap each object in array in json', () => {
const data = [
{
id: 1,
name: 'Name',
},
{
id: 2,
name: 'Name 2',
},
];
const wrappedData = wrapData(data);
expect(wrappedData).toBeDefined();
expect(wrappedData).toEqual([{ json: data[0] }, { json: data[1] }]);
});
it('json key from source should be inside json', () => {
const data = {
json: {
id: 1,
name: 'Name',
},
};
const wrappedData = wrapData(data);
expect(wrappedData).toBeDefined();
expect(wrappedData).toEqual([{ json: data }]);
expect(Object.keys(wrappedData[0].json)).toContain('json');
});
});
describe('Test keysToLowercase', () => {
it('should convert keys to lowercase', () => {
const headers = {
'Content-Type': 'application/json',
'X-Test-Header': 'Test',
Accept: 'application/json',
};
const newHeaders = keysToLowercase(headers);
expect(newHeaders).toEqual({
'content-type': 'application/json',
'x-test-header': 'Test',
accept: 'application/json',
});
});
it('should return original value if it is not an object', () => {
const test1 = keysToLowercase(['hello']);
const test2 = keysToLowercase('test');
const test3 = keysToLowercase(1);
const test4 = keysToLowercase(true);
const test5 = keysToLowercase(null);
const test6 = keysToLowercase(undefined);
expect(test1).toEqual(['hello']);
expect(test2).toEqual('test');
expect(test3).toEqual(1);
expect(test4).toEqual(true);
expect(test5).toEqual(null);
expect(test6).toEqual(undefined);
});
});
describe('Test getResolvables', () => {
it('should return empty array when there are no resolvables', () => {
expect(getResolvables('Plain String, no resolvables here.')).toEqual([]);
});
it('should properly handle resovables in SQL query', () => {
expect(getResolvables('SELECT * FROM {{ $json.db }}.{{ $json.table }};')).toEqual([
'{{ $json.db }}',
'{{ $json.table }}',
]);
});
it('should properly handle resovables in HTML string', () => {
expect(
getResolvables(
`
<!DOCTYPE html>
<html>
<head><title>{{ $json.pageTitle }}</title></head>
<body><h1>{{ $json.heading }}</h1></body>
<html>
<style>
body { height: {{ $json.pageHeight }}; }
</style>
<script>
console.log('{{ $json.welcomeMessage }}');
</script>
`,
),
).toEqual([
'{{ $json.pageTitle }}',
'{{ $json.heading }}',
'{{ $json.pageHeight }}',
'{{ $json.welcomeMessage }}',
]);
});
});