generated from kbysiec/vite-vanilla-ts-lib-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make useCondition() more type-safe - v0.1.2 (#6)
Fixes #4 Make `useCondition()` more type-safe by only allowing field paths that are associated with keys of the conditional logic config instead of every possible field path associated with the form schema. If a user specifies an invalid field path (not associated with a condition), we'll still return `true` (in the returned array) but will also log a warning in the console.
- Loading branch information
Showing
10 changed files
with
177 additions
and
58 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,90 @@ | ||
import { get } from 'lodash-es'; | ||
import { afterAll, describe, expect, it, test, vi } from 'vitest'; | ||
import { getConditionalLogic as gcl } from '../src/utils/conditional-logic'; | ||
|
||
const formValues = { | ||
contactName: 'Micah', | ||
contactEmail: '', | ||
caterer: null, | ||
guests: [ | ||
{ | ||
name: 'Ben', | ||
age: 24, | ||
hasGloves: true, | ||
bottles: [ | ||
{ | ||
grape: 'Tempranillo', | ||
sips: 3, | ||
isSmudgedByThisGuest: undefined, | ||
isSmudgedByFirstGuest: undefined, | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'Kate', | ||
age: 28, | ||
hasGloves: false, | ||
bottles: [ | ||
{ | ||
grape: 'Tempranillo', | ||
sips: 2, | ||
isSmudgedByThisGuest: undefined, | ||
isSmudgedByFirstGuest: undefined, | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'Joseph', | ||
age: 16, | ||
hasGloves: false, | ||
}, | ||
], | ||
}; | ||
const conditions: Record<string, (getValues: (key: string) => unknown) => boolean> = { | ||
contactEmail: gv => (gv('contactName') as string).length > 0, | ||
'guests.#.bottles.#.isSmudgedByThisGuest': gv => | ||
(gv('guests.#.hasGloves') as boolean | undefined) !== true, | ||
'guests.#.bottles.#.isSmudgedByFirstGuest': gv => | ||
(gv('guests.0.hasGloves') as boolean | undefined) !== true, | ||
}; | ||
const getValues = ((paths: string | string[]) => | ||
Array.isArray(paths) | ||
? paths.map(path => get(formValues, path)) | ||
: get(formValues, paths)) as any; // eslint-disable-line @typescript-eslint/no-explicit-any | ||
|
||
test('Basic case', () => { | ||
expect(gcl(['contactEmail'], conditions, getValues)).toStrictEqual([true]); | ||
}); | ||
|
||
test('Referencing parent in same lineage', () => { | ||
expect( | ||
gcl(['guests.0.bottles.0.isSmudgedByThisGuest'], conditions, getValues) | ||
).toStrictEqual([false]); | ||
expect( | ||
gcl(['guests.1.bottles.0.isSmudgedByThisGuest'], conditions, getValues) | ||
).toStrictEqual([true]); | ||
}); | ||
|
||
test('Referencing parent in different lineage', () => { | ||
expect( | ||
gcl(['guests.0.bottles.0.isSmudgedByFirstGuest'], conditions, getValues) | ||
).toStrictEqual([false]); | ||
expect( | ||
gcl(['guests.1.bottles.0.isSmudgedByFirstGuest'], conditions, getValues) | ||
).toStrictEqual([false]); | ||
}); | ||
|
||
describe('mocking console.warn...', () => { | ||
const consoleWarnMock = vi.spyOn(console, 'warn').mockImplementation(() => undefined); | ||
afterAll(() => { | ||
consoleWarnMock.mockReset(); | ||
}); | ||
|
||
it('Log warning when looking up invalid condition', () => { | ||
expect(gcl(['not_a_valid_condition'], conditions, getValues)).toStrictEqual([true]); | ||
expect(consoleWarnMock).toHaveBeenCalledOnce(); | ||
expect(consoleWarnMock).toHaveBeenLastCalledWith( | ||
'Missing RHF conditional logic for "not_a_valid_condition"' | ||
); | ||
}); | ||
}); |