Skip to content

Commit 5d2343f

Browse files
committed
jest tests for updates
1 parent 671bf21 commit 5d2343f

4 files changed

Lines changed: 149 additions & 8 deletions

File tree

packages/components/src/internal/components/entities/models.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,7 @@ export class EntityIdCreationModel extends Record({
406406
parentCols.forEach(parentCol => {
407407
const parents: any[] = values.get(parentCol);
408408
parents.forEach(parent => {
409-
// create a copy of the values map, to retain other non-parent values, and then update the parentCol
410-
let singleParentValues = Map<string, any>(values);
409+
let singleParentValues = Map<string, any>();
411410
singleParentValues = singleParentValues.set(parentCol, List<any>([parent]));
412411
for (let c = 0; c < this.numPerParent; c++) {
413412
data = data.push(singleParentValues);

packages/components/src/internal/components/samples/StorageAmountInput.test.tsx

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('StorageAmountInput', () => {
1212
render(
1313
<StorageAmountInput
1414
model={testModel}
15-
preferredUnit={undefined}
15+
preferredUnit={'mL'}
1616
label={undefined}
1717
amountChangedHandler={jest.fn()}
1818
/>
@@ -40,11 +40,10 @@ describe('StorageAmountInput', () => {
4040
});
4141

4242
test('Metric units, preferred units same', () => {
43-
const unit = 'uL';
4443
render(
4544
<StorageAmountInput
4645
model={testModel}
47-
preferredUnit={unit}
46+
preferredUnit={'uL'}
4847
label={undefined}
4948
amountChangedHandler={jest.fn()}
5049
unitsChangedHandler={jest.fn}
@@ -53,7 +52,42 @@ describe('StorageAmountInput', () => {
5352

5453
expect(document.querySelector('input.storage-amount-input')).toHaveProperty('value', "0");
5554
expect(document.querySelectorAll('.checkin-unit-select')).toHaveLength(1);
56-
expect(document.querySelector('.checkin-unit-select').textContent).toBe(unit);
55+
expect(document.querySelector('.checkin-unit-select').textContent).toBe('uL');
56+
expect(document.querySelectorAll('.storage-item-check-in-preferred-display')).toHaveLength(0);
57+
});
58+
59+
test('Metric units, preferred units different', () => {
60+
render(
61+
<StorageAmountInput
62+
model={testModel}
63+
preferredUnit="mL"
64+
label={undefined}
65+
amountChangedHandler={jest.fn()}
66+
unitsChangedHandler={jest.fn}
67+
/>
68+
);
69+
70+
expect(document.querySelector('input.storage-amount-input')).toHaveProperty('value', "0");
71+
expect(document.querySelectorAll('.checkin-unit-select')).toHaveLength(1);
72+
expect(document.querySelector('.checkin-unit-select').textContent).toBe('uL');
73+
expect(document.querySelectorAll('.storage-item-check-in-preferred-display')).toHaveLength(0);
74+
});
75+
76+
test('Metric unit with display in preferred units', () => {
77+
render(
78+
<StorageAmountInput
79+
model={new UnitModel(10, 'uL')}
80+
preferredUnit="mL"
81+
label={undefined}
82+
amountChangedHandler={jest.fn()}
83+
unitsChangedHandler={jest.fn}
84+
/>
85+
);
86+
87+
expect(document.querySelector('input.storage-amount-input')).toHaveProperty('value', "10");
88+
expect(document.querySelectorAll('.checkin-unit-select')).toHaveLength(1);
89+
expect(document.querySelector('.checkin-unit-select').textContent).toBe('uL');
90+
expect(document.querySelector('.storage-item-check-in-preferred-display').textContent).toBe('Displayed as 0.01 mL');
5791
});
5892

5993
test('Label check', () => {

packages/components/src/internal/util/utils.test.ts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ import {
3434
getUpdatedData,
3535
getValueFromRow,
3636
getValuesSummary,
37+
hasAmountOrUnitChanged,
3738
isBoolean,
3839
isImage,
3940
isInteger,
4041
isIntegerInRange,
4142
isNonNegativeFloat,
4243
isNonNegativeInteger,
4344
isQuotedWithDelimiters,
45+
isSameWithStringCompare,
4446
isSetEqual,
4547
makeCommaSeparatedString,
4648
parseCsvString,
@@ -474,6 +476,12 @@ describe('getUpdatedData', () => {
474476
Other: {
475477
value: null,
476478
},
479+
StoredAmount: {
480+
value: 1,
481+
},
482+
Units: {
483+
value: 'mL',
484+
},
477485
},
478486
});
479487

@@ -512,6 +520,14 @@ describe('getUpdatedData', () => {
512520
name: 'IntValue',
513521
fieldKey: 'IntValue',
514522
}),
523+
storedamount: new QueryColumn({
524+
name: 'StoredAmount',
525+
fieldKey: 'StoredAmount',
526+
}),
527+
units: new QueryColumn({
528+
name: 'Units',
529+
fieldKey: 'Units',
530+
}),
515531
}),
516532
});
517533

@@ -560,6 +576,40 @@ describe('getUpdatedData', () => {
560576
});
561577
});
562578

579+
test('changed value for amount but not units', () => {
580+
const updatedData = getUpdatedData(
581+
originalData,
582+
{
583+
StoredAmount: 2,
584+
Units: 'mL',
585+
},
586+
queryInfo
587+
);
588+
expect(updatedData).toHaveLength(1);
589+
expect(updatedData[0]).toStrictEqual({
590+
RowId: 445,
591+
StoredAmount: 2,
592+
Units: 'mL',
593+
});
594+
});
595+
596+
test('changed value for units but not amount', () => {
597+
const updatedData = getUpdatedData(
598+
originalData,
599+
{
600+
StoredAmount: 1,
601+
Units: 'uL',
602+
},
603+
queryInfo
604+
);
605+
expect(updatedData).toHaveLength(1);
606+
expect(updatedData[0]).toStrictEqual({
607+
RowId: 445,
608+
StoredAmount: 1,
609+
Units: 'uL',
610+
});
611+
});
612+
563613
test('changed values for all', () => {
564614
const updatedData = getUpdatedData(
565615
originalData,
@@ -1565,3 +1615,59 @@ describe('isSetEqual', () => {
15651615
expect(isSetEqual([getDomainDetailsJSON, 1], [getDomainDetailsJSON, 1, 2])).toBe(false);
15661616
});
15671617
});
1618+
1619+
describe('hasAmountOrUnitChanged', () => {
1620+
test('updated amount', () => {
1621+
const originalRowMap = fromJS({ StoredAmount: { value: 5 }, Units: { value: 'mg' } });
1622+
expect(hasAmountOrUnitChanged(Map({ Amount: 10, Units: 'mg' }), originalRowMap)).toBe(false);
1623+
expect(hasAmountOrUnitChanged(Map({ StoredAmount: 5, Units: 'mg' }), originalRowMap)).toBe(false);
1624+
expect(hasAmountOrUnitChanged(Map({ StoredAmount: 5.1, Units: 'mg' }), originalRowMap)).toBe(true);
1625+
expect(hasAmountOrUnitChanged(Map({ StoredAmount: 10, Units: 'mg' }), originalRowMap)).toBe(true);
1626+
expect(hasAmountOrUnitChanged(Map({ StoredAmount: null, Units: 'mg' }), originalRowMap)).toBe(true);
1627+
expect(hasAmountOrUnitChanged(Map({ StoredAmount: undefined, Units: 'mg' }), originalRowMap)).toBe(true);
1628+
});
1629+
1630+
test('updated unit', () => {
1631+
const originalRowMap = fromJS({ StoredAmount: { value: 5 }, Units: { value: 'mg' } });
1632+
expect(hasAmountOrUnitChanged(Map({ StoredAmount: 5, RawUnits: 'mg' }), originalRowMap)).toBe(false);
1633+
expect(hasAmountOrUnitChanged(Map({ StoredAmount: 5, Units: 'mg' }), originalRowMap)).toBe(false);
1634+
expect(hasAmountOrUnitChanged(Map({ StoredAmount: 5, Units: 'g' }), originalRowMap)).toBe(true);
1635+
expect(hasAmountOrUnitChanged(Map({ StoredAmount: 5, Units: null }), originalRowMap)).toBe(true);
1636+
expect(hasAmountOrUnitChanged(Map({ StoredAmount: 5, Units: undefined }), originalRowMap)).toBe(true);
1637+
});
1638+
});
1639+
1640+
describe('isSameWithStringCompare', () => {
1641+
test('both empty', () => {
1642+
expect(isSameWithStringCompare(undefined, undefined)).toBe(true);
1643+
expect(isSameWithStringCompare(null, null)).toBe(true);
1644+
expect(isSameWithStringCompare('', '')).toBe(true);
1645+
});
1646+
1647+
test('one empty', () => {
1648+
expect(isSameWithStringCompare(undefined, 'abc')).toBe(false);
1649+
expect(isSameWithStringCompare(null, 123)).toBe(false);
1650+
expect(isSameWithStringCompare('', true)).toBe(false);
1651+
expect(isSameWithStringCompare('abc', undefined)).toBe(false);
1652+
expect(isSameWithStringCompare(123, null)).toBe(false);
1653+
expect(isSameWithStringCompare(true, '')).toBe(false);
1654+
});
1655+
1656+
test('non-empty values', () => {
1657+
expect(isSameWithStringCompare('abc', 'abc')).toBe(true);
1658+
expect(isSameWithStringCompare('abc ', 'abc')).toBe(false);
1659+
expect(isSameWithStringCompare('abc', 'ABC')).toBe(false);
1660+
expect(isSameWithStringCompare(' abc', 'abc')).toBe(false);
1661+
expect(isSameWithStringCompare('abc', 'abcd')).toBe(false);
1662+
expect(isSameWithStringCompare('abc', 'ab')).toBe(false);
1663+
});
1664+
1665+
test('numeric values', () => {
1666+
expect(isSameWithStringCompare(123, 123)).toBe(true);
1667+
expect(isSameWithStringCompare(123, '123')).toBe(true);
1668+
expect(isSameWithStringCompare(123, ' 123')).toBe(false);
1669+
expect(isSameWithStringCompare(123, '123 ')).toBe(false);
1670+
expect(isSameWithStringCompare(123, 123.0)).toBe(true);
1671+
expect(isSameWithStringCompare(123, 123.1)).toBe(false);
1672+
});
1673+
});

packages/components/src/internal/util/utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ export function getCommonDataValues(data: Map<any, any>, fileFields?: string[]):
269269
return valueMap.toObject();
270270
}
271271

272-
function hasAmountOrUnitChanged(updatedValuesMap: Map<string, any>, originalRowMap: Map<string, any>): boolean {
272+
// exported for jest testing
273+
export function hasAmountOrUnitChanged(updatedValuesMap: Map<string, any>, originalRowMap: Map<string, any>): boolean {
273274
// if we have an updated value for amount and it has been changed, return true
274275
if (
275276
updatedValuesMap.has(STORED_AMOUNT_FIELDS.AMOUNT) &&
@@ -293,7 +294,8 @@ function hasAmountOrUnitChanged(updatedValuesMap: Map<string, any>, originalRowM
293294
return false;
294295
}
295296

296-
function isSameWithStringCompare(value1: any, value2: any): boolean {
297+
// export for jest testing
298+
export function isSameWithStringCompare(value1: any, value2: any): boolean {
297299
if (value1 === value2 || (valueIsEmpty(value1) && valueIsEmpty(value2))) return true;
298300
if (value1 && value2) {
299301
const strVal1 = value1.toString();

0 commit comments

Comments
 (0)