Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Move assertion utils out of components #3007

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/bruno-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"start": "next start",
"lint": "next lint",
"test": "jest",
"test:watch": "jest --watch",
"test:prettier": "prettier --check \"./src/**/*.{js,jsx,json,ts,tsx}\"",
"prettier": "prettier --write \"./src/**/*.{js,jsx,json,ts,tsx}\""
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,88 +1,14 @@
import React from 'react';

/**
* Assertion operators
*
* eq : equal to
* neq : not equal to
* gt : greater than
* gte : greater than or equal to
* lt : less than
* lte : less than or equal to
* in : in
* notIn : not in
* contains : contains
* notContains : not contains
* length : length
* matches : matches
* notMatches : not matches
* startsWith : starts with
* endsWith : ends with
* between : between
* isEmpty : is empty
* isNull : is null
* isUndefined : is undefined
* isDefined : is defined
* isTruthy : is truthy
* isFalsy : is falsy
* isJson : is json
* isNumber : is number
* isString : is string
* isBoolean : is boolean
* isArray : is array
*/
import { operators, getOperatorLabel } from 'utils/testing/assertions/index';

const AssertionOperator = ({ operator, onChange }) => {
const operators = [
'eq',
'neq',
'gt',
'gte',
'lt',
'lte',
'in',
'notIn',
'contains',
'notContains',
'length',
'matches',
'notMatches',
'startsWith',
'endsWith',
'between',
'isEmpty',
'isNull',
'isUndefined',
'isDefined',
'isTruthy',
'isFalsy',
'isJson',
'isNumber',
'isString',
'isBoolean',
'isArray'
];

const handleChange = (e) => {
onChange(e.target.value);
};

const getLabel = (operator) => {
switch (operator) {
case 'eq':
return 'equals';
case 'neq':
return 'notEquals';
default:
return operator;
}
};
const handleChange = (e) => onChange(e.target.value);

return (
<select value={operator} onChange={handleChange} className="mousetrap">
{operators.map((operator) => (
<option key={operator} value={operator}>
{getLabel(operator)}
{getOperatorLabel(operator)}
</option>
))}
</select>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,143 +3,11 @@ import { IconTrash } from '@tabler/icons';
import SingleLineEditor from 'components/SingleLineEditor';
import AssertionOperator from '../AssertionOperator';
import { useTheme } from 'providers/Theme';
import { parseAssertion, unaryOperators } from 'utils/testing/assertions/index';

/**
* Assertion operators
*
* eq : equal to
* neq : not equal to
* gt : greater than
* gte : greater than or equal to
* lt : less than
* lte : less than or equal to
* in : in
* notIn : not in
* contains : contains
* notContains : not contains
* length : length
* matches : matches
* notMatches : not matches
* startsWith : starts with
* endsWith : ends with
* between : between
* isEmpty : is empty
* isNull : is null
* isUndefined : is undefined
* isDefined : is defined
* isTruthy : is truthy
* isFalsy : is falsy
* isJson : is json
* isNumber : is number
* isString : is string
* isBoolean : is boolean
* isArray : is array
*/
const parseAssertionOperator = (str = '') => {
if (!str || typeof str !== 'string' || !str.length) {
return {
operator: 'eq',
value: str
};
}

const operators = [
'eq',
'neq',
'gt',
'gte',
'lt',
'lte',
'in',
'notIn',
'contains',
'notContains',
'length',
'matches',
'notMatches',
'startsWith',
'endsWith',
'between',
'isEmpty',
'isNull',
'isUndefined',
'isDefined',
'isTruthy',
'isFalsy',
'isJson',
'isNumber',
'isString',
'isBoolean',
'isArray'
];

const unaryOperators = [
'isEmpty',
'isNull',
'isUndefined',
'isDefined',
'isTruthy',
'isFalsy',
'isJson',
'isNumber',
'isString',
'isBoolean',
'isArray'
];

const [operator, ...rest] = str.trim().split(' ');
const value = rest.join(' ');

if (unaryOperators.includes(operator)) {
return {
operator,
value: ''
};
}

if (operators.includes(operator)) {
return {
operator,
value
};
}

return {
operator: 'eq',
value: str
};
};

const isUnaryOperator = (operator) => {
const unaryOperators = [
'isEmpty',
'isNull',
'isUndefined',
'isDefined',
'isTruthy',
'isFalsy',
'isJson',
'isNumber',
'isString',
'isBoolean',
'isArray'
];

return unaryOperators.includes(operator);
};

const AssertionRow = ({
item,
collection,
assertion,
handleAssertionChange,
handleRemoveAssertion,
onSave,
handleRun
}) => {
const AssertionRow = ({ collection, assertion, handleAssertionChange, handleRemoveAssertion, onSave, handleRun }) => {
const { storedTheme } = useTheme();

const { operator, value } = parseAssertionOperator(assertion.value);
const { operator, value } = parseAssertion(assertion.value);

return (
<tr key={assertion.uid}>
Expand Down Expand Up @@ -172,7 +40,7 @@ const AssertionRow = ({
/>
</td>
<td>
{!isUnaryOperator(operator) ? (
{!unaryOperators.includes(operator) ? (
<SingleLineEditor
value={value}
theme={storedTheme}
Expand Down
102 changes: 102 additions & 0 deletions packages/bruno-app/src/utils/testing/assertions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import _ from 'lodash';

/**
* Assertion operators
* eq : equal to
* neq : not equal to
* gt : greater than
* gte : greater than or equal to
* lt : less than
* lte : less than or equal to
* in : in
* notIn : not in
* contains : contains
* notContains : not contains
* length : length
* matches : matches
* notMatches : not matches
* startsWith : starts with
* endsWith : ends with
* between : between
* isEmpty : is empty
* isNull : is null
* isUndefined : is undefined
* isDefined : is defined
* isTruthy : is truthy
* isFalsy : is falsy
* isJson : is json
* isNumber : is number
* isString : is string
* isBoolean : is boolean
* isArray : is array
*/

export const unaryOperators = [
'isEmpty',
'isNull',
'isUndefined',
'isDefined',
'isTruthy',
'isFalsy',
'isJson',
'isNumber',
'isString',
'isBoolean',
'isArray'
];

export const nonUnaryOperators = ['eq', 'neq', 'gt', 'gte', 'lt', 'lte'];

export const specialOperators = [
'in',
'notIn',
'contains',
'notContains',
'length',
'matches',
'notMatches',
'startsWith',
'endsWith',
'between'
];

export const operators = [...unaryOperators, ...nonUnaryOperators, ...specialOperators];

export const getOperatorLabel = (operator) => {
switch (operator) {
case 'eq':
return 'equals';
case 'neq':
return 'notEquals';
default:
return operator;
}
};

export const parseAssertion = (assertionString = '') => {
const defaultResult = {
operator: 'eq',
value: ''
};

if (!_.isString(assertionString)) {
return defaultResult;
}

const [operator, ...rest] = assertionString.trim().split(' ');
const value = rest.join(' ');

if (unaryOperators.includes(operator)) {
return {
operator,
value: ''
};
} else if ([...nonUnaryOperators, ...specialOperators].includes(operator)) {
return {
operator,
value
};
} else {
return defaultResult;
}
};
Loading