-
Notifications
You must be signed in to change notification settings - Fork 21
chore(component): update utils to prepare for slider #6436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alizedebray
wants to merge
2
commits into
main
Choose a base branch
from
slider-component-utils
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| export type PropertyType = 'boolean' | 'number' | 'string' | 'array' | 'object' | 'function'; | ||
| export type PrimitiveType = 'boolean' | 'number' | 'string'; | ||
| export type ReferenceType = 'array' | 'object' | 'function'; | ||
|
|
||
| export type PropertyType = PrimitiveType | ReferenceType; |
This file contains hidden or 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,3 @@ | ||
| export function clamp(value: number, min: number, max: number): number { | ||
| return Math.min(Math.max(value, min), max); | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| import { EMPTY_VALUES } from './property-checkers/constants'; | ||
|
|
||
| export function isValueEmpty(value: unknown): boolean { | ||
| return EMPTY_VALUES.some(v => v === value); | ||
| return value == null || value === '' || (typeof value === 'number' && isNaN(value)); | ||
| } | ||
18 changes: 18 additions & 0 deletions
18
packages/components/src/utils/property-checkers/check-array-of.ts
This file contains hidden or 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,18 @@ | ||
| import { PrimitiveType } from '@/types/property-types'; | ||
|
|
||
| export function checkArrayOf<T extends { host: HTMLElement }>( | ||
| component: T, | ||
| prop: keyof T, | ||
| type: PrimitiveType, | ||
| ) { | ||
| const componentName = component.host.localName; | ||
| const value = component[prop]; | ||
|
|
||
| const message = `The prop \`${String( | ||
| prop, | ||
| )}\` of the \`${componentName}\` component must be an \`${type}\` array.`; | ||
|
|
||
| if (!Array.isArray(value) || value.some(val => typeof val !== type)) { | ||
| console.error(message); | ||
| } | ||
| } |
This file contains hidden or 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
65 changes: 65 additions & 0 deletions
65
packages/components/src/utils/property-checkers/tests/check-array-of.spec.ts
This file contains hidden or 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,65 @@ | ||
| import { checkArrayOf } from '../check-array-of'; | ||
| import { PrimitiveType } from '@/types'; | ||
| import { describe } from 'node:test'; | ||
|
|
||
| describe('checkArrayOf', () => { | ||
| const componentName = 'post-component'; | ||
| const propName = 'myProp'; | ||
| const mockValues = [ | ||
| undefined, | ||
| null, | ||
| true, | ||
| false, | ||
| 42, | ||
| NaN, | ||
| 'string', | ||
| '', | ||
| [], | ||
| {}, | ||
| () => { | ||
| /* empty */ | ||
| }, | ||
| ]; | ||
|
|
||
| let consoleErrorSpy: jest.SpyInstance; | ||
|
|
||
| beforeEach(() => { | ||
| consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| consoleErrorSpy.mockRestore(); | ||
| }); | ||
|
|
||
| const primitiveTypes: PrimitiveType[] = ['boolean', 'string', 'number']; | ||
| primitiveTypes.forEach(type => { | ||
| describe(type, () => { | ||
| const error = `The prop \`${propName}\` of the \`${componentName}\` component must be an \`${type}\` array.`; | ||
|
|
||
| const runCheckForValue = (value: unknown) => { | ||
| const component = { host: { localName: componentName } as HTMLElement, [propName]: value }; | ||
| checkArrayOf(component, propName, type); | ||
| }; | ||
|
|
||
| it('should log an error if the value is not an array', () => { | ||
| mockValues | ||
| .filter(value => !Array.isArray(value)) | ||
| .forEach(value => { | ||
| runCheckForValue(value); | ||
| expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringContaining(error)); | ||
| }); | ||
| }); | ||
|
|
||
| it('should log an error if the array contains some values that don\'t have the expected type', () => { | ||
| runCheckForValue(mockValues); | ||
| expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringContaining(error)); | ||
| }); | ||
|
|
||
| it('should not log an error if the array contains only values with the expected type', () => { | ||
| const validArray = mockValues.filter(value => typeof value === type); | ||
| runCheckForValue(validArray); | ||
| expect(consoleErrorSpy).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,12 +1,11 @@ | ||
| import { requiredAnd } from '../required-and'; | ||
| import { EMPTY_VALUES } from '../constants'; | ||
| describe('requiredAnd', () => { | ||
| const mockCheck = jest.fn(); | ||
|
|
||
| const mockRequiredAndCheck = requiredAnd(mockCheck); | ||
|
|
||
| it('should throw error if the provided value is empty', () => { | ||
| EMPTY_VALUES.forEach(emptyValue => { | ||
| [undefined, null, '', NaN].forEach(emptyValue => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we have to update the values which are considered as "empty", we also have to update the test. |
||
| const component = { host: { localName: 'post-component' } as HTMLElement, prop: emptyValue }; | ||
| const prop = component['prop']; | ||
| const error = `The prop \`${emptyValue}\` of the \`post-component\` component is not defined.`; | ||
|
|
@@ -17,7 +16,6 @@ describe('requiredAnd', () => { | |
| it('should run the check if the provided value is not empty', () => { | ||
| [ | ||
| 0, | ||
| NaN, | ||
| ' ', | ||
| false, | ||
| [], | ||
|
|
@@ -36,7 +34,7 @@ describe('requiredAnd', () => { | |
| }); | ||
|
|
||
| it('should pass all provided arguments to the nested check function', () => { | ||
| const args = ['non empty value', true, false, ['arg in an array'], { arg: 'in an object' }]; | ||
| const args = [0, false, 'text', [], {}]; | ||
|
|
||
| args.forEach(arg => { | ||
| const component = { host: { localName: 'post-component' } as HTMLElement, prop: arg }; | ||
|
|
||
This file contains hidden or 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,15 @@ | ||
| import { clamp } from '../clamp'; | ||
|
|
||
| describe('clamp', () => { | ||
| it('should return the provided value if within the bounds', () => { | ||
| expect(clamp(10, 0, 100)).toEqual(10); | ||
| }); | ||
|
|
||
| it('should return the min if provided value is lower', () => { | ||
| expect(clamp(0, 10, 100)).toEqual(10); | ||
| }); | ||
|
|
||
| it('should return the max if provided value is greater', () => { | ||
| expect(clamp(100, 0, 10)).toEqual(10); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest to keep the รฌsValueEmpty` function as is but change the way we compare the values.
The following would allow us to compare NaN with NaN, beside all the other values:
return EMPTY_VALUES.some(v => Object.is(v, value));Test:
NaN === NaN-> falseObject.is(NaN, NaN)-> trueThe idea behind this is, to allow editing the tested values โโwithout touching the test logic itself, thus minimizing the risk of regressions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We didnโt notice the function wasnโt working earlier because both the function and its tests were using the same
EMPTY_VALUESarray. As a result,NaN === NaNevaluated to true, but only because both values referred to the exact sameNaNinstance from that shared array. In general, itโs best to decouple tests from implementation details as much as possible to avoid false negatives like this.