snapshot of https://github.com/frappe/crm.git (develop, 2026-09-18; blobs > 131072B omitted)
Some checks failed
Frontend / Unit Tests & Coverage (push) Has been skipped
Regenerate POT file (translatable strings) / Regenerate POT file (develop) (push) Failing after 1m13s
Regenerate POT file (translatable strings) / Regenerate POT file (main-hotfix) (push) Failing after 6s
Server / Tests (frappe ${{ matrix.frappe-branch }}) (push) Has been skipped
Server / Resolve frappe matrix (push) Failing after 1s
Frontend / Lint (push) Failing after 1s
Frontend / Production Build (push) Failing after 1s
Server / Coverage Wrap Up (push) Has been skipped
Frontend / Coverage Wrap Up (push) Has been skipped
Some checks failed
Frontend / Unit Tests & Coverage (push) Has been skipped
Regenerate POT file (translatable strings) / Regenerate POT file (develop) (push) Failing after 1m13s
Regenerate POT file (translatable strings) / Regenerate POT file (main-hotfix) (push) Failing after 6s
Server / Tests (frappe ${{ matrix.frappe-branch }}) (push) Has been skipped
Server / Resolve frappe matrix (push) Failing after 1s
Frontend / Lint (push) Failing after 1s
Frontend / Production Build (push) Failing after 1s
Server / Coverage Wrap Up (push) Has been skipped
Frontend / Coverage Wrap Up (push) Has been skipped
This commit is contained in:
128
frontend/tests/unit/applyStateFieldOptions.test.js
Normal file
128
frontend/tests/unit/applyStateFieldOptions.test.js
Normal file
@@ -0,0 +1,128 @@
|
||||
import { applyStateFieldOptions } from '@/utils/fieldTransforms'
|
||||
|
||||
const stateField = { fieldname: 'state', fieldtype: 'Data', label: 'State' }
|
||||
const stateOptions = { India: ['Goa', 'Kerala', 'Punjab'] }
|
||||
|
||||
describe('applyStateFieldOptions', () => {
|
||||
it('turns the Address state field into an Autocomplete for a known country', () => {
|
||||
const result = applyStateFieldOptions(
|
||||
stateField,
|
||||
{ country: 'India' },
|
||||
'Address',
|
||||
stateOptions,
|
||||
)
|
||||
expect(result.fieldtype).toBe('Autocomplete')
|
||||
expect(result.options).toEqual(['Goa', 'Kerala', 'Punjab'])
|
||||
})
|
||||
|
||||
it('prepends an out-of-list current state value so it stays visible', () => {
|
||||
const result = applyStateFieldOptions(
|
||||
stateField,
|
||||
{ country: 'India', state: 'California' },
|
||||
'Address',
|
||||
stateOptions,
|
||||
)
|
||||
expect(result.options).toEqual(['California', 'Goa', 'Kerala', 'Punjab'])
|
||||
})
|
||||
|
||||
it('does not duplicate a current value already in the list', () => {
|
||||
const result = applyStateFieldOptions(
|
||||
stateField,
|
||||
{ country: 'India', state: 'Goa' },
|
||||
'Address',
|
||||
stateOptions,
|
||||
)
|
||||
expect(result.options).toEqual(['Goa', 'Kerala', 'Punjab'])
|
||||
})
|
||||
|
||||
it('does not mutate the input field', () => {
|
||||
applyStateFieldOptions(
|
||||
stateField,
|
||||
{ country: 'India' },
|
||||
'Address',
|
||||
stateOptions,
|
||||
)
|
||||
expect(stateField.fieldtype).toBe('Data')
|
||||
expect(stateField.options).toBeUndefined()
|
||||
})
|
||||
|
||||
it('leaves the field unchanged when the country has no state list', () => {
|
||||
const result = applyStateFieldOptions(
|
||||
stateField,
|
||||
{ country: 'France' },
|
||||
'Address',
|
||||
stateOptions,
|
||||
)
|
||||
expect(result).toBe(stateField)
|
||||
})
|
||||
|
||||
it('hints to pick a country first when regional state data exists but no country is set', () => {
|
||||
const withEmptyDoc = applyStateFieldOptions(
|
||||
stateField,
|
||||
{},
|
||||
'Address',
|
||||
stateOptions,
|
||||
)
|
||||
expect(withEmptyDoc.fieldtype).toBe('Data')
|
||||
expect(withEmptyDoc.placeholder).toBe('Select Country to see state options')
|
||||
|
||||
const withNullDoc = applyStateFieldOptions(
|
||||
stateField,
|
||||
null,
|
||||
'Address',
|
||||
stateOptions,
|
||||
)
|
||||
expect(withNullDoc.placeholder).toBe('Select Country to see state options')
|
||||
})
|
||||
|
||||
it('does not mutate the input field when adding the country hint', () => {
|
||||
applyStateFieldOptions(stateField, {}, 'Address', stateOptions)
|
||||
expect(stateField.placeholder).toBeUndefined()
|
||||
})
|
||||
|
||||
it('leaves the field unchanged when no options map is provided (app absent)', () => {
|
||||
expect(
|
||||
applyStateFieldOptions(
|
||||
stateField,
|
||||
{ country: 'India' },
|
||||
'Address',
|
||||
undefined,
|
||||
),
|
||||
).toBe(stateField)
|
||||
expect(
|
||||
applyStateFieldOptions(stateField, { country: 'India' }, 'Address', {}),
|
||||
).toBe(stateField)
|
||||
})
|
||||
|
||||
it('only applies to the Address doctype', () => {
|
||||
const result = applyStateFieldOptions(
|
||||
stateField,
|
||||
{ country: 'India' },
|
||||
'CRM Lead',
|
||||
stateOptions,
|
||||
)
|
||||
expect(result).toBe(stateField)
|
||||
})
|
||||
|
||||
it('only applies to the state field', () => {
|
||||
const cityField = { fieldname: 'city', fieldtype: 'Data' }
|
||||
const result = applyStateFieldOptions(
|
||||
cityField,
|
||||
{ country: 'India' },
|
||||
'Address',
|
||||
stateOptions,
|
||||
)
|
||||
expect(result).toBe(cityField)
|
||||
})
|
||||
|
||||
it('handles a nullish field safely', () => {
|
||||
expect(
|
||||
applyStateFieldOptions(
|
||||
null,
|
||||
{ country: 'India' },
|
||||
'Address',
|
||||
stateOptions,
|
||||
),
|
||||
).toBeNull()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user