-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(app): create utils to format value for rtp (#14720)
* refactor(app): create utils to format value for rtp
1 parent
1ba6166
commit 7c39473
Showing
8 changed files
with
151 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
app/src/organisms/ProtocolDetails/ProtocolParameters/__tests__/utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { useTranslation } from 'react-i18next' | ||
import { describe, it, expect, vi } from 'vitest' | ||
import { formatRunTimeParameterValue } from '../utils' | ||
|
||
import type { RunTimeParameter } from '@opentrons/shared-data' | ||
|
||
const capitalizeFirstLetter = (str: string): string => { | ||
return str.charAt(0).toUpperCase() + str.slice(1) | ||
} | ||
|
||
const mockTFunction = vi.fn(str => capitalizeFirstLetter(str)) | ||
|
||
vi.mock('react-i18next', async importOriginal => { | ||
const actual = await importOriginal<typeof useTranslation>() | ||
return { | ||
...actual, | ||
t: mockTFunction, | ||
} | ||
}) | ||
|
||
describe('utils-formatRunTimeParameterValue', () => { | ||
it('should return value with suffix when type is int', () => { | ||
const mockData = { | ||
value: 6, | ||
displayName: 'PCR Cycles', | ||
variableName: 'PCR_CYCLES', | ||
description: 'number of PCR cycles on a thermocycler', | ||
type: 'int', | ||
min: 1, | ||
max: 10, | ||
default: 6, | ||
} as RunTimeParameter | ||
const result = formatRunTimeParameterValue(mockData, mockTFunction) | ||
expect(result).toEqual('6') | ||
}) | ||
|
||
it('should return value with suffix when type is float', () => { | ||
const mockData = { | ||
value: 6.5, | ||
displayName: 'EtoH Volume', | ||
variableName: 'ETOH_VOLUME', | ||
description: '70% ethanol volume', | ||
type: 'float', | ||
suffix: 'mL', | ||
min: 1.5, | ||
max: 10.0, | ||
default: 6.5, | ||
} as RunTimeParameter | ||
const result = formatRunTimeParameterValue(mockData, mockTFunction) | ||
expect(result).toEqual('6.5 mL') | ||
}) | ||
|
||
it('should return value with suffix when type is str', () => { | ||
const mockData = { | ||
value: 'left', | ||
displayName: 'pipette mount', | ||
variableName: 'mont', | ||
description: 'pipette mount', | ||
type: 'str', | ||
choices: [ | ||
{ | ||
displayName: 'Left', | ||
value: 'left', | ||
}, | ||
{ | ||
displayName: 'Right', | ||
value: 'right', | ||
}, | ||
], | ||
default: 'left', | ||
} as RunTimeParameter | ||
const result = formatRunTimeParameterValue(mockData, mockTFunction) | ||
expect(result).toEqual('Left') | ||
}) | ||
|
||
it('should return value with suffix when type is boolean true', () => { | ||
const mockData = { | ||
value: true, | ||
displayName: 'Deactivate Temperatures', | ||
variableName: 'DEACTIVATE_TEMP', | ||
description: 'deactivate temperature on the module', | ||
type: 'boolean', | ||
default: true, | ||
} as RunTimeParameter | ||
const result = formatRunTimeParameterValue(mockData, mockTFunction) | ||
expect(result).toEqual('On') | ||
}) | ||
|
||
it('should return value with suffix when type is boolean false', () => { | ||
const mockData = { | ||
value: false, | ||
displayName: 'Dry Run', | ||
variableName: 'DRYRUN', | ||
description: 'Is this a dry or wet run? Wet is true, dry is false', | ||
type: 'boolean', | ||
default: false, | ||
} as RunTimeParameter | ||
const result = formatRunTimeParameterValue(mockData, mockTFunction) | ||
expect(result).toEqual('Off') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
app/src/organisms/ProtocolDetails/ProtocolParameters/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useTranslation } from 'react-i18next' | ||
import type { RunTimeParameter } from '@opentrons/shared-data' | ||
|
||
export const formatRunTimeParameterValue = ( | ||
runTimeParameter: RunTimeParameter, | ||
t: ReturnType<typeof useTranslation>['t'] | ||
): string => { | ||
const { type, default: defaultValue } = runTimeParameter | ||
const suffix = | ||
'suffix' in runTimeParameter && runTimeParameter.suffix != null | ||
? runTimeParameter.suffix | ||
: null | ||
switch (type) { | ||
case 'int': | ||
case 'float': | ||
return suffix !== null | ||
? `${defaultValue.toString()} ${suffix}` | ||
: defaultValue.toString() | ||
case 'boolean': | ||
return Boolean(defaultValue) ? t('on') : t('off') | ||
case 'str': | ||
if ('choices' in runTimeParameter && runTimeParameter.choices != null) { | ||
const choice = runTimeParameter.choices.find( | ||
choice => choice.value === defaultValue | ||
) | ||
if (choice != null) { | ||
return choice.displayName | ||
} | ||
} | ||
break | ||
} | ||
return '' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters