Skip to content

Commit

Permalink
Gh6866 MasterWidget (#6872)
Browse files Browse the repository at this point in the history
* Extract isNumberField function to an external util file

* format dateParse arr from the constants prevent DRY

* Removed the logic from componentDidMount that was causing extra re-rendering by setting the state

* Added back the original doc

* Add unit tests for the isNumberField fnct

* Ditch the UNSAFE_componentWillReceiveProp

* handlePatch refactoring

* More refactoring within MasterWidget move validatePrecision outside

* Add unit tests for formatValueByWidgetType

* Add unit tests for the validatePrecision

* Import only the necessary actions

* Removed desc on main render function

* Changed variable name

* Added extra safety checks

Co-authored-by: Petrica Nanca <petrica.nanca@metasfresh.com>
  • Loading branch information
petrican and Petrica Nanca committed Jun 18, 2020
1 parent 0dd19e6 commit 20cc8d5
Show file tree
Hide file tree
Showing 3 changed files with 227 additions and 139 deletions.
88 changes: 88 additions & 0 deletions frontend/src/__tests__/utils/widgetHelper.test.js
@@ -0,0 +1,88 @@
import {
isNumberField,
formatValueByWidgetType,
validatePrecision,
} from '../../utils/widgetHelper';

describe('Widget helpers', () => {
describe('is NumberField function', () => {
it('createAmmount works correctly', () => {
const resultToCheckOne = isNumberField('Integer');
expect(resultToCheckOne).toBe(true);

const resultToCheckTwo = isNumberField('Amount');
expect(resultToCheckTwo).toBe(true);

const resultToCheckThree = isNumberField('Quantity');
expect(resultToCheckThree).toBe(true);

const resultToCheckFour = isNumberField('Date');
expect(resultToCheckFour).toBe(false);

const resultToCheckFive = isNumberField('Color');
expect(resultToCheckFive).toBe(false);
});
});

describe('formatValueByWidgetType function', () => {
it('works correctly', () => {
const resultToCheckOne = formatValueByWidgetType({
widgetType: 'Quantity',
value: '',
});
expect(resultToCheckOne).toBe(null);

const resultToCheckTwo = formatValueByWidgetType({
widgetType: 'Amount',
value: null,
});
expect(resultToCheckTwo).toBe('0');
});
});

describe('validatePrecision function', () => {
it('works correctly', () => {
const resultToCheckOne = validatePrecision({
widgetValue: '223,544334',
widgetType: 'Quantity',
precision: 2,
});
expect(resultToCheckOne).toBe(true);


const resultToCheckTwo = validatePrecision({
widgetValue: '223,544334',
widgetType: 'Quantity',
precision: 0,
});
expect(resultToCheckTwo).toBe(true);


const resultToCheckThree = validatePrecision({
widgetValue: '223.544334',
widgetType: 'Amount',
precision: 0,
});

expect(resultToCheckThree).toBe(false);
});

it('test null and empty object as value', () => {
const resultToCheckNull = validatePrecision({
widgetValue: null,
widgetType: 'Quantity',
precision: 0,
});
expect(resultToCheckNull).toBe(false);


const resultToCheckEmptyObj = validatePrecision({
widgetValue: {},
widgetType: 'Amount',
precision: 0,
});

expect(resultToCheckEmptyObj).toBe(false);
});
});
});

0 comments on commit 20cc8d5

Please sign in to comment.